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.gaul.modernizer_maven_annotations.SuppressModernizer
17 import org.opendaylight.mdsal.binding.model.api.AnnotationType
18 import org.opendaylight.mdsal.binding.model.api.Constant
19 import org.opendaylight.mdsal.binding.model.api.Enumeration
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType
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.
30 class InterfaceTemplate extends BaseTemplate {
32 * List of constant instances which are generated as JAVA public static final attributes.
34 val List<Constant> consts
37 * List of method signatures which are generated as method declarations.
39 val List<MethodSignature> methods
42 * List of enumeration which are generated as JAVA enum type.
44 val List<Enumeration> enums
47 * List of generated types which are enclosed inside <code>genType</code>
49 val List<GeneratedType> enclosedGeneratedTypes
52 * Creates the instance of this class which is used for generating the interface file source
53 * code from <code>genType</code>.
55 * @throws NullPointerException if <code>genType</code> is <code>null</code>
57 new(GeneratedType genType) {
59 consts = genType.constantDefinitions
60 methods = genType.methodDefinitions
61 enums = genType.enumerations
62 enclosedGeneratedTypes = genType.enclosedTypes
66 * Template method which generate the whole body of the interface.
68 * @return string with code for interface body in JAVA format
71 «wrapToDocumentation(formatDataForJavaDoc(type))»
72 «type.annotations.generateAnnotations»
73 public interface «type.name»
77 «generateInnerClasses»
89 def private generateAnnotations(List<AnnotationType> annotations) '''
90 «IF annotations !== null && !annotations.empty»
91 «FOR annotation : annotations»
92 «annotation.generateAnnotation»
98 * Template method which generates the interface name declaration.
100 * @return string with the code for the interface declaration in JAVA format
102 def private superInterfaces()
104 «IF (!type.implements.empty)»
106 «FOR type : type.implements SEPARATOR ","»
113 * Template method which generates inner classes inside this interface.
115 * @return string with the source code for inner classes in JAVA format
117 def private generateInnerClasses() '''
118 «IF !enclosedGeneratedTypes.empty»
119 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
120 «generateInnerClass(innerClass)»
126 * Template method which generates JAVA enum type.
128 * @return string with inner enum source code in JAVA format
130 def private generateEnums() '''
132 «FOR e : enums SEPARATOR "\n"»
133 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
134 «enumTemplate.generateAsInnerClass»
140 * Template method wich generates JAVA constants.
142 * @return string with constants in JAVA format
144 def private generateConstants() '''
147 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
155 * Template method which generates the declaration of the methods.
157 * @return string with the declaration of methods source code in JAVA format
159 def private generateMethods() '''
161 «FOR m : methods SEPARATOR "\n"»
163 «generateDefaultMethod(m)»
164 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
165 «generateAccessorMethod(m)»
173 def private generateDefaultMethod(MethodSignature method) {
174 if (method.name.isNonnullMethodName) {
175 generateNonnullMethod(method)
178 case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
183 def private generateMethod(MethodSignature method) '''
184 «method.comment.asJavadoc»
185 «method.annotations.generateAnnotations»
186 «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
189 def private generateAccessorMethod(MethodSignature method) '''
190 «val ret = method.returnType»
191 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or " + asCode("null") + " if not present")»
192 «method.annotations.generateAnnotations»
193 «nullableType(ret)» «method.name»();
196 def private generateDefaultImplementedInterface() '''
197 @«OVERRIDE.importedName»
198 default «Class.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
199 return «type.fullyQualifiedName».class;
203 def private generateNonnullMethod(MethodSignature method) '''
204 «val ret = method.returnType»
205 «val name = method.name»
206 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or an empty list if it is not present")»
207 «method.annotations.generateAnnotations»
208 default «ret.importedNonNull» «name»() {
209 return «CodeHelpers.importedName».nonnull(«getGetterMethodForNonnull(name)»());
213 def private String nullableType(Type type) {
215 return type.importedNullable
217 return type.importedName
220 def private static boolean isObject(Type type) {
221 // The return type has a package, so it's not a primitive type
222 return !type.getPackageName().isEmpty()