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.GeneratedTransferObject
15 import org.opendaylight.mdsal.binding.model.api.GeneratedType
16 import org.opendaylight.mdsal.binding.model.api.MethodSignature
17 import org.opendaylight.mdsal.binding.model.util.TypeConstants
20 * Template for generating JAVA interfaces.
22 class InterfaceTemplate extends BaseTemplate {
25 * List of constant instances which are generated as JAVA public static final attributes.
27 val List<Constant> consts
30 * List of method signatures which are generated as method declarations.
32 val List<MethodSignature> methods
35 * List of enumeration which are generated as JAVA enum type.
37 val List<Enumeration> enums
40 * List of generated types which are enclosed inside <code>genType</code>
42 val List<GeneratedType> enclosedGeneratedTypes
45 * Creates the instance of this class which is used for generating the interface file source
46 * code from <code>genType</code>.
48 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
50 new(GeneratedType genType) {
52 if (genType === null) {
53 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
56 consts = genType.constantDefinitions
57 methods = genType.methodDefinitions
58 enums = genType.enumerations
59 enclosedGeneratedTypes = genType.enclosedTypes
63 * Template method which generate the whole body of the interface.
65 * @return string with code for interface body in JAVA format
68 «wrapToDocumentation(formatDataForJavaDoc(type))»
69 «type.annotations.generateAnnotations»
70 public interface «type.name»
74 «generateInnerClasses»
87 def private generateAnnotations(List<AnnotationType> annotations) '''
88 «IF annotations !== null && !annotations.empty»
89 «FOR annotation : annotations»
90 @«annotation.importedName»
91 «IF annotation.parameters !== null && !annotation.parameters.empty»
93 «FOR param : annotation.parameters SEPARATOR ","»
94 «param.name»=«param.value»
103 * Template method which generates the interface name declaration.
105 * @return string with the code for the interface declaration in JAVA format
107 def private superInterfaces()
109 «IF (!type.implements.empty)»
111 «FOR type : type.implements SEPARATOR ","»
118 * Template method which generates inner classes inside this interface.
120 * @return string with the source code for inner classes in JAVA format
122 def private generateInnerClasses() '''
123 «IF !enclosedGeneratedTypes.empty»
124 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
125 «IF (innerClass instanceof GeneratedTransferObject)»
126 «val innerJavaType = javaType.getEnclosedType(innerClass.identifier)»
127 «IF innerClass.unionType»
128 «val unionTemplate = new UnionTemplate(innerJavaType, innerClass)»
129 «unionTemplate.generateAsInnerClass»
131 «val classTemplate = new ClassTemplate(innerJavaType, innerClass)»
132 «classTemplate.generateAsInnerClass»
141 * Template method which generates JAVA enum type.
143 * @return string with inner enum source code in JAVA format
145 def private generateEnums() '''
147 «FOR e : enums SEPARATOR "\n"»
148 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
149 «enumTemplate.generateAsInnerClass»
155 * Template method wich generates JAVA constants.
157 * @return string with constants in JAVA format
159 def private generateConstants() '''
162 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
170 * Template method which generates the declaration of the methods.
172 * @return string with the declaration of methods source code in JAVA format
174 def private generateMethods() '''
176 «FOR m : methods SEPARATOR "\n"»
178 «m.comment.asJavadoc»
180 «formatDataForJavaDoc(m, "@return " + asCode(m.returnType.fullyQualifiedName) + " "
181 + asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
183 «m.annotations.generateAnnotations»
184 «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);