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.JavaTypeName
16 import org.opendaylight.mdsal.binding.model.api.MethodSignature
17 import org.opendaylight.mdsal.binding.model.api.Type
18 import org.opendaylight.mdsal.binding.model.util.TypeConstants
21 * Template for generating JAVA interfaces.
23 class InterfaceTemplate extends BaseTemplate {
24 static val JavaTypeName NULLABLE = JavaTypeName.create("org.eclipse.jdt.annotation", "Nullable")
27 * List of constant instances which are generated as JAVA public static final attributes.
29 val List<Constant> consts
32 * List of method signatures which are generated as method declarations.
34 val List<MethodSignature> methods
37 * List of enumeration which are generated as JAVA enum type.
39 val List<Enumeration> enums
42 * List of generated types which are enclosed inside <code>genType</code>
44 val List<GeneratedType> enclosedGeneratedTypes
47 * Creates the instance of this class which is used for generating the interface file source
48 * code from <code>genType</code>.
50 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
52 new(GeneratedType genType) {
54 if (genType === null) {
55 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
58 consts = genType.constantDefinitions
59 methods = genType.methodDefinitions
60 enums = genType.enumerations
61 enclosedGeneratedTypes = genType.enclosedTypes
65 * Template method which generate the whole body of the interface.
67 * @return string with code for interface body in JAVA format
70 «wrapToDocumentation(formatDataForJavaDoc(type))»
71 «type.annotations.generateAnnotations»
72 public interface «type.name»
76 «generateInnerClasses»
89 def private generateAnnotations(List<AnnotationType> annotations) '''
90 «IF annotations !== null && !annotations.empty»
91 «FOR annotation : annotations»
92 @«annotation.importedName»
93 «IF annotation.parameters !== null && !annotation.parameters.empty»
95 «FOR param : annotation.parameters SEPARATOR ","»
96 «param.name»=«param.value»
105 * Template method which generates the interface name declaration.
107 * @return string with the code for the interface declaration in JAVA format
109 def private superInterfaces()
111 «IF (!type.implements.empty)»
113 «FOR type : type.implements SEPARATOR ","»
120 * Template method which generates inner classes inside this interface.
122 * @return string with the source code for inner classes in JAVA format
124 def private generateInnerClasses() '''
125 «IF !enclosedGeneratedTypes.empty»
126 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
127 «generateInnerClass(innerClass)»
133 * Template method which generates JAVA enum type.
135 * @return string with inner enum source code in JAVA format
137 def private generateEnums() '''
139 «FOR e : enums SEPARATOR "\n"»
140 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
141 «enumTemplate.generateAsInnerClass»
147 * Template method wich generates JAVA constants.
149 * @return string with constants in JAVA format
151 def private generateConstants() '''
154 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
162 * Template method which generates the declaration of the methods.
164 * @return string with the declaration of methods source code in JAVA format
166 def private generateMethods() '''
168 «FOR m : methods SEPARATOR "\n"»
169 «val accessor = m.isAccessor»
170 «val ret = m.returnType»
172 «formatDataForJavaDoc(m, "@return " + asCode(ret.fullyQualifiedName) + " "
173 + asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
175 «m.comment.asJavadoc»
177 «m.annotations.generateAnnotations»
178 «nullableType(ret, accessor)» «m.name»(«m.parameters.generateParameters»);
183 def private String nullableType(Type type, boolean accessor) {
184 if (accessor && type.isObject) {
185 return importedName(type, NULLABLE.importedName)
187 return type.importedName
190 def private static boolean isObject(Type type) {
191 // The return type has a package, so it's not a primitive type
192 return !type.getPackageName().isEmpty()