Comments of source code.
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / InterfaceTemplate.xtend
1 package org.opendaylight.yangtools.sal.java.api.generator
2
3 import java.util.List
4 import java.util.Map
5 import org.opendaylight.yangtools.binding.generator.util.TypeConstants
6 import org.opendaylight.yangtools.sal.binding.model.api.Constant
7 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
8 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
9 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
10 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature
11 import org.opendaylight.yangtools.sal.binding.model.api.Type
12 import java.util.LinkedHashMap
13 /**
14  * Template for generating JAVA interfaces. 
15  */
16 class InterfaceTemplate {
17     
18     /**
19      * Generated type which is transformed to interface JAVA file.
20      */
21     val GeneratedType genType
22     
23     /**
24      * Map of imports for this <code>genTO</code>.
25      */
26     val Map<String, String> imports
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         if (genType == null) {
56             throw new IllegalArgumentException("Generated type reference cannot be NULL!")
57         }
58         
59         this.genType = genType
60         imports = GeneratorUtil.createImports(genType)
61         consts = genType.constantDefinitions
62         methods = genType.methodDefinitions
63         enums = genType.enumerations
64         enclosedGeneratedTypes = genType.enclosedTypes
65     }
66     
67     /**
68      * Generates the source code for interface with package name, imports and interface body.
69      * 
70      * @return string with the code for the interface file in JAVA format
71      */
72     def String generate() {
73         val body = generateBody
74         val pkgAndImports = generatePkgAndImports
75         return pkgAndImports.toString + body.toString
76     }
77     
78     /**
79      * Template method which generate the whole body of the interface.
80      * 
81      * @return string with code for interface body in JAVA format
82      */
83     def private generateBody() '''
84         «genType.comment.generateComment»
85         «generateIfcDeclaration» {
86         
87             «generateInnerClasses»
88         
89             «generateEnums»
90         
91             «generateConstants»
92         
93             «generateMethods»
94         
95         }
96         
97     '''
98     
99     /**
100      * Template method which generates JAVA comment.
101      * 
102      * @param comment 
103      * string with the comment for whole JAVA interface
104      * @return string with comment in JAVA format
105      */
106     def private generateComment(String comment) '''
107         «IF comment != null && !comment.empty»
108             /*
109             «comment»
110             */
111         «ENDIF»
112     '''
113     
114     /**
115      * Template method which generates the interface name declaration.
116      * 
117      * @return string with the code for the interface declaration in JAVA format
118      */
119     def private generateIfcDeclaration() '''
120         public interface «genType.name»«
121         IF (!genType.implements.empty)»«
122             " extends "»«
123             FOR type : genType.implements SEPARATOR ", "»«
124                 type.resolveName»«
125             ENDFOR»«
126         ENDIF
127     »'''
128     
129     /**
130      * Template method which generates inner classes inside this interface.
131      * 
132      * @return string with the source code for inner classes in JAVA format
133      */
134     def private generateInnerClasses() '''
135         «IF !enclosedGeneratedTypes.empty»
136             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
137                 «IF (innerClass instanceof GeneratedTransferObject)»
138                     «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)»
139                     «classTemplate.generateAsInnerClass»
140                 «ENDIF»
141             «ENDFOR»
142         «ENDIF»
143     '''
144
145     /**
146      * Template method which generates JAVA enum type.
147      * 
148      * @return string with inner enum source code in JAVA format
149      */    
150     def private generateEnums() '''
151         «IF !enums.empty»
152             «FOR e : enums SEPARATOR "\n"»
153                 «val enumTemplate = new EnumTemplate(e)»
154                 «enumTemplate.generateAsInnerClass»
155             «ENDFOR»
156         «ENDIF»
157     '''
158     
159     /**
160      * Template method wich generates JAVA constants.
161      * 
162      * @return string with constants in JAVA format 
163      */    
164     def private generateConstants() '''
165         «IF !consts.empty»
166             «FOR c : consts»
167                 «IF c.name != TypeConstants.PATTERN_CONSTANT_NAME»
168                     public static final «c.type.resolveName» «c.name» = «c.value»;
169                 «ENDIF»
170             «ENDFOR»
171         «ENDIF»
172     '''
173
174     /**
175      * Template method which generates the declaration of the methods.
176      * 
177      * @return string with the declaration of methods source code in JAVA format 
178      */    
179     def private generateMethods() '''
180         «IF !methods.empty»
181             «FOR m : methods SEPARATOR "\n"»
182                 «m.comment.generateComment»
183                 «m.returnType.resolveName» «m.name»(«m.parameters.generateParameters»);
184             «ENDFOR»
185         «ENDIF»
186     '''
187     
188     /**
189      * Template method which generates method parameters with their types from <code>parameters</code>.
190      * 
191      * @param parameters
192      * list of parameter instances which are transformed to the method parameters
193      * @return string with the list of the method parameters with their types in JAVA format
194      */
195     def private generateParameters(List<MethodSignature.Parameter> parameters) '''«
196         IF !parameters.empty»«
197             FOR parameter : parameters SEPARATOR ", "»«
198                 parameter.type.resolveName» «parameter.name»«
199             ENDFOR»«
200         ENDIF
201     »'''
202     
203
204     /**
205      * Template method which generates the map of all the required imports for and imports 
206      * from extended type (and recursivelly so on).
207      * 
208      * @return map which maps type name to package name   
209      */
210     def private Map<String, String> resolveImports() {
211         val innerTypeImports = GeneratorUtil.createChildImports(genType)
212         val Map<String, String> resolvedImports = new LinkedHashMap
213         for (Map.Entry<String, String> entry : imports.entrySet() + innerTypeImports.entrySet) {
214             val typeName = entry.getKey();
215             val packageName = entry.getValue();
216             if (packageName != genType.packageName && packageName != genType.fullyQualifiedName) {
217                 resolvedImports.put(typeName, packageName);
218             }
219         }
220         return resolvedImports
221     }
222     
223     /**
224      * Template method which generate package name line and import lines.
225      * 
226      * @result string with package and import lines in JAVA format
227      */    
228     def private generatePkgAndImports() '''
229         package «genType.packageName»;
230         
231         
232         «IF !imports.empty»
233             «FOR entry : resolveImports.entrySet»
234                 import «entry.value».«entry.key»;
235             «ENDFOR»
236         «ENDIF»
237         
238     '''    
239     
240     /**
241      * Adds package to imports if it is necessary and returns necessary type name (with or without package name)
242      *  
243      * @param type JAVA <code>Type</code> 
244      * @return string with the type name (with or without package name)
245      */      
246     def private resolveName(Type type) {
247         GeneratorUtil.putTypeIntoImports(genType, type, imports);
248         GeneratorUtil.getExplicitType(genType, type, imports)
249     }
250     
251 }