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