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
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
175 * Template method which generates JAVA comments.
\r
177 * @param string with the comment for whole JAVA class
\r
178 * @return string with comment in JAVA format
\r
180 def protected generateComment(String comment) '''
\r
181 «IF comment != null && !comment.empty»
\r
189 * Template method which generates JAVA class declaration.
\r
191 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
192 * @return string with class declaration in JAVA format
\r
194 def protected generateClassDeclaration(boolean isInnerClass) '''
\r
196 IF (isInnerClass)»«
\r
198 ELSEIF (type.abstract)»«
\r
202 ENDIF»class «type.name»«
\r
203 IF (genTO.superType != null)»«
\r
204 " extends "»«genTO.superType.importedName»«
\r
206 IF (!type.implements.empty)»«
\r
208 FOR type : type.implements SEPARATOR ", "»«
\r
209 type.importedName»«
\r
215 * Template method which generates JAVA enum type.
\r
217 * @return string with inner enum source code in JAVA format
\r
219 def protected enumDeclarations() '''
\r
221 «FOR e : enums SEPARATOR "\n"»
\r
222 «val enumTemplate = new EnumTemplate(e)»
\r
223 «enumTemplate.generateAsInnerClass»
\r
229 * Template method wich generates JAVA constants.
\r
231 * @return string with constants in JAVA format
\r
233 def protected constantsDeclarations() '''
\r
236 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
\r
237 «val cValue = c.value»
\r
238 «IF cValue instanceof List<?>»
\r
239 «val cValues = cValue as List<?>»
\r
240 private static final List<Pattern> «Constants.MEMBER_PATTERN_LIST» = new ArrayList<Pattern>();
\r
241 public static final List<String> «TypeConstants.PATTERN_CONSTANT_NAME» = Arrays.asList(«
\r
242 FOR v : cValues SEPARATOR ", "»«
\r
243 IF v instanceof String»"«
\r
248 «generateStaticInicializationBlock»
\r
251 public static final «c.type.importedName» «c.name» = «c.value»;
\r
258 * Template method which generates JAVA static initialization block.
\r
260 * @return string with static initialization block in JAVA format
\r
262 def protected generateStaticInicializationBlock() '''
\r
264 for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») {
\r
265 «Constants.MEMBER_PATTERN_LIST».add(Pattern.compile(regEx));
\r
271 * Template method which generates JAVA class attributes.
\r
273 * @return string with the class attributes in JAVA format
\r
275 def protected generateFields() '''
\r
276 «IF !properties.empty»
\r
277 «FOR f : properties»
\r
278 «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
\r
285 * Template method which generates the method <code>hashCode()</code>.
\r
287 * @return string with the <code>hashCode()</code> method definition in JAVA format
\r
289 def protected generateHashCode() '''
\r
290 «IF !genTO.hashCodeIdentifiers.empty»
\r
292 public int hashCode() {
\r
293 final int prime = 31;
\r
295 «FOR property : genTO.hashCodeIdentifiers»
\r
296 result = prime * result + ((«property.fieldName» == null) ? 0 : «property.fieldName».hashCode());
\r
304 * Template method which generates the method <code>equals()</code>.
\r
306 * @return string with the <code>equals()</code> method definition in JAVA format
\r
308 def protected generateEquals() '''
\r
309 «IF !genTO.equalsIdentifiers.empty»
\r
311 public boolean equals(java.lang.Object obj) {
\r
318 if (getClass() != obj.getClass()) {
\r
321 «type.name» other = («type.name») obj;
\r
322 «FOR property : genTO.equalsIdentifiers»
\r
323 «val fieldName = property.fieldName»
\r
324 if («fieldName» == null) {
\r
325 if (other.«fieldName» != null) {
\r
328 } else if(!«fieldName».equals(other.«fieldName»)) {
\r
338 * Template method which generates the method <code>toString()</code>.
\r
340 * @return string with the <code>toString()</code> method definition in JAVA format
\r
342 def protected generateToString() '''
\r
343 «IF !genTO.toStringIdentifiers.empty»
\r
345 public String toString() {
\r
346 StringBuilder builder = new StringBuilder();
\r
347 «val properties = genTO.toStringIdentifiers»
\r
348 builder.append("«type.name» [«properties.get(0).fieldName»=");
\r
349 builder.append(«properties.get(0).fieldName»);
\r
350 «FOR i : 1..<genTO.toStringIdentifiers.size»
\r
351 builder.append(", «properties.get(i).fieldName»=");
\r
352 builder.append(«properties.get(i).fieldName»);
\r
354 builder.append("]");
\r
355 return builder.toString();
\r