1 package org.opendaylight.yangtools.sal.java.api.generator
3 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
4 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
6 import org.opendaylight.yangtools.sal.binding.model.api.Type
7 import org.opendaylight.yangtools.binding.generator.util.Types
9 abstract class BaseTemplate {
12 protected val GeneratedType type;
13 protected val Map<String,String> importMap;
15 new(GeneratedType _type) {
17 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
20 this.importMap = GeneratorUtil.createImports(type)
23 def packageDefinition () '''package «type.packageName»;'''
26 final public def generate() {
35 protected def imports() '''
37 «FOR entry : importMap.entrySet»
38 import «entry.value».«entry.key»;
44 protected abstract def CharSequence body();
48 final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
51 * Template method which generates the getter method for <code>field</code>
54 * generated property with data about field which is generated as the getter method
55 * @return string with the getter method source code in JAVA format
57 final protected def getterMethod(GeneratedProperty field) {
58 val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
60 public «field.returnType.importedName» «prefix»«field.name.toFirstUpper»() {
61 return «field.fieldName»;
67 * Template method which generates the setter method for <code>field</code>
70 * generated property with data about field which is generated as the setter method
71 * @return string with the setter method source code in JAVA format
73 final protected def setterMethod(GeneratedProperty field) '''
74 «val returnType = field.returnType.importedName»
75 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
76 this.«field.fieldName» = value;
81 final protected def importedName(Type intype) {
82 GeneratorUtil.putTypeIntoImports(type, intype, importMap);
83 GeneratorUtil.getExplicitType(type, intype, importMap)
86 final protected def importedName(Class cls) {
87 importedName(Types.typeForClass(cls))
91 * Template method which generates method parameters with their types from <code>parameters</code>.
94 * group of generated property instances which are transformed to the method parameters
95 * @return string with the list of the method parameters with their types in JAVA format
97 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters)
98 '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
101 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
104 * group of generated property instances which are transformed to the sequence of parameter names
105 * @return string with the list of the parameter names of the <code>parameters</code>
107 def final protected asArguments(Iterable<GeneratedProperty> parameters)
108 '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.fieldName»«ENDFOR»«ENDIF»'''