Move getter method naming to BindingMapping
[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.isGetterMethodName
11
12 import java.util.List
13 import org.opendaylight.mdsal.binding.model.api.AnnotationType
14 import org.opendaylight.mdsal.binding.model.api.Constant
15 import org.opendaylight.mdsal.binding.model.api.Enumeration
16 import org.opendaylight.mdsal.binding.model.api.GeneratedType
17 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
18 import org.opendaylight.mdsal.binding.model.api.MethodSignature
19 import org.opendaylight.mdsal.binding.model.api.Type
20 import org.opendaylight.mdsal.binding.model.util.TypeConstants
21
22 /**
23  * Template for generating JAVA interfaces.
24  */
25 class InterfaceTemplate extends BaseTemplate {
26     static val JavaTypeName NULLABLE = JavaTypeName.create("org.eclipse.jdt.annotation", "Nullable")
27
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                 «val accessor = m.name.isGetterMethodName»
172                 «val ret = m.returnType»
173                 «IF accessor»
174                     «formatDataForJavaDoc(m, "@return " + asCode(ret.fullyQualifiedName) + " "
175                     + asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
176                 «ELSE»
177                     «m.comment.asJavadoc»
178                 «ENDIF»
179                 «m.annotations.generateAnnotations»
180                 «nullableType(ret, accessor)» «m.name»(«m.parameters.generateParameters»);
181             «ENDFOR»
182         «ENDIF»
183     '''
184
185     def private String nullableType(Type type, boolean accessor) {
186         if (accessor && type.isObject) {
187             return importedName(type, NULLABLE.importedName)
188         }
189         return type.importedName
190     }
191
192     def private static boolean isObject(Type type) {
193         // The return type has a package, so it's not a primitive type
194         return !type.getPackageName().isEmpty()
195     }
196 }