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.generateComment»
\r
87 «generateClassDeclaration(isInnerClass)» {
\r
88 «innerClassesDeclarations»
\r
92 «constantsDeclarations»
\r
98 «FOR field : properties SEPARATOR "\n"»
\r
99 «field.getterMethod»
\r
100 «IF !field.readOnly»
\r
102 «field.setterMethod»
\r
117 * Template method which generates inner classes inside this interface.
\r
119 * @return string with the source code for inner classes in JAVA format
\r
121 def protected innerClassesDeclarations() '''
\r
122 «IF !enclosedGeneratedTypes.empty»
\r
123 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
\r
124 «IF (innerClass instanceof GeneratedTransferObject)»
\r
125 «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)»
\r
126 «classTemplate.generateAsInnerClass»
\r
134 def protected constructors() '''
\r
135 «allValuesConstructor»
\r
136 «IF !allProperties.empty»
\r
139 «IF properties.empty && !parentProperties.empty »
\r
140 «parentConstructor»
\r
144 def protected allValuesConstructor() '''
\r
145 public «type.name»(«allProperties.asArgumentsDeclaration») {
\r
146 «IF false == parentProperties.empty»
\r
147 super(«parentProperties.asArguments»);
\r
149 «FOR p : properties»
\r
150 this.«p.fieldName» = «p.fieldName»;
\r
156 def protected copyConstructor() '''
\r
158 * Creates a copy from Source Object.
\r
160 * @param source Source object
\r
162 public «type.name»(«type.name» source) {
\r
163 «IF false == parentProperties.empty»
\r
166 «FOR p : properties»
\r
167 this.«p.fieldName» = source.«p.fieldName»;
\r
172 def protected parentConstructor() '''
\r
174 * Creates a new instance from «genTO.extends.importedName»
\r
176 * @param source Source object
\r
178 public «type.name»(«genTO.extends.importedName» source) {
\r
184 * Template method which generates JAVA comments.
\r
186 * @param string with the comment for whole JAVA class
\r
187 * @return string with comment in JAVA format
\r
189 def protected generateComment(String comment) '''
\r
190 «IF comment != null && !comment.empty»
\r
198 * Template method which generates JAVA class declaration.
\r
200 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
201 * @return string with class declaration in JAVA format
\r
203 def protected generateClassDeclaration(boolean isInnerClass) '''
\r
205 IF (isInnerClass)»«
\r
207 ELSEIF (type.abstract)»«
\r
211 ENDIF»class «type.name»«
\r
212 IF (genTO.extends != null)»«
\r
213 " extends "»«genTO.extends.importedName»«
\r
215 IF (!type.implements.empty)»«
\r
217 FOR type : type.implements SEPARATOR ", "»«
\r
218 type.importedName»«
\r
224 * Template method which generates JAVA enum type.
\r
226 * @return string with inner enum source code in JAVA format
\r
228 def protected enumDeclarations() '''
\r
230 «FOR e : enums SEPARATOR "\n"»
\r
231 «val enumTemplate = new EnumTemplate(e)»
\r
232 «enumTemplate.generateAsInnerClass»
\r
238 * Template method wich generates JAVA constants.
\r
240 * @return string with constants in JAVA format
\r
242 def protected constantsDeclarations() '''
\r
245 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
\r
246 «val cValue = c.value»
\r
247 «IF cValue instanceof List<?>»
\r
248 «val cValues = cValue as List<?>»
\r
249 private static final List<Pattern> «Constants.MEMBER_PATTERN_LIST» = new ArrayList<Pattern>();
\r
250 public static final List<String> «TypeConstants.PATTERN_CONSTANT_NAME» = Arrays.asList(«
\r
251 FOR v : cValues SEPARATOR ", "»«
\r
252 IF v instanceof String»"«
\r
257 «generateStaticInicializationBlock»
\r
260 public static final «c.type.importedName» «c.name» = «c.value»;
\r
267 * Template method which generates JAVA static initialization block.
\r
269 * @return string with static initialization block in JAVA format
\r
271 def protected generateStaticInicializationBlock() '''
\r
273 for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») {
\r
274 «Constants.MEMBER_PATTERN_LIST».add(Pattern.compile(regEx));
\r
280 * Template method which generates JAVA class attributes.
\r
282 * @return string with the class attributes in JAVA format
\r
284 def protected generateFields() '''
\r
285 «IF !properties.empty»
\r
286 «FOR f : properties»
\r
287 «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
\r
294 * Template method which generates the method <code>hashCode()</code>.
\r
296 * @return string with the <code>hashCode()</code> method definition in JAVA format
\r
298 def protected generateHashCode() '''
\r
299 «IF !genTO.hashCodeIdentifiers.empty»
\r
301 public int hashCode() {
\r
302 final int prime = 31;
\r
304 «FOR property : genTO.hashCodeIdentifiers»
\r
305 result = prime * result + ((«property.fieldName» == null) ? 0 : «property.fieldName».hashCode());
\r
313 * Template method which generates the method <code>equals()</code>.
\r
315 * @return string with the <code>equals()</code> method definition in JAVA format
\r
317 def protected generateEquals() '''
\r
318 «IF !genTO.equalsIdentifiers.empty»
\r
320 public boolean equals(java.lang.Object obj) {
\r
327 if (getClass() != obj.getClass()) {
\r
330 «type.name» other = («type.name») obj;
\r
331 «FOR property : genTO.equalsIdentifiers»
\r
332 «val fieldName = property.fieldName»
\r
333 if («fieldName» == null) {
\r
334 if (other.«fieldName» != null) {
\r
337 } else if(!«fieldName».equals(other.«fieldName»)) {
\r
347 * Template method which generates the method <code>toString()</code>.
\r
349 * @return string with the <code>toString()</code> method definition in JAVA format
\r
351 def protected generateToString() '''
\r
352 «IF !genTO.toStringIdentifiers.empty»
\r
354 public String toString() {
\r
355 StringBuilder builder = new StringBuilder();
\r
356 «val properties = genTO.toStringIdentifiers»
\r
357 builder.append("«type.name» [«properties.get(0).fieldName»=");
\r
358 builder.append(«properties.get(0).fieldName»);
\r
359 «FOR i : 1..<genTO.toStringIdentifiers.size»
\r
360 builder.append(", «properties.get(i).fieldName»=");
\r
361 builder.append(«properties.get(i).fieldName»);
\r
363 builder.append("]");
\r
364 return builder.toString();
\r