Tag generated QNAME field with @NonNull
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / InterfaceTemplate.xtend
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator
9
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
13
14 import java.util.List
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
23
24 /**
25  * Template for generating JAVA interfaces.
26  */
27 class InterfaceTemplate extends BaseTemplate {
28     /**
29      * List of constant instances which are generated as JAVA public static final attributes.
30      */
31     val List<Constant> consts
32
33     /**
34      * List of method signatures which are generated as method declarations.
35      */
36     val List<MethodSignature> methods
37
38     /**
39      * List of enumeration which are generated as JAVA enum type.
40      */
41     val List<Enumeration> enums
42
43     /**
44      * List of generated types which are enclosed inside <code>genType</code>
45      */
46     val List<GeneratedType> enclosedGeneratedTypes
47
48     /**
49      * Creates the instance of this class which is used for generating the interface file source
50      * code from <code>genType</code>.
51      *
52      * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
53      */
54     new(GeneratedType genType) {
55         super(genType)
56         if (genType === null) {
57             throw new IllegalArgumentException("Generated type reference cannot be NULL!")
58         }
59
60         consts = genType.constantDefinitions
61         methods = genType.methodDefinitions
62         enums = genType.enumerations
63         enclosedGeneratedTypes = genType.enclosedTypes
64     }
65
66     /**
67      * Template method which generate the whole body of the interface.
68      *
69      * @return string with code for interface body in JAVA format
70      */
71     override body() '''
72         «wrapToDocumentation(formatDataForJavaDoc(type))»
73         «type.annotations.generateAnnotations»
74         public interface «type.name»
75             «superInterfaces»
76         {
77
78             «generateInnerClasses»
79
80             «generateEnums»
81
82             «generateConstants»
83
84             «generateMethods»
85
86         }
87
88     '''
89
90
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»
96                 (
97                 «FOR param : annotation.parameters SEPARATOR ","»
98                     «param.name»=«param.value»
99                 «ENDFOR»
100                 )
101                 «ENDIF»
102             «ENDFOR»
103         «ENDIF»
104     '''
105
106     /**
107      * Template method which generates the interface name declaration.
108      *
109      * @return string with the code for the interface declaration in JAVA format
110      */
111     def private superInterfaces()
112     '''
113     «IF (!type.implements.empty)»
114          extends
115          «FOR type : type.implements SEPARATOR ","»
116              «type.importedName»
117          «ENDFOR»
118      « ENDIF»
119      '''
120
121     /**
122      * Template method which generates inner classes inside this interface.
123      *
124      * @return string with the source code for inner classes in JAVA format
125      */
126     def private generateInnerClasses() '''
127         «IF !enclosedGeneratedTypes.empty»
128             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
129                 «generateInnerClass(innerClass)»
130             «ENDFOR»
131         «ENDIF»
132     '''
133
134     /**
135      * Template method which generates JAVA enum type.
136      *
137      * @return string with inner enum source code in JAVA format
138      */
139     def private generateEnums() '''
140         «IF !enums.empty»
141             «FOR e : enums SEPARATOR "\n"»
142                 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
143                 «enumTemplate.generateAsInnerClass»
144             «ENDFOR»
145         «ENDIF»
146     '''
147
148     /**
149      * Template method wich generates JAVA constants.
150      *
151      * @return string with constants in JAVA format
152      */
153     def private generateConstants() '''
154         «IF !consts.empty»
155             «FOR c : consts»
156                 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
157                     «emitConstant(c)»
158                 «ENDIF»
159             «ENDFOR»
160         «ENDIF»
161     '''
162
163     /**
164      * Template method which generates the declaration of the methods.
165      *
166      * @return string with the declaration of methods source code in JAVA format
167      */
168     def private generateMethods() '''
169         «IF !methods.empty»
170             «FOR m : methods SEPARATOR "\n"»
171                 «IF m.isDefault»
172                     «generateDefaultMethod(m)»
173                 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
174                     «generateAccessorMethod(m)»
175                 «ELSE»
176                     «generateMethod(m)»
177                 «ENDIF»
178             «ENDFOR»
179         «ENDIF»
180     '''
181
182     def private generateDefaultMethod(MethodSignature method) {
183         if (method.name.isNonnullMethodName) {
184             return generateNonnullMethod(method)
185         }
186     }
187
188     def private generateMethod(MethodSignature method) '''
189         «method.comment.asJavadoc»
190         «method.annotations.generateAnnotations»
191         «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
192     '''
193
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»();
199     '''
200
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)»());
208         }
209     '''
210
211     def private String nullableType(Type type) {
212         if (type.isObject) {
213             return type.importedNullable
214         }
215         return type.importedName
216     }
217
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()
221     }
222 }