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.BaseYangTypes.BINARY_TYPE
12 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.BOOLEAN_TYPE
13 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.EMPTY_TYPE
14 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INSTANCE_IDENTIFIER
15 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INT16_TYPE
16 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INT32_TYPE
17 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INT64_TYPE
18 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.INT8_TYPE
19 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.STRING_TYPE
20 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.UINT16_TYPE
21 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.UINT32_TYPE
22 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.UINT64_TYPE
23 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.UINT8_TYPE
24 import static org.opendaylight.mdsal.binding.model.util.BindingTypes.SCALAR_TYPE_OBJECT
25 import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN
26 import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
27 import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
29 import com.google.common.base.MoreObjects
30 import com.google.common.base.Preconditions
31 import com.google.common.collect.ImmutableList
32 import com.google.common.collect.Lists
33 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
34 import java.beans.ConstructorProperties
35 import java.util.ArrayList
36 import java.util.Base64;
37 import java.util.Collection
38 import java.util.Comparator
42 import javax.management.ConstructorParameters
43 import org.gaul.modernizer_maven_annotations.SuppressModernizer
44 import org.opendaylight.mdsal.binding.model.api.ConcreteType
45 import org.opendaylight.mdsal.binding.model.api.Constant
46 import org.opendaylight.mdsal.binding.model.api.Enumeration
47 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
48 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
49 import org.opendaylight.mdsal.binding.model.api.Restrictions
50 import org.opendaylight.mdsal.binding.model.api.Type
51 import org.opendaylight.mdsal.binding.model.util.TypeConstants
52 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
53 import org.opendaylight.yangtools.yang.common.Empty
54 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
57 * Template for generating JAVA class.
60 class ClassTemplate extends BaseTemplate {
61 static val Comparator<GeneratedProperty> PROP_COMPARATOR = Comparator.comparing([prop | prop.name])
62 static val VALUEOF_TYPES = Set.of(
73 protected val List<GeneratedProperty> properties
74 protected val List<GeneratedProperty> finalProperties
75 protected val List<GeneratedProperty> parentProperties
76 protected val List<GeneratedProperty> allProperties
77 protected val Restrictions restrictions
80 * List of enumeration which are generated as JAVA enum type.
82 protected val List<Enumeration> enums
85 * List of constant instances which are generated as JAVA public static final attributes.
87 protected val List<Constant> consts
89 protected val GeneratedTransferObject genTO
91 val AbstractRangeGenerator<?> rangeGenerator
94 * Creates instance of this class with concrete <code>genType</code>.
96 * @param genType generated transfer object which will be transformed to JAVA class source code
98 new(GeneratedTransferObject genType) {
99 this(new TopLevelJavaGeneratedType(genType), genType)
103 * Creates instance of this class with concrete <code>genType</code>.
105 * @param genType generated transfer object which will be transformed to JAVA class source code
107 new(AbstractJavaGeneratedType javaType, GeneratedTransferObject genType) {
108 super(javaType, genType)
110 this.properties = genType.properties
111 this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties)
112 this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
113 this.restrictions = genType.restrictions
115 val sorted = new ArrayList();
116 sorted.addAll(properties);
117 sorted.addAll(parentProperties);
118 sorted.sort(PROP_COMPARATOR);
120 this.allProperties = sorted
121 this.enums = genType.enumerations
122 this.consts = genType.constantDefinitions
124 if (restrictions !== null && restrictions.rangeConstraint.present) {
125 rangeGenerator = requireNonNull(AbstractRangeGenerator.forType(TypeUtils.encapsulatedValueType(genType)))
127 rangeGenerator = null
132 * Generates JAVA class source code (class body only).
134 * @return string with JAVA class body source code
136 def CharSequence generateAsInnerClass() {
137 return generateBody(true)
140 override protected body() {
145 * Template method which generates class body.
147 * @param isInnerClass boolean value which specify if generated class is|isn't inner
148 * @return string with class source code in JAVA format
150 def protected generateBody(boolean isInnerClass) '''
151 «type.formatDataForJavaDoc.wrapToDocumentation»
152 «annotationDeclaration»
153 «generateClassDeclaration(isInnerClass)» {
155 «innerClassesDeclarations»
157 «constantsDeclarations»
160 «IF restrictions !== null»
161 «IF restrictions.lengthConstraint.present»
162 «LengthGenerator.generateLengthChecker("_value", TypeUtils.encapsulatedValueType(genTO),
163 restrictions.lengthConstraint.get, this)»
165 «IF restrictions.rangeConstraint.present»
166 «rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraint.get, this)»
176 «IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)»
177 «generateGetValueForBitsTypeDef»
184 «generateToString(genTO.toStringIdentifiers)»
189 def private propertyMethods() {
190 if (properties.empty) {
193 isScalarTypeObject ? scalarTypeObjectValue(properties.get(0)) : defaultProperties
196 def private isScalarTypeObject() {
197 for (impl : genTO.implements) {
198 if (SCALAR_TYPE_OBJECT.identifier.equals(impl.identifier)) {
205 def private defaultProperties() '''
206 «FOR field : properties SEPARATOR "\n"»
214 def private scalarTypeObjectValue(GeneratedProperty field) '''
215 @«OVERRIDE.importedName»
216 public «field.returnType.importedName» «BindingMapping.SCALAR_TYPE_OBJECT_GET_VALUE_NAME»() {
217 return «field.fieldName»«field.cloneCall»;
222 * Template method which generates the method <code>getValue()</code> for typedef,
223 * which base type is BitsDefinition.
225 * @return string with the <code>getValue()</code> method definition in JAVA format
227 def protected generateGetValueForBitsTypeDef() '''
229 public boolean[] getValue() {
230 return new boolean[]{
231 «FOR property: genTO.properties SEPARATOR ','»
239 * Template method which generates inner classes inside this interface.
241 * @return string with the source code for inner classes in JAVA format
243 def protected innerClassesDeclarations() '''
244 «IF !type.enclosedTypes.empty»
245 «FOR innerClass : type.enclosedTypes SEPARATOR "\n"»
246 «generateInnerClass(innerClass)»
251 def protected constructors() '''
253 «genUnionConstructor»
254 «ELSEIF genTO.typedef && allProperties.size == 1 && allProperties.get(0).name.equals(TypeConstants.VALUE_PROP)»
258 «allValuesConstructor»
262 «IF !allProperties.empty»
265 «IF properties.empty && !parentProperties.empty »
270 def allValuesConstructor() '''
271 public «type.name»(«allProperties.asArgumentsDeclaration») {
272 «IF !parentProperties.empty»
273 super(«parentProperties.asArguments»);
275 «FOR p : allProperties»
276 «generateRestrictions(type, p.fieldName, p.returnType)»
280 «val fieldName = p.fieldName»
281 «IF p.returnType.name.endsWith("[]")»
282 this.«fieldName» = «fieldName» == null ? null : «fieldName».clone();
284 this.«fieldName» = «fieldName»;
290 def private typedefConstructor() '''
291 @«ConstructorParameters.importedName»("«TypeConstants.VALUE_PROP»")
292 @«ConstructorProperties.importedName»("«TypeConstants.VALUE_PROP»")
293 public «type.name»(«allProperties.asArgumentsDeclaration») {
294 «IF !parentProperties.empty»
295 super(«parentProperties.asArguments»);
297 «FOR p : allProperties»
298 «generateRestrictions(type, p.fieldName, p.returnType)»
301 * If we have patterns, we need to apply them to the value field. This is a sad consequence of how this code is
304 «CODEHELPERS.importedName».requireValue(_value);
305 «genPatternEnforcer("_value")»
308 «val fieldName = p.fieldName»
309 this.«fieldName» = «fieldName»«p.cloneCall»;
314 def private legacyConstructor() {
315 if (!hasUintProperties) {
319 val compatUint = CODEHELPERS.importedName + ".compatUint("
323 * Utility migration constructor.
325 «FOR prop : allProperties»
326 * @param «prop.fieldName» «prop.name»«IF prop.isUintType» in legacy Java type«ENDIF»
328 * @deprecated Use {#link «type.name»(«FOR prop : allProperties SEPARATOR ", "»«prop.returnType.importedJavadocName»«ENDFOR»)} instead.
330 @Deprecated(forRemoval = true)
331 public «type.getName»(«FOR prop : allProperties SEPARATOR ", "»«prop.legacyType.importedName» «prop.fieldName»«ENDFOR») {
332 this(«FOR prop : allProperties SEPARATOR ", "»«IF prop.isUintType»«compatUint»«prop.fieldName»)«ELSE»«prop.fieldName»«ENDIF»«ENDFOR»);
337 def protected genUnionConstructor() '''
338 «FOR p : allProperties»
339 «val List<GeneratedProperty> other = new ArrayList(properties)»
341 «genConstructor(p, other)»
346 def protected genConstructor(GeneratedProperty property, Iterable<GeneratedProperty> other) '''
347 public «type.name»(«property.returnType.importedName + " " + property.name») {
348 «IF !parentProperties.empty»
349 super(«parentProperties.asArguments»);
352 «val fieldName = property.fieldName»
353 «generateRestrictions(type, fieldName, property.returnType)»
355 this.«fieldName» = «property.name»;
357 this.«p.fieldName» = null;
362 def private genPatternEnforcer(String ref) '''
364 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
365 «CODEHELPERS.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
370 def private static paramValue(Type returnType, String paramName) {
371 if (returnType instanceof ConcreteType) {
374 return paramName + ".getValue()"
378 def generateRestrictions(Type type, String paramName, Type returnType) '''
379 «val restrictions = type.restrictions»
380 «IF restrictions !== null»
381 «IF restrictions.lengthConstraint.present || restrictions.rangeConstraint.present»
382 if («paramName» != null) {
383 «IF restrictions.lengthConstraint.present»
384 «LengthGenerator.generateLengthCheckerCall(paramName, paramValue(returnType, paramName))»
386 «IF restrictions.rangeConstraint.present»
387 «rangeGenerator.generateRangeCheckerCall(paramName, paramValue(returnType, paramName))»
394 def protected copyConstructor() '''
396 * Creates a copy from Source Object.
398 * @param source Source object
400 public «type.name»(«type.name» source) {
401 «IF !parentProperties.empty»
405 «val fieldName = p.fieldName»
406 this.«fieldName» = source.«fieldName»;
411 def protected parentConstructor() '''
413 * Creates a new instance from «genTO.superType.importedName»
415 * @param source Source object
417 public «type.name»(«genTO.superType.importedName» source) {
419 «genPatternEnforcer("getValue()")»
423 def protected defaultInstance() '''
424 «IF genTO.typedef && !allProperties.empty && !genTO.unionType»
425 «val prop = allProperties.get(0)»
426 «val propType = prop.returnType»
427 «IF !(INSTANCE_IDENTIFIER.identifier.equals(propType.identifier))»
428 public static «genTO.name» getDefaultInstance(final String defaultValue) {
429 «IF allProperties.size > 1»
431 «ELSEIF VALUEOF_TYPES.contains(propType)»
432 return new «genTO.name»(«propType.importedName».valueOf(defaultValue));
433 «ELSEIF STRING_TYPE.equals(propType)»
434 return new «genTO.name»(defaultValue);
435 «ELSEIF BINARY_TYPE.equals(propType)»
436 return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
437 «ELSEIF EMPTY_TYPE.equals(propType)»
438 «Preconditions.importedName».checkArgument(defaultValue.isEmpty(), "Invalid value %s", defaultValue);
439 return new «genTO.name»(«Empty.importedName».getInstance());
441 return new «genTO.name»(new «propType.importedName»(defaultValue));
448 @SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE", justification = "FOR with SEPARATOR, not needing for value")
449 def protected bitsArgs() '''
450 «JU_LIST.importedName»<«STRING.importedName»> properties = «Lists.importedName».newArrayList(«allProperties.propsAsArgs»);
451 if (!properties.contains(defaultValue)) {
452 throw new «IllegalArgumentException.importedName»("invalid default parameter");
455 return new «genTO.name»(
456 «FOR prop : allProperties SEPARATOR ","»
457 properties.get(i++).equals(defaultValue) ? «BOOLEAN.importedName».TRUE : null
462 def protected propsAsArgs(Iterable<GeneratedProperty> properties) '''
463 «FOR prop : properties SEPARATOR ","»
469 * Template method which generates JAVA class declaration.
471 * @param isInnerClass boolean value which specify if generated class is|isn't inner
472 * @return string with class declaration in JAVA format
474 def protected generateClassDeclaration(boolean isInnerClass) '''
478 ELSEIF (type.abstract)»«
482 ENDIF»class «type.name»«
483 IF (genTO.superType !== null)»«
484 " extends "»«genTO.superType.importedName»«
486 «IF (!type.implements.empty)»«
488 FOR type : type.implements SEPARATOR ", "»«
495 * Template method which generates JAVA enum type.
497 * @return string with inner enum source code in JAVA format
499 def protected enumDeclarations() '''
501 «FOR e : enums SEPARATOR "\n"»
502 «new EnumTemplate(javaType.getEnclosedType(e.identifier), e).generateAsInnerClass»
507 def protected suidDeclaration() '''
508 «IF genTO.SUID !== null»
509 private static final long serialVersionUID = «genTO.SUID.value»L;
513 def protected annotationDeclaration() '''
514 «IF genTO.getAnnotations !== null»
515 «FOR e : genTO.getAnnotations»
522 * Template method which generates JAVA constants.
524 * @return string with constants in JAVA format
526 def protected constantsDeclarations() '''
529 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
530 «val cValue = c.value as Map<String, String>»
531 «val jurPatternRef = JUR_PATTERN.importedName»
532 public static final «JU_LIST.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
533 FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»);
534 «IF cValue.size == 1»
535 private static final «jurPatternRef» «Constants.MEMBER_PATTERN_LIST» = «jurPatternRef».compile(«TypeConstants.PATTERN_CONSTANT_NAME».get(0));
536 private static final String «Constants.MEMBER_REGEX_LIST» = "«cValue.values.iterator.next.escapeJava»";
538 private static final «jurPatternRef»[] «Constants.MEMBER_PATTERN_LIST» = «CODEHELPERS.importedName».compilePatterns(«TypeConstants.PATTERN_CONSTANT_NAME»);
539 private static final String[] «Constants.MEMBER_REGEX_LIST» = { «
540 FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
550 * Template method which generates JAVA class attributes.
552 * @return string with the class attributes in JAVA format
554 def protected generateFields() '''
555 «IF !properties.empty»
557 private«IF isReadOnly(f)» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
562 protected def isReadOnly(GeneratedProperty field) {
563 return field.readOnly
567 * Template method which generates the method <code>hashCode()</code>.
569 * @return string with the <code>hashCode()</code> method definition in JAVA format
571 def protected generateHashCode() {
572 val size = genTO.hashCodeIdentifiers.size
577 @«OVERRIDE.importedName»
578 public int hashCode() {
580 final int prime = 31;
582 «FOR property : genTO.hashCodeIdentifiers»
583 result = prime * result + «property.importedUtilClass».hashCode(«property.fieldName»);
587 return «CODEHELPERS.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
594 * Template method which generates the method <code>equals()</code>.
596 * @return string with the <code>equals()</code> method definition in JAVA format
598 def private generateEquals() '''
599 «IF !genTO.equalsIdentifiers.empty»
600 @«OVERRIDE.importedName»
601 public final boolean equals(java.lang.Object obj) {
605 if (!(obj instanceof «type.name»)) {
608 final «type.name» other = («type.name») obj;
609 «FOR property : genTO.equalsIdentifiers»
610 «val fieldName = property.fieldName»
611 if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {
620 def private generateToString(Collection<? extends GeneratedProperty> properties) '''
621 «IF !properties.empty»
622 @«OVERRIDE.importedName»
623 public «STRING.importedName» toString() {
624 final «MoreObjects.importedName».ToStringHelper helper = «MoreObjects.importedName».toStringHelper(«type.importedName».class);
625 «FOR property : properties»
626 «CODEHELPERS.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
628 return helper.toString();
633 def GeneratedProperty getPropByName(String name) {
634 for (GeneratedProperty prop : allProperties) {
635 if (prop.name.equals(name)) {
642 def private hasUintProperties() {
643 for (GeneratedProperty prop : allProperties) {
644 if (prop.isUintType) {
651 def private static isUintType(GeneratedProperty prop) {
652 UINT_TYPES.containsKey(prop.returnType)
655 def private static legacyType(GeneratedProperty prop) {
656 val type = prop.returnType
657 val uint = UINT_TYPES.get(type)