2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator
10 import static java.util.Objects.requireNonNull
11 import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
12 import static org.opendaylight.mdsal.binding.model.util.Types.BYTE_ARRAY;
13 import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
14 import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
16 import com.google.common.base.Preconditions
17 import com.google.common.collect.ImmutableList
18 import com.google.common.collect.Lists
19 import java.beans.ConstructorProperties
20 import java.util.ArrayList
21 import java.util.Base64;
22 import java.util.Comparator
25 import java.util.regex.Pattern
26 import org.opendaylight.mdsal.binding.model.api.ConcreteType
27 import org.opendaylight.mdsal.binding.model.api.Constant
28 import org.opendaylight.mdsal.binding.model.api.Enumeration
29 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
30 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
31 import org.opendaylight.mdsal.binding.model.api.Restrictions
32 import org.opendaylight.mdsal.binding.model.api.Type
33 import org.opendaylight.mdsal.binding.model.util.TypeConstants
34 import org.opendaylight.yangtools.yang.binding.CodeHelpers
35 import org.opendaylight.yangtools.yang.common.Empty
36 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
39 * Template for generating JAVA class.
41 class ClassTemplate extends BaseTemplate {
42 static val Comparator<GeneratedProperty> PROP_COMPARATOR = Comparator.comparing([prop | prop.name])
44 protected val List<GeneratedProperty> properties
45 protected val List<GeneratedProperty> finalProperties
46 protected val List<GeneratedProperty> parentProperties
47 protected val List<GeneratedProperty> allProperties
48 protected val Restrictions restrictions
51 * List of enumeration which are generated as JAVA enum type.
53 protected val List<Enumeration> enums
56 * List of constant instances which are generated as JAVA public static final attributes.
58 protected val List<Constant> consts
60 protected val GeneratedTransferObject genTO
62 val AbstractRangeGenerator<?> rangeGenerator
65 * Creates instance of this class with concrete <code>genType</code>.
67 * @param genType generated transfer object which will be transformed to JAVA class source code
69 new(GeneratedTransferObject genType) {
70 this(new TopLevelJavaGeneratedType(genType), genType)
74 * Creates instance of this class with concrete <code>genType</code>.
76 * @param genType generated transfer object which will be transformed to JAVA class source code
78 new(AbstractJavaGeneratedType javaType, GeneratedTransferObject genType) {
79 super(javaType, genType)
81 this.properties = genType.properties
82 this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties)
83 this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
84 this.restrictions = genType.restrictions
86 val sorted = new ArrayList();
87 sorted.addAll(properties);
88 sorted.addAll(parentProperties);
89 sorted.sort(PROP_COMPARATOR);
91 this.allProperties = sorted
92 this.enums = genType.enumerations
93 this.consts = genType.constantDefinitions
95 if (restrictions !== null && restrictions.rangeConstraint.present) {
96 rangeGenerator = requireNonNull(AbstractRangeGenerator.forType(findProperty(genType, "value").returnType))
103 * Generates JAVA class source code (class body only).
105 * @return string with JAVA class body source code
107 def CharSequence generateAsInnerClass() {
108 return generateBody(true)
111 override protected body() {
116 * Template method which generates class body.
118 * @param isInnerClass boolean value which specify if generated class is|isn't inner
119 * @return string with class source code in JAVA format
121 def protected generateBody(boolean isInnerClass) '''
122 «wrapToDocumentation(formatDataForJavaDoc(type))»
123 «annotationDeclaration»
124 «generateClassDeclaration(isInnerClass)» {
126 «innerClassesDeclarations»
128 «constantsDeclarations»
131 «IF restrictions !== null»
132 «IF restrictions.lengthConstraint.present»
133 «LengthGenerator.generateLengthChecker("_value", findProperty(genTO, "value").returnType, restrictions.lengthConstraint.get, this)»
135 «IF restrictions.rangeConstraint.present»
136 «rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraint.get, this)»
144 «FOR field : properties SEPARATOR "\n"»
151 «IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)»
152 «generateGetValueForBitsTypeDef»
159 «generateToString(genTO.toStringIdentifiers)»
165 * Template method which generates the method <code>getValue()</code> for typedef,
166 * which base type is BitsDefinition.
168 * @return string with the <code>getValue()</code> method definition in JAVA format
170 def protected generateGetValueForBitsTypeDef() '''
172 public boolean[] getValue() {
173 return new boolean[]{
174 «FOR property: genTO.properties SEPARATOR ','»
182 * Template method which generates inner classes inside this interface.
184 * @return string with the source code for inner classes in JAVA format
186 def protected innerClassesDeclarations() '''
187 «IF !type.enclosedTypes.empty»
188 «FOR innerClass : type.enclosedTypes SEPARATOR "\n"»
189 «generateInnerClass(innerClass)»
194 def protected constructors() '''
196 «genUnionConstructor»
198 «allValuesConstructor»
200 «IF !allProperties.empty»
203 «IF properties.empty && !parentProperties.empty »
208 def protected allValuesConstructor() '''
209 «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
210 @«ConstructorProperties.importedName»("value")
212 public «type.name»(«allProperties.asArgumentsDeclaration») {
213 «IF false == parentProperties.empty»
214 super(«parentProperties.asArguments»);
216 «FOR p : allProperties»
217 «generateRestrictions(type, p.fieldName.toString, p.returnType)»
221 * If we have patterns, we need to apply them to the value field. This is a sad
222 * consequence of how this code is structured.
224 IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
225 «CodeHelpers.importedName».requireValue(_value);
226 «genPatternEnforcer("_value")»
230 «IF p.returnType.importedName.contains("[]")»
231 «IF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name
233 this.«p.fieldName» = «p.fieldName».clone();
235 this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone();
238 this.«p.fieldName» = «p.fieldName»;
245 def protected genUnionConstructor() '''
246 «FOR p : allProperties»
247 «val List<GeneratedProperty> other = new ArrayList(properties)»
249 «genConstructor(p, other)»
255 def protected genConstructor(GeneratedProperty property, GeneratedProperty... other) '''
256 public «type.name»(«property.returnType.importedName + " " + property.name») {
257 «IF false == parentProperties.empty»
258 super(«parentProperties.asArguments»);
261 «generateRestrictions(type, property.fieldName.toString, property.returnType)»
263 this.«property.fieldName» = «property.name»;
265 this.«p.fieldName» = null;
270 def private genPatternEnforcer(String ref) '''
272 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
273 «CodeHelpers.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
278 def private static paramValue(Type returnType, String paramName) {
279 if (returnType instanceof ConcreteType) {
282 return paramName + ".getValue()"
286 def private generateRestrictions(Type type, String paramName, Type returnType) '''
287 «val restrictions = type.restrictions»
288 «IF restrictions !== null»
289 «IF restrictions.lengthConstraint.present || restrictions.rangeConstraint.present»
290 if («paramName» != null) {
291 «IF restrictions.lengthConstraint.present»
292 «LengthGenerator.generateLengthCheckerCall(paramName, paramValue(returnType, paramName))»
294 «IF restrictions.rangeConstraint.present»
295 «rangeGenerator.generateRangeCheckerCall(paramName, paramValue(returnType, paramName))»
302 def protected copyConstructor() '''
304 * Creates a copy from Source Object.
306 * @param source Source object
308 public «type.name»(«type.name» source) {
309 «IF false == parentProperties.empty»
313 this.«p.fieldName» = source.«p.fieldName»;
318 def protected parentConstructor() '''
320 * Creates a new instance from «genTO.superType.importedName»
322 * @param source Source object
324 public «type.name»(«genTO.superType.importedName» source) {
326 «genPatternEnforcer("getValue()")»
330 def protected defaultInstance() '''
331 «IF genTO.typedef && !allProperties.empty && !genTO.unionType»
332 «val prop = allProperties.get(0)»
333 «IF !("org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(prop.returnType.fullyQualifiedName))»
334 public static «genTO.name» getDefaultInstance(String defaultValue) {
335 «IF BYTE_ARRAY.equals(prop.returnType)»
336 return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
337 «ELSEIF STRING.equals(prop.returnType)»
338 return new «genTO.name»(defaultValue);
339 «ELSEIF Constants.EMPTY.equals(prop.returnType)»
340 «Preconditions.importedName».checkArgument(defaultValue.isEmpty(), "Invalid value %s", defaultValue);
341 return new «genTO.name»(«Empty.importedName».getInstance());
342 «ELSEIF allProperties.size > 1»
344 «ELSEIF BOOLEAN.equals(prop.returnType)»
345 return new «genTO.name»(«Boolean.importedName».valueOf(defaultValue));
346 «ELSEIF "java.lang.Byte".equals(prop.returnType.fullyQualifiedName)»
347 return new «genTO.name»(«Byte.importedName».valueOf(defaultValue));
348 «ELSEIF "java.lang.Short".equals(prop.returnType.fullyQualifiedName)»
349 return new «genTO.name»(«Short.importedName».valueOf(defaultValue));
350 «ELSEIF "java.lang.Integer".equals(prop.returnType.fullyQualifiedName)»
351 return new «genTO.name»(«Integer.importedName».valueOf(defaultValue));
352 «ELSEIF "java.lang.Long".equals(prop.returnType.fullyQualifiedName)»
353 return new «genTO.name»(«Long.importedName».valueOf(defaultValue));
355 return new «genTO.name»(new «prop.returnType.importedName»(defaultValue));
362 def protected bitsArgs() '''
363 «List.importedName»<«String.importedName»> properties = «Lists.importedName».newArrayList(«allProperties.propsAsArgs»);
364 if (!properties.contains(defaultValue)) {
365 throw new «IllegalArgumentException.importedName»("invalid default parameter");
368 return new «genTO.name»(
369 «FOR prop : allProperties SEPARATOR ","»
370 properties.get(i++).equals(defaultValue) ? «Boolean.importedName».TRUE : null
375 def protected propsAsArgs(Iterable<GeneratedProperty> properties) '''
376 «FOR prop : properties SEPARATOR ","»
382 * Template method which generates JAVA class declaration.
384 * @param isInnerClass boolean value which specify if generated class is|isn't inner
385 * @return string with class declaration in JAVA format
387 def protected generateClassDeclaration(boolean isInnerClass) '''
391 ELSEIF (type.abstract)»«
395 ENDIF»class «type.name»«
396 IF (genTO.superType !== null)»«
397 " extends "»«genTO.superType.importedName»«
399 «IF (!type.implements.empty)»«
401 FOR type : type.implements SEPARATOR ", "»«
408 * Template method which generates JAVA enum type.
410 * @return string with inner enum source code in JAVA format
412 def protected enumDeclarations() '''
414 «FOR e : enums SEPARATOR "\n"»
415 «new EnumTemplate(javaType.getEnclosedType(e.identifier), e).generateAsInnerClass»
420 def protected suidDeclaration() '''
421 «IF genTO.SUID !== null»
422 private static final long serialVersionUID = «genTO.SUID.value»L;
426 def protected annotationDeclaration() '''
427 «IF genTO.getAnnotations !== null»
428 «FOR e : genTO.getAnnotations»
435 * Template method which generates JAVA constants.
437 * @return string with constants in JAVA format
439 def protected constantsDeclarations() '''
442 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
443 «val cValue = c.value as Map<String, String>»
444 public static final «List.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
445 FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»);
446 «IF cValue.size == 1»
447 private static final «Pattern.importedName» «Constants.MEMBER_PATTERN_LIST» = «Pattern.importedName».compile(«TypeConstants.PATTERN_CONSTANT_NAME».get(0));
448 private static final String «Constants.MEMBER_REGEX_LIST» = "«cValue.values.iterator.next.escapeJava»";
450 private static final «Pattern.importedName»[] «Constants.MEMBER_PATTERN_LIST» = «CodeHelpers.importedName».compilePatterns(«TypeConstants.PATTERN_CONSTANT_NAME»);
451 private static final String[] «Constants.MEMBER_REGEX_LIST» = { «
452 FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
462 * Template method which generates JAVA class attributes.
464 * @return string with the class attributes in JAVA format
466 def protected generateFields() '''
467 «IF !properties.empty»
469 private«IF isReadOnly(f)» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
474 protected def isReadOnly(GeneratedProperty field) {
475 return field.readOnly
479 * Template method which generates the method <code>hashCode()</code>.
481 * @return string with the <code>hashCode()</code> method definition in JAVA format
483 def protected generateHashCode() {
484 val size = genTO.hashCodeIdentifiers.size
489 @«Override.importedName»
490 public int hashCode() {
492 «hashCodeResult(genTO.hashCodeIdentifiers)»
495 return «CodeHelpers.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
502 * Template method which generates the method <code>equals()</code>.
504 * @return string with the <code>equals()</code> method definition in JAVA format
506 def protected generateEquals() '''
507 «IF !genTO.equalsIdentifiers.empty»
508 @«Override.importedName»
509 public boolean equals(java.lang.Object obj) {
516 if (getClass() != obj.getClass()) {
519 «type.name» other = («type.name») obj;
520 «FOR property : genTO.equalsIdentifiers»
521 «val fieldName = property.fieldName»
522 if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {
531 def GeneratedProperty getPropByName(String name) {
532 for (GeneratedProperty prop : allProperties) {
533 if (prop.name.equals(name)) {