2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator
11 import org.opendaylight.mdsal.binding.model.api.AnnotationType
12 import org.opendaylight.mdsal.binding.model.api.Constant
13 import org.opendaylight.mdsal.binding.model.api.Enumeration
14 import org.opendaylight.mdsal.binding.model.api.GeneratedType
15 import org.opendaylight.mdsal.binding.model.api.MethodSignature
16 import org.opendaylight.mdsal.binding.model.util.TypeConstants
19 * Template for generating JAVA interfaces.
21 class InterfaceTemplate extends BaseTemplate {
24 * List of constant instances which are generated as JAVA public static final attributes.
26 val List<Constant> consts
29 * List of method signatures which are generated as method declarations.
31 val List<MethodSignature> methods
34 * List of enumeration which are generated as JAVA enum type.
36 val List<Enumeration> enums
39 * List of generated types which are enclosed inside <code>genType</code>
41 val List<GeneratedType> enclosedGeneratedTypes
44 * Creates the instance of this class which is used for generating the interface file source
45 * code from <code>genType</code>.
47 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
49 new(GeneratedType genType) {
51 if (genType === null) {
52 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
55 consts = genType.constantDefinitions
56 methods = genType.methodDefinitions
57 enums = genType.enumerations
58 enclosedGeneratedTypes = genType.enclosedTypes
62 * Template method which generate the whole body of the interface.
64 * @return string with code for interface body in JAVA format
67 «wrapToDocumentation(formatDataForJavaDoc(type))»
68 «type.annotations.generateAnnotations»
69 public interface «type.name»
73 «generateInnerClasses»
86 def private generateAnnotations(List<AnnotationType> annotations) '''
87 «IF annotations !== null && !annotations.empty»
88 «FOR annotation : annotations»
89 @«annotation.importedName»
90 «IF annotation.parameters !== null && !annotation.parameters.empty»
92 «FOR param : annotation.parameters SEPARATOR ","»
93 «param.name»=«param.value»
102 * Template method which generates the interface name declaration.
104 * @return string with the code for the interface declaration in JAVA format
106 def private superInterfaces()
108 «IF (!type.implements.empty)»
110 «FOR type : type.implements SEPARATOR ","»
117 * Template method which generates inner classes inside this interface.
119 * @return string with the source code for inner classes in JAVA format
121 def private generateInnerClasses() '''
122 «IF !enclosedGeneratedTypes.empty»
123 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
124 «generateInnerClass(innerClass)»
130 * Template method which generates JAVA enum type.
132 * @return string with inner enum source code in JAVA format
134 def private generateEnums() '''
136 «FOR e : enums SEPARATOR "\n"»
137 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
138 «enumTemplate.generateAsInnerClass»
144 * Template method wich generates JAVA constants.
146 * @return string with constants in JAVA format
148 def private generateConstants() '''
151 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
159 * Template method which generates the declaration of the methods.
161 * @return string with the declaration of methods source code in JAVA format
163 def private generateMethods() '''
165 «FOR m : methods SEPARATOR "\n"»
167 «m.comment.asJavadoc»
169 «formatDataForJavaDoc(m, "@return " + asCode(m.returnType.fullyQualifiedName) + " "
170 + asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
172 «m.annotations.generateAnnotations»
173 «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);