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
10 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
13 import org.opendaylight.mdsal.binding.model.api.AnnotationType
14 import org.opendaylight.mdsal.binding.model.api.Constant
15 import org.opendaylight.mdsal.binding.model.api.Enumeration
16 import org.opendaylight.mdsal.binding.model.api.GeneratedType
17 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
18 import org.opendaylight.mdsal.binding.model.api.MethodSignature
19 import org.opendaylight.mdsal.binding.model.api.Type
20 import org.opendaylight.mdsal.binding.model.util.TypeConstants
23 * Template for generating JAVA interfaces.
25 class InterfaceTemplate extends BaseTemplate {
26 static val JavaTypeName NULLABLE = JavaTypeName.create("org.eclipse.jdt.annotation", "Nullable")
29 * List of constant instances which are generated as JAVA public static final attributes.
31 val List<Constant> consts
34 * List of method signatures which are generated as method declarations.
36 val List<MethodSignature> methods
39 * List of enumeration which are generated as JAVA enum type.
41 val List<Enumeration> enums
44 * List of generated types which are enclosed inside <code>genType</code>
46 val List<GeneratedType> enclosedGeneratedTypes
49 * Creates the instance of this class which is used for generating the interface file source
50 * code from <code>genType</code>.
52 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
54 new(GeneratedType genType) {
56 if (genType === null) {
57 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
60 consts = genType.constantDefinitions
61 methods = genType.methodDefinitions
62 enums = genType.enumerations
63 enclosedGeneratedTypes = genType.enclosedTypes
67 * Template method which generate the whole body of the interface.
69 * @return string with code for interface body in JAVA format
72 «wrapToDocumentation(formatDataForJavaDoc(type))»
73 «type.annotations.generateAnnotations»
74 public interface «type.name»
78 «generateInnerClasses»
91 def private generateAnnotations(List<AnnotationType> annotations) '''
92 «IF annotations !== null && !annotations.empty»
93 «FOR annotation : annotations»
94 @«annotation.importedName»
95 «IF annotation.parameters !== null && !annotation.parameters.empty»
97 «FOR param : annotation.parameters SEPARATOR ","»
98 «param.name»=«param.value»
107 * Template method which generates the interface name declaration.
109 * @return string with the code for the interface declaration in JAVA format
111 def private superInterfaces()
113 «IF (!type.implements.empty)»
115 «FOR type : type.implements SEPARATOR ","»
122 * Template method which generates inner classes inside this interface.
124 * @return string with the source code for inner classes in JAVA format
126 def private generateInnerClasses() '''
127 «IF !enclosedGeneratedTypes.empty»
128 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
129 «generateInnerClass(innerClass)»
135 * Template method which generates JAVA enum type.
137 * @return string with inner enum source code in JAVA format
139 def private generateEnums() '''
141 «FOR e : enums SEPARATOR "\n"»
142 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
143 «enumTemplate.generateAsInnerClass»
149 * Template method wich generates JAVA constants.
151 * @return string with constants in JAVA format
153 def private generateConstants() '''
156 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
164 * Template method which generates the declaration of the methods.
166 * @return string with the declaration of methods source code in JAVA format
168 def private generateMethods() '''
170 «FOR m : methods SEPARATOR "\n"»
171 «val accessor = m.name.isGetterMethodName»
172 «val ret = m.returnType»
174 «formatDataForJavaDoc(m, "@return " + asCode(ret.fullyQualifiedName) + " "
175 + asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
177 «m.comment.asJavadoc»
179 «m.annotations.generateAnnotations»
180 «nullableType(ret, accessor)» «m.name»(«m.parameters.generateParameters»);
185 def private String nullableType(Type type, boolean accessor) {
186 if (accessor && type.isObject) {
187 return importedName(type, NULLABLE.importedName)
189 return type.importedName
192 def private static boolean isObject(Type type) {
193 // The return type has a package, so it's not a primitive type
194 return !type.getPackageName().isEmpty()