1 package org.opendaylight.yangtools.sal.java.api.generator
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
14 * Template for generating JAVA interfaces.
16 class InterfaceTemplate {
19 * Generated type which is transformed to interface JAVA file.
21 val GeneratedType genType
24 * Map of imports for this <code>genTO</code>.
26 val Map<String, String> imports
29 * List of constant instances which are generated as JAVA public static final attributes.
31 val List<Constant> consts
34 * List of method signatures which are generated as method declarations.
36 val List<MethodSignature> methods
39 * List of enumeration which are generated as JAVA enum type.
41 val List<Enumeration> enums
44 * List of generated types which are enclosed inside <code>genType</code>
46 val List<GeneratedType> enclosedGeneratedTypes
49 * Creates the instance of this class which is used for generating the interface file source
50 * code from <code>genType</code>.
52 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
54 new(GeneratedType genType) {
55 if (genType == null) {
56 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
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
68 * Generates the source code for interface with package name, imports and interface body.
70 * @return string with the code for the interface file in JAVA format
72 def String generate() {
73 val body = generateBody
74 val pkgAndImports = generatePkgAndImports
75 return pkgAndImports.toString + body.toString
79 * Template method which generate the whole body of the interface.
81 * @return string with code for interface body in JAVA format
83 def private generateBody() '''
84 «genType.comment.generateComment»
85 «generateIfcDeclaration» {
87 «generateInnerClasses»
100 * Template method which generates JAVA comment.
103 * string with the comment for whole JAVA interface
104 * @return string with comment in JAVA format
106 def private generateComment(String comment) '''
107 «IF comment != null && !comment.empty»
115 * Template method which generates the interface name declaration.
117 * @return string with the code for the interface declaration in JAVA format
119 def private generateIfcDeclaration() '''
120 public interface «genType.name»«
121 IF (!genType.implements.empty)»«
123 FOR type : genType.implements SEPARATOR ", "»«
130 * Template method which generates inner classes inside this interface.
132 * @return string with the source code for inner classes in JAVA format
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»
146 * Template method which generates JAVA enum type.
148 * @return string with inner enum source code in JAVA format
150 def private generateEnums() '''
152 «FOR e : enums SEPARATOR "\n"»
153 «val enumTemplate = new EnumTemplate(e)»
154 «enumTemplate.generateAsInnerClass»
160 * Template method wich generates JAVA constants.
162 * @return string with constants in JAVA format
164 def private generateConstants() '''
167 «IF c.name != TypeConstants.PATTERN_CONSTANT_NAME»
168 public static final «c.type.resolveName» «c.name» = «c.value»;
175 * Template method which generates the declaration of the methods.
177 * @return string with the declaration of methods source code in JAVA format
179 def private generateMethods() '''
181 «FOR m : methods SEPARATOR "\n"»
182 «m.comment.generateComment»
183 «m.returnType.resolveName» «m.name»(«m.parameters.generateParameters»);
189 * Template method which generates method parameters with their types from <code>parameters</code>.
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
195 def private generateParameters(List<MethodSignature.Parameter> parameters) '''«
196 IF !parameters.empty»«
197 FOR parameter : parameters SEPARATOR ", "»«
198 parameter.type.resolveName» «parameter.name»«
205 * Template method which generates the map of all the required imports for and imports
206 * from extended type (and recursivelly so on).
208 * @return map which maps type name to package name
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);
220 return resolvedImports
224 * Template method which generate package name line and import lines.
226 * @result string with package and import lines in JAVA format
228 def private generatePkgAndImports() '''
229 package «genType.packageName»;
233 «FOR entry : resolveImports.entrySet»
234 import «entry.value».«entry.key»;
241 * Adds package to imports if it is necessary and returns necessary type name (with or without package name)
243 * @param type JAVA <code>Type</code>
244 * @return string with the type name (with or without package name)
246 def private resolveName(Type type) {
247 GeneratorUtil.putTypeIntoImports(genType, type, imports);
248 GeneratorUtil.getExplicitType(genType, type, imports)