1 package org.opendaylight.yangtools.sal.java.api.generator
\r
3 import java.util.List
\r
4 import org.opendaylight.yangtools.binding.generator.util.TypeConstants
\r
5 import org.opendaylight.yangtools.sal.binding.model.api.Constant
\r
6 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
\r
7 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
\r
8 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
\r
9 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
\r
13 * Template for generating JAVA class.
\r
15 class ClassTemplate extends BaseTemplate {
\r
17 protected val List<GeneratedProperty> properties
\r
18 protected val List<GeneratedProperty> finalProperties
\r
19 protected val List<GeneratedProperty> parentProperties
\r
20 protected val Iterable<GeneratedProperty> allProperties;
\r
23 * List of enumeration which are generated as JAVA enum type.
\r
25 protected val List<Enumeration> enums
\r
28 * List of constant instances which are generated as JAVA public static final attributes.
\r
30 protected val List<Constant> consts
\r
33 * List of generated types which are enclosed inside <code>genType</code>
\r
35 protected val List<GeneratedType> enclosedGeneratedTypes;
\r
38 protected val GeneratedTransferObject genTO;
\r
41 * Creates instance of this class with concrete <code>genType</code>.
\r
43 * @param genType generated transfer object which will be transformed to JAVA class source code
\r
45 new(GeneratedTransferObject genType) {
\r
47 this.genTO = genType
\r
48 this.properties = genType.properties
\r
49 this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties)
\r
50 this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
\r
51 this.allProperties = properties + parentProperties
\r
52 this.enums = genType.enumerations
\r
53 this.consts = genType.constantDefinitions
\r
54 this.enclosedGeneratedTypes = genType.enclosedTypes
\r
62 * Generates JAVA class source code (class body only).
\r
64 * @return string with JAVA class body source code
\r
66 def generateAsInnerClass() {
\r
67 return generateBody(true)
\r
72 override protected body() {
\r
73 generateBody(false);
\r
77 * Template method which generates class body.
\r
79 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
80 * @return string with class source code in JAVA format
\r
82 def protected generateBody(boolean isInnerClass) '''
\r
83 «type.comment.asJavadoc»
\r
84 «generateClassDeclaration(isInnerClass)» {
\r
85 «innerClassesDeclarations»
\r
87 «constantsDeclarations»
\r
90 «FOR field : properties SEPARATOR "\n"»
\r
91 «field.getterMethod»
\r
92 «IF !field.readOnly»
\r
93 «field.setterMethod»
\r
105 * Template method which generates inner classes inside this interface.
\r
107 * @return string with the source code for inner classes in JAVA format
\r
109 def protected innerClassesDeclarations() '''
\r
110 «IF !enclosedGeneratedTypes.empty»
\r
111 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
\r
112 «IF (innerClass instanceof GeneratedTransferObject)»
\r
113 «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)»
\r
114 «classTemplate.generateAsInnerClass»
\r
122 def protected constructors() '''
\r
123 «allValuesConstructor»
\r
124 «IF !allProperties.empty»
\r
127 «IF properties.empty && !parentProperties.empty »
\r
128 «parentConstructor»
\r
132 def protected allValuesConstructor() '''
\r
133 public «type.name»(«allProperties.asArgumentsDeclaration») {
\r
134 «IF false == parentProperties.empty»
\r
135 super(«parentProperties.asArguments»);
\r
137 «FOR p : properties»
\r
138 this.«p.fieldName» = «p.fieldName»;
\r
144 def protected copyConstructor() '''
\r
146 * Creates a copy from Source Object.
\r
148 * @param source Source object
\r
150 public «type.name»(«type.name» source) {
\r
151 «IF false == parentProperties.empty»
\r
154 «FOR p : properties»
\r
155 this.«p.fieldName» = source.«p.fieldName»;
\r
160 def protected parentConstructor() '''
\r
162 * Creates a new instance from «genTO.superType.importedName»
\r
164 * @param source Source object
\r
166 public «type.name»(«genTO.superType.importedName» source) {
\r
174 * Template method which generates JAVA class declaration.
\r
176 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
177 * @return string with class declaration in JAVA format
\r
179 def protected generateClassDeclaration(boolean isInnerClass) '''
\r
181 IF (isInnerClass)»«
\r
183 ELSEIF (type.abstract)»«
\r
187 ENDIF»class «type.name»«
\r
188 IF (genTO.superType != null)»«
\r
189 " extends "»«genTO.superType.importedName»«
\r
191 «IF (!type.implements.empty)»«
\r
193 FOR type : type.implements SEPARATOR ", "»«
\r
194 type.importedName»«
\r
200 * Template method which generates JAVA enum type.
\r
202 * @return string with inner enum source code in JAVA format
\r
204 def protected enumDeclarations() '''
\r
206 «FOR e : enums SEPARATOR "\n"»
\r
207 «val enumTemplate = new EnumTemplate(e)»
\r
208 «enumTemplate.generateAsInnerClass»
\r
214 * Template method wich generates JAVA constants.
\r
216 * @return string with constants in JAVA format
\r
218 def protected constantsDeclarations() '''
\r
221 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
\r
222 «val cValue = c.value»
\r
223 «IF cValue instanceof List<?>»
\r
224 «val cValues = cValue as List<?>»
\r
225 private static final List<Pattern> «Constants.MEMBER_PATTERN_LIST» = new ArrayList<Pattern>();
\r
226 public static final List<String> «TypeConstants.PATTERN_CONSTANT_NAME» = Arrays.asList(«
\r
227 FOR v : cValues SEPARATOR ", "»«
\r
228 IF v instanceof String»"«
\r
233 «generateStaticInicializationBlock»
\r
236 public static final «c.type.importedName» «c.name» = «c.value»;
\r
243 * Template method which generates JAVA static initialization block.
\r
245 * @return string with static initialization block in JAVA format
\r
247 def protected generateStaticInicializationBlock() '''
\r
249 for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») {
\r
250 «Constants.MEMBER_PATTERN_LIST».add(Pattern.compile(regEx));
\r
256 * Template method which generates JAVA class attributes.
\r
258 * @return string with the class attributes in JAVA format
\r
260 def protected generateFields() '''
\r
261 «IF !properties.empty»
\r
262 «FOR f : properties»
\r
263 «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
\r
270 * Template method which generates the method <code>hashCode()</code>.
\r
272 * @return string with the <code>hashCode()</code> method definition in JAVA format
\r
274 def protected generateHashCode() '''
\r
275 «IF !genTO.hashCodeIdentifiers.empty»
\r
277 public int hashCode() {
\r
278 final int prime = 31;
\r
280 «FOR property : genTO.hashCodeIdentifiers»
\r
281 result = prime * result + ((«property.fieldName» == null) ? 0 : «property.fieldName».hashCode());
\r
289 * Template method which generates the method <code>equals()</code>.
\r
291 * @return string with the <code>equals()</code> method definition in JAVA format
\r
293 def protected generateEquals() '''
\r
294 «IF !genTO.equalsIdentifiers.empty»
\r
296 public boolean equals(java.lang.Object obj) {
\r
303 if (getClass() != obj.getClass()) {
\r
306 «type.name» other = («type.name») obj;
\r
307 «FOR property : genTO.equalsIdentifiers»
\r
308 «val fieldName = property.fieldName»
\r
309 if («fieldName» == null) {
\r
310 if (other.«fieldName» != null) {
\r
313 } else if(!«fieldName».equals(other.«fieldName»)) {
\r
323 * Template method which generates the method <code>toString()</code>.
\r
325 * @return string with the <code>toString()</code> method definition in JAVA format
\r
327 def protected generateToString() '''
\r
328 «IF !genTO.toStringIdentifiers.empty»
\r
330 public String toString() {
\r
331 StringBuilder builder = new StringBuilder();
\r
332 «val properties = genTO.toStringIdentifiers»
\r
333 builder.append("«type.name» [«properties.get(0).fieldName»=");
\r
334 «IF properties.get(0).returnType.name.contains("[")»
\r
335 builder.append(java.util.Arrays.toString(«properties.get(0).fieldName»));
\r
337 builder.append(«properties.get(0).fieldName»);
\r
339 builder.append(«properties.get(0).fieldName»);
\r
340 «FOR i : 1..<genTO.toStringIdentifiers.size»
\r
341 builder.append(", «properties.get(i).fieldName»=");
\r
342 «IF properties.get(i).returnType.name.contains("[")»
\r
343 builder.append(java.util.Arrays.toString(«properties.get(i).fieldName»));
\r
345 builder.append(«properties.get(i).fieldName»);
\r
348 builder.append("]");
\r
349 return builder.toString();
\r