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.MethodSignature
21 import org.opendaylight.mdsal.binding.model.api.Type
22 import org.opendaylight.mdsal.binding.model.util.TypeConstants
23 import org.opendaylight.yangtools.yang.binding.CodeHelpers
26 * Template for generating JAVA interfaces.
28 class InterfaceTemplate extends BaseTemplate {
30 * List of constant instances which are generated as JAVA public static final attributes.
32 val List<Constant> consts
35 * List of method signatures which are generated as method declarations.
37 val List<MethodSignature> methods
40 * List of enumeration which are generated as JAVA enum type.
42 val List<Enumeration> enums
45 * List of generated types which are enclosed inside <code>genType</code>
47 val List<GeneratedType> enclosedGeneratedTypes
50 * Creates the instance of this class which is used for generating the interface file source
51 * code from <code>genType</code>.
53 * @throws NullPointerException if <code>genType</code> is <code>null</code>
55 new(GeneratedType genType) {
57 consts = genType.constantDefinitions
58 methods = genType.methodDefinitions
59 enums = genType.enumerations
60 enclosedGeneratedTypes = genType.enclosedTypes
64 * Template method which generate the whole body of the interface.
66 * @return string with code for interface body in JAVA format
69 «wrapToDocumentation(formatDataForJavaDoc(type))»
70 «type.annotations.generateAnnotations»
71 public interface «type.name»
75 «generateInnerClasses»
87 def private generateAnnotations(List<AnnotationType> annotations) '''
88 «IF annotations !== null && !annotations.empty»
89 «FOR annotation : annotations»
90 «annotation.generateAnnotation»
96 * Template method which generates the interface name declaration.
98 * @return string with the code for the interface declaration in JAVA format
100 def private superInterfaces()
102 «IF (!type.implements.empty)»
104 «FOR type : type.implements SEPARATOR ","»
111 * Template method which generates inner classes inside this interface.
113 * @return string with the source code for inner classes in JAVA format
115 def private generateInnerClasses() '''
116 «IF !enclosedGeneratedTypes.empty»
117 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
118 «generateInnerClass(innerClass)»
124 * Template method which generates JAVA enum type.
126 * @return string with inner enum source code in JAVA format
128 def private generateEnums() '''
130 «FOR e : enums SEPARATOR "\n"»
131 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
132 «enumTemplate.generateAsInnerClass»
138 * Template method wich generates JAVA constants.
140 * @return string with constants in JAVA format
142 def private generateConstants() '''
145 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
153 * Template method which generates the declaration of the methods.
155 * @return string with the declaration of methods source code in JAVA format
157 def private generateMethods() '''
159 «FOR m : methods SEPARATOR "\n"»
161 «generateDefaultMethod(m)»
162 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
163 «generateAccessorMethod(m)»
171 def private generateDefaultMethod(MethodSignature method) {
172 if (method.name.isNonnullMethodName) {
173 generateNonnullMethod(method)
176 case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
181 def private generateMethod(MethodSignature method) '''
182 «method.comment.asJavadoc»
183 «method.annotations.generateAnnotations»
184 «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
187 def private generateAccessorMethod(MethodSignature method) '''
188 «val ret = method.returnType»
189 «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or " + asCode("null") + " if not present")»
190 «method.annotations.generateAnnotations»
191 «nullableType(ret)» «method.name»();
194 def private generateDefaultImplementedInterface() '''
195 @«Override.importedName»
196 default «Class.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
197 return «type.fullyQualifiedName».class;
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()