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.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
11 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getGetterMethodForNonnull
12 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
13 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isNonnullMethodName
16 import org.opendaylight.mdsal.binding.model.api.AnnotationType
17 import org.opendaylight.mdsal.binding.model.api.Constant
18 import org.opendaylight.mdsal.binding.model.api.Enumeration
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType
20 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
21 import org.opendaylight.mdsal.binding.model.api.MethodSignature
22 import org.opendaylight.mdsal.binding.model.api.Type
23 import org.opendaylight.mdsal.binding.model.util.TypeConstants
24 import org.opendaylight.yangtools.yang.binding.CodeHelpers
27 * Template for generating JAVA interfaces.
29 class InterfaceTemplate extends BaseTemplate {
30 static val JavaTypeName NONNULL = JavaTypeName.create("org.eclipse.jdt.annotation", "NonNull")
31 static val JavaTypeName NULLABLE = JavaTypeName.create("org.eclipse.jdt.annotation", "Nullable")
34 * List of constant instances which are generated as JAVA public static final attributes.
36 val List<Constant> consts
39 * List of method signatures which are generated as method declarations.
41 val List<MethodSignature> methods
44 * List of enumeration which are generated as JAVA enum type.
46 val List<Enumeration> enums
49 * List of generated types which are enclosed inside <code>genType</code>
51 val List<GeneratedType> enclosedGeneratedTypes
54 * Creates the instance of this class which is used for generating the interface file source
55 * code from <code>genType</code>.
57 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
59 new(GeneratedType genType) {
61 if (genType === null) {
62 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
65 consts = genType.constantDefinitions
66 methods = genType.methodDefinitions
67 enums = genType.enumerations
68 enclosedGeneratedTypes = genType.enclosedTypes
72 * Template method which generate the whole body of the interface.
74 * @return string with code for interface body in JAVA format
77 «wrapToDocumentation(formatDataForJavaDoc(type))»
78 «type.annotations.generateAnnotations»
79 public interface «type.name»
83 «generateInnerClasses»
96 def private generateAnnotations(List<AnnotationType> annotations) '''
97 «IF annotations !== null && !annotations.empty»
98 «FOR annotation : annotations»
99 @«annotation.importedName»
100 «IF annotation.parameters !== null && !annotation.parameters.empty»
102 «FOR param : annotation.parameters SEPARATOR ","»
103 «param.name»=«param.value»
112 * Template method which generates the interface name declaration.
114 * @return string with the code for the interface declaration in JAVA format
116 def private superInterfaces()
118 «IF (!type.implements.empty)»
120 «FOR type : type.implements SEPARATOR ","»
127 * Template method which generates inner classes inside this interface.
129 * @return string with the source code for inner classes in JAVA format
131 def private generateInnerClasses() '''
132 «IF !enclosedGeneratedTypes.empty»
133 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
134 «generateInnerClass(innerClass)»
140 * Template method which generates JAVA enum type.
142 * @return string with inner enum source code in JAVA format
144 def private generateEnums() '''
146 «FOR e : enums SEPARATOR "\n"»
147 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
148 «enumTemplate.generateAsInnerClass»
154 * Template method wich generates JAVA constants.
156 * @return string with constants in JAVA format
158 def private generateConstants() '''
161 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
169 * Template method which generates the declaration of the methods.
171 * @return string with the declaration of methods source code in JAVA format
173 def private generateMethods() '''
175 «FOR m : methods SEPARATOR "\n"»
177 «generateDefaultMethod(m)»
178 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
179 «generateAccessorMethod(m)»
187 def private generateDefaultMethod(MethodSignature method) {
188 if (method.name.isNonnullMethodName) {
189 generateNonnullMethod(method)
192 case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
197 def private generateMethod(MethodSignature method) '''
198 «method.comment.asJavadoc»
199 «method.annotations.generateAnnotations»
200 «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
203 def private generateAccessorMethod(MethodSignature method) '''
204 «val ret = method.returnType»
205 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or " + asCode("null") + " if not present")»
206 «method.annotations.generateAnnotations»
207 «nullableType(ret)» «method.name»();
210 def private generateDefaultImplementedInterface() '''
211 @«Override.importedName»
212 default «Class.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
213 return «type.fullyQualifiedName».class;
217 def private generateNonnullMethod(MethodSignature method) '''
218 «val ret = method.returnType»
219 «val name = method.name»
220 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or an empty list if it is not present")»
221 «method.annotations.generateAnnotations»
222 default «ret.importedName(NONNULL.importedName)» «name»() {
223 return «CodeHelpers.importedName».nonnull(«getGetterMethodForNonnull(name)»());
227 def private String nullableType(Type type) {
229 return type.importedName(NULLABLE.importedName)
231 return type.importedName
234 def private static boolean isObject(Type type) {
235 // The return type has a package, so it's not a primitive type
236 return !type.getPackageName().isEmpty()