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.getGetterMethodForNonnull
11 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
12 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isNonnullMethodName
15 import org.opendaylight.mdsal.binding.model.api.AnnotationType
16 import org.opendaylight.mdsal.binding.model.api.Constant
17 import org.opendaylight.mdsal.binding.model.api.Enumeration
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType
19 import org.opendaylight.mdsal.binding.model.api.MethodSignature
20 import org.opendaylight.mdsal.binding.model.api.Type
21 import org.opendaylight.mdsal.binding.model.util.TypeConstants
22 import org.opendaylight.yangtools.yang.binding.CodeHelpers
25 * Template for generating JAVA interfaces.
27 class InterfaceTemplate extends BaseTemplate {
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"»
172 «generateDefaultMethod(m)»
173 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
174 «generateAccessorMethod(m)»
182 def private generateDefaultMethod(MethodSignature method) {
183 if (method.name.isNonnullMethodName) {
184 return generateNonnullMethod(method)
188 def private generateMethod(MethodSignature method) '''
189 «method.comment.asJavadoc»
190 «method.annotations.generateAnnotations»
191 «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
194 def private generateAccessorMethod(MethodSignature method) '''
195 «val ret = method.returnType»
196 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or " + asCode("null") + " if not present")»
197 «method.annotations.generateAnnotations»
198 «nullableType(ret)» «method.name»();
201 def private generateNonnullMethod(MethodSignature method) '''
202 «val ret = method.returnType»
203 «val name = method.name»
204 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or an empty list if it is not present")»
205 «method.annotations.generateAnnotations»
206 default «ret.importedNonNull» «name»() {
207 return «CodeHelpers.importedName».nonnull(«getGetterMethodForNonnull(name)»());
211 def private String nullableType(Type type) {
213 return type.importedNullable
215 return type.importedName
218 def private static boolean isObject(Type type) {
219 // The return type has a package, so it's not a primitive type
220 return !type.getPackageName().isEmpty()