1 package org.opendaylight.yangtools.sal.java.api.generator
\r
3 import java.util.List
\r
5 import org.opendaylight.yangtools.binding.generator.util.TypeConstants
\r
6 import org.opendaylight.yangtools.sal.binding.model.api.Constant
\r
7 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
\r
8 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
\r
9 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
\r
10 import org.opendaylight.yangtools.sal.binding.model.api.Type
\r
11 import org.opendaylight.yangtools.binding.generator.util.Types
\r
12 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
\r
16 * Template for generating JAVA class.
\r
18 class ClassTemplate extends BaseTemplate {
\r
20 protected val List<GeneratedProperty> properties
\r
21 protected val List<GeneratedProperty> finalProperties
\r
22 protected val List<GeneratedProperty> parentProperties
\r
23 protected val Iterable<GeneratedProperty> allProperties;
\r
26 * List of enumeration which are generated as JAVA enum type.
\r
28 protected val List<Enumeration> enums
\r
31 * List of constant instances which are generated as JAVA public static final attributes.
\r
33 protected val List<Constant> consts
\r
36 * List of generated types which are enclosed inside <code>genType</code>
\r
38 protected val List<GeneratedType> enclosedGeneratedTypes;
\r
41 protected val GeneratedTransferObject genTO;
\r
44 * Creates instance of this class with concrete <code>genType</code>.
\r
46 * @param genType generated transfer object which will be transformed to JAVA class source code
\r
48 new(GeneratedTransferObject genType) {
\r
50 this.genTO = genType
\r
51 this.properties = genType.properties
\r
52 this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties)
\r
53 this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
\r
54 this.allProperties = properties + parentProperties
\r
55 this.enums = genType.enumerations
\r
56 this.consts = genType.constantDefinitions
\r
57 this.enclosedGeneratedTypes = genType.enclosedTypes
\r
65 * Generates JAVA class source code (class body only).
\r
67 * @return string with JAVA class body source code
\r
69 def generateAsInnerClass() {
\r
70 return generateBody(true)
\r
75 override protected body() {
\r
76 generateBody(false);
\r
80 * Template method which generates class body.
\r
82 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
83 * @return string with class source code in JAVA format
\r
85 def protected generateBody(boolean isInnerClass) '''
\r
86 «type.comment.asJavadoc»
\r
87 «generateClassDeclaration(isInnerClass)» {
\r
88 «innerClassesDeclarations»
\r
90 «constantsDeclarations»
\r
93 «FOR field : properties SEPARATOR "\n"»
\r
94 «field.getterMethod»
\r
95 «IF !field.readOnly»
\r
96 «field.setterMethod»
\r
108 * Template method which generates inner classes inside this interface.
\r
110 * @return string with the source code for inner classes in JAVA format
\r
112 def protected innerClassesDeclarations() '''
\r
113 «IF !enclosedGeneratedTypes.empty»
\r
114 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
\r
115 «IF (innerClass instanceof GeneratedTransferObject)»
\r
116 «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)»
\r
117 «classTemplate.generateAsInnerClass»
\r
125 def protected constructors() '''
\r
126 «allValuesConstructor»
\r
127 «IF !allProperties.empty»
\r
130 «IF properties.empty && !parentProperties.empty »
\r
131 «parentConstructor»
\r
135 def protected allValuesConstructor() '''
\r
136 public «type.name»(«allProperties.asArgumentsDeclaration») {
\r
137 «IF false == parentProperties.empty»
\r
138 super(«parentProperties.asArguments»);
\r
140 «FOR p : properties»
\r
141 this.«p.fieldName» = «p.fieldName»;
\r
147 def protected copyConstructor() '''
\r
149 * Creates a copy from Source Object.
\r
151 * @param source Source object
\r
153 public «type.name»(«type.name» source) {
\r
154 «IF false == parentProperties.empty»
\r
157 «FOR p : properties»
\r
158 this.«p.fieldName» = source.«p.fieldName»;
\r
163 def protected parentConstructor() '''
\r
165 * Creates a new instance from «genTO.superType.importedName»
\r
167 * @param source Source object
\r
169 public «type.name»(«genTO.superType.importedName» source) {
\r
177 * Template method which generates JAVA class declaration.
\r
179 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
180 * @return string with class declaration in JAVA format
\r
182 def protected generateClassDeclaration(boolean isInnerClass) '''
\r
184 IF (isInnerClass)»«
\r
186 ELSEIF (type.abstract)»«
\r
190 ENDIF»class «type.name»«
\r
191 IF (genTO.superType != null)»«
\r
192 " extends "»«genTO.superType.importedName»«
\r
194 «IF (!type.implements.empty)»«
\r
196 FOR type : type.implements SEPARATOR ", "»«
\r
197 type.importedName»«
\r
203 * Template method which generates JAVA enum type.
\r
205 * @return string with inner enum source code in JAVA format
\r
207 def protected enumDeclarations() '''
\r
209 «FOR e : enums SEPARATOR "\n"»
\r
210 «val enumTemplate = new EnumTemplate(e)»
\r
211 «enumTemplate.generateAsInnerClass»
\r
217 * Template method wich generates JAVA constants.
\r
219 * @return string with constants in JAVA format
\r
221 def protected constantsDeclarations() '''
\r
224 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
\r
225 «val cValue = c.value»
\r
226 «IF cValue instanceof List<?>»
\r
227 «val cValues = cValue as List<?>»
\r
228 private static final List<Pattern> «Constants.MEMBER_PATTERN_LIST» = new ArrayList<Pattern>();
\r
229 public static final List<String> «TypeConstants.PATTERN_CONSTANT_NAME» = Arrays.asList(«
\r
230 FOR v : cValues SEPARATOR ", "»«
\r
231 IF v instanceof String»"«
\r
236 «generateStaticInicializationBlock»
\r
239 public static final «c.type.importedName» «c.name» = «c.value»;
\r
246 * Template method which generates JAVA static initialization block.
\r
248 * @return string with static initialization block in JAVA format
\r
250 def protected generateStaticInicializationBlock() '''
\r
252 for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») {
\r
253 «Constants.MEMBER_PATTERN_LIST».add(Pattern.compile(regEx));
\r
259 * Template method which generates JAVA class attributes.
\r
261 * @return string with the class attributes in JAVA format
\r
263 def protected generateFields() '''
\r
264 «IF !properties.empty»
\r
265 «FOR f : properties»
\r
266 «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
\r
273 * Template method which generates the method <code>hashCode()</code>.
\r
275 * @return string with the <code>hashCode()</code> method definition in JAVA format
\r
277 def protected generateHashCode() '''
\r
278 «IF !genTO.hashCodeIdentifiers.empty»
\r
280 public int hashCode() {
\r
281 final int prime = 31;
\r
283 «FOR property : genTO.hashCodeIdentifiers»
\r
284 result = prime * result + ((«property.fieldName» == null) ? 0 : «property.fieldName».hashCode());
\r
292 * Template method which generates the method <code>equals()</code>.
\r
294 * @return string with the <code>equals()</code> method definition in JAVA format
\r
296 def protected generateEquals() '''
\r
297 «IF !genTO.equalsIdentifiers.empty»
\r
299 public boolean equals(java.lang.Object obj) {
\r
306 if (getClass() != obj.getClass()) {
\r
309 «type.name» other = («type.name») obj;
\r
310 «FOR property : genTO.equalsIdentifiers»
\r
311 «val fieldName = property.fieldName»
\r
312 if («fieldName» == null) {
\r
313 if (other.«fieldName» != null) {
\r
316 } else if(!«fieldName».equals(other.«fieldName»)) {
\r
326 * Template method which generates the method <code>toString()</code>.
\r
328 * @return string with the <code>toString()</code> method definition in JAVA format
\r
330 def protected generateToString() '''
\r
331 «IF !genTO.toStringIdentifiers.empty»
\r
333 public String toString() {
\r
334 StringBuilder builder = new StringBuilder();
\r
335 «val properties = genTO.toStringIdentifiers»
\r
336 builder.append("«type.name» [«properties.get(0).fieldName»=");
\r
337 builder.append(«properties.get(0).fieldName»);
\r
338 «FOR i : 1..<genTO.toStringIdentifiers.size»
\r
339 builder.append(", «properties.get(i).fieldName»=");
\r
340 builder.append(«properties.get(i).fieldName»);
\r
342 builder.append("]");
\r
343 return builder.toString();
\r