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»
147 * Template method which generates JAVA enum type.
149 * @return string with inner enum source code in JAVA format
151 def private generateEnums() '''
153 «FOR e : enums SEPARATOR "\n"»
154 «val enumTemplate = new EnumTemplate(e)»
155 «enumTemplate.generateAsInnerClass»
161 * Template method wich generates JAVA constants.
163 * @return string with constants in JAVA format
165 def private generateConstants() '''
168 «IF c.name != TypeConstants.PATTERN_CONSTANT_NAME»
169 public static final «c.type.resolveName» «c.name» = «c.value»;
176 * Template method which generates the declaration of the methods.
178 * @return string with the declaration of methods source code in JAVA format
180 def private generateMethods() '''
182 «FOR m : methods SEPARATOR "\n"»
183 «m.comment.generateComment»
184 «m.returnType.resolveName» «m.name»(«m.parameters.generateParameters»);
190 * Template method which generates method parameters with their types from <code>parameters</code>.
193 * list of parameter instances which are transformed to the method parameters
194 * @return string with the list of the method parameters with their types in JAVA format
196 def private generateParameters(List<MethodSignature.Parameter> parameters) '''«
197 IF !parameters.empty»«
198 FOR parameter : parameters SEPARATOR ", "»«
199 parameter.type.resolveName» «parameter.name»«
206 * Template method which generates the map of all the required imports for and imports
207 * from extended type (and recursivelly so on).
209 * @return map which maps type name to package name
211 def private Map<String, String> resolveImports() {
212 val innerTypeImports = GeneratorUtil.createChildImports(genType)
213 val Map<String, String> resolvedImports = new LinkedHashMap
214 for (Map.Entry<String, String> entry : imports.entrySet() + innerTypeImports.entrySet) {
215 val typeName = entry.getKey();
216 val packageName = entry.getValue();
217 if (packageName != genType.packageName && packageName != genType.fullyQualifiedName) {
218 resolvedImports.put(typeName, packageName);
221 return resolvedImports
225 * Template method which generate package name line and import lines.
227 * @result string with package and import lines in JAVA format
229 def private generatePkgAndImports() '''
230 package «genType.packageName»;
234 «FOR entry : resolveImports.entrySet»
235 import «entry.value».«entry.key»;
242 * Adds package to imports if it is necessary and returns necessary type name (with or without package name)
244 * @param type JAVA <code>Type</code>
245 * @return string with the type name (with or without package name)
247 def private resolveName(Type type) {
248 GeneratorUtil.putTypeIntoImports(genType, type, imports);
249 GeneratorUtil.getExplicitType(genType, type, imports)