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 extension org.apache.commons.text.StringEscapeUtils.escapeJava
13 import com.google.common.collect.ImmutableList
14 import com.google.common.collect.Lists
15 import java.beans.ConstructorProperties
16 import java.util.ArrayList
17 import java.util.Base64;
18 import java.util.Collections
21 import java.util.Objects
22 import java.util.regex.Pattern
23 import org.opendaylight.mdsal.binding.model.api.ConcreteType
24 import org.opendaylight.mdsal.binding.model.api.Constant
25 import org.opendaylight.mdsal.binding.model.api.Enumeration
26 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
27 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
28 import org.opendaylight.mdsal.binding.model.api.Restrictions
29 import org.opendaylight.mdsal.binding.model.api.Type
30 import org.opendaylight.mdsal.binding.model.util.TypeConstants
31 import org.opendaylight.yangtools.yang.binding.CodeHelpers
32 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
35 * Template for generating JAVA class.
37 class ClassTemplate extends BaseTemplate {
39 protected val List<GeneratedProperty> properties
40 protected val List<GeneratedProperty> finalProperties
41 protected val List<GeneratedProperty> parentProperties
42 protected val Iterable<GeneratedProperty> allProperties
43 protected val Restrictions restrictions
46 * List of enumeration which are generated as JAVA enum type.
48 protected val List<Enumeration> enums
51 * List of constant instances which are generated as JAVA public static final attributes.
53 protected val List<Constant> consts
55 protected val GeneratedTransferObject genTO
57 val AbstractRangeGenerator<?> rangeGenerator
60 * Creates instance of this class with concrete <code>genType</code>.
62 * @param genType generated transfer object which will be transformed to JAVA class source code
64 new(GeneratedTransferObject genType) {
65 this(new TopLevelJavaGeneratedType(genType), genType)
69 * Creates instance of this class with concrete <code>genType</code>.
71 * @param genType generated transfer object which will be transformed to JAVA class source code
73 new(AbstractJavaGeneratedType javaType, GeneratedTransferObject genType) {
74 super(javaType, genType)
76 this.properties = genType.properties
77 this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties)
78 this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
79 this.restrictions = genType.restrictions
81 var List<GeneratedProperty> sorted = new ArrayList<GeneratedProperty>();
82 sorted.addAll(properties);
83 sorted.addAll(parentProperties);
84 Collections.sort(sorted, [p1, p2|
85 p1.name.compareTo(p2.name)
88 this.allProperties = sorted
89 this.enums = genType.enumerations
90 this.consts = genType.constantDefinitions
92 if (restrictions !== null && restrictions.rangeConstraint.present) {
93 rangeGenerator = requireNonNull(AbstractRangeGenerator.forType(findProperty(genType, "value").returnType))
100 * Generates JAVA class source code (class body only).
102 * @return string with JAVA class body source code
104 def CharSequence generateAsInnerClass() {
105 return generateBody(true)
108 override protected body() {
113 * Template method which generates class body.
115 * @param isInnerClass boolean value which specify if generated class is|isn't inner
116 * @return string with class source code in JAVA format
118 def protected generateBody(boolean isInnerClass) '''
119 «wrapToDocumentation(formatDataForJavaDoc(type))»
120 «annotationDeclaration»
121 «generateClassDeclaration(isInnerClass)» {
123 «innerClassesDeclarations»
125 «constantsDeclarations»
128 «IF restrictions !== null»
129 «IF restrictions.lengthConstraint.present»
130 «LengthGenerator.generateLengthChecker("_value", findProperty(genTO, "value").returnType, restrictions.lengthConstraint.get, this)»
132 «IF restrictions.rangeConstraint.present»
133 «rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraint.get, this)»
141 «FOR field : properties SEPARATOR "\n"»
148 «IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)»
149 «generateGetValueForBitsTypeDef»
156 «generateToString(genTO.toStringIdentifiers)»
162 * Template method which generates the method <code>getValue()</code> for typedef,
163 * which base type is BitsDefinition.
165 * @return string with the <code>getValue()</code> method definition in JAVA format
167 def protected generateGetValueForBitsTypeDef() '''
169 public boolean[] getValue() {
170 return new boolean[]{
171 «FOR property: genTO.properties SEPARATOR ','»
179 * Template method which generates inner classes inside this interface.
181 * @return string with the source code for inner classes in JAVA format
183 def protected innerClassesDeclarations() '''
184 «IF !type.enclosedTypes.empty»
185 «FOR innerClass : type.enclosedTypes SEPARATOR "\n"»
186 «generateInnerClass(innerClass)»
191 def protected constructors() '''
193 «genUnionConstructor»
195 «allValuesConstructor»
197 «IF !allProperties.empty»
200 «IF properties.empty && !parentProperties.empty »
205 def protected allValuesConstructor() '''
206 «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
207 @«ConstructorProperties.importedName»("value")
209 public «type.name»(«allProperties.asArgumentsDeclaration») {
210 «IF false == parentProperties.empty»
211 super(«parentProperties.asArguments»);
213 «FOR p : allProperties»
214 «generateRestrictions(type, p.fieldName.toString, p.returnType)»
218 * If we have patterns, we need to apply them to the value field. This is a sad
219 * consequence of how this code is structured.
221 IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
222 «Objects.importedName».requireNonNull(_value, "Supplied value may not be null");
223 «genPatternEnforcer("_value")»
227 «IF p.returnType.importedName.contains("[]")»
228 «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name
230 this.«p.fieldName» = «p.fieldName».clone();
232 this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone();
235 this.«p.fieldName» = «p.fieldName»;
242 def protected genUnionConstructor() '''
243 «FOR p : allProperties»
244 «val List<GeneratedProperty> other = new ArrayList(properties)»
246 «genConstructor(p, other)»
252 def protected genConstructor(GeneratedProperty property, GeneratedProperty... other) '''
253 public «type.name»(«property.returnType.importedName + " " + property.name») {
254 «IF false == parentProperties.empty»
255 super(«parentProperties.asArguments»);
258 «generateRestrictions(type, property.fieldName.toString, property.returnType)»
260 this.«property.fieldName» = «property.name»;
262 this.«p.fieldName» = null;
267 def private genPatternEnforcer(String ref) '''
269 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
270 «CodeHelpers.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
275 def private static paramValue(Type returnType, String paramName) {
276 if (returnType instanceof ConcreteType) {
279 return paramName + ".getValue()"
283 def private generateRestrictions(Type type, String paramName, Type returnType) '''
284 «val restrictions = type.restrictions»
285 «IF restrictions !== null»
286 «IF restrictions.lengthConstraint.present || restrictions.rangeConstraint.present»
287 if («paramName» != null) {
288 «IF restrictions.lengthConstraint.present»
289 «LengthGenerator.generateLengthCheckerCall(paramName, paramValue(returnType, paramName))»
291 «IF restrictions.rangeConstraint.present»
292 «rangeGenerator.generateRangeCheckerCall(paramName, paramValue(returnType, paramName))»
299 def protected copyConstructor() '''
301 * Creates a copy from Source Object.
303 * @param source Source object
305 public «type.name»(«type.name» source) {
306 «IF false == parentProperties.empty»
310 this.«p.fieldName» = source.«p.fieldName»;
315 def protected parentConstructor() '''
317 * Creates a new instance from «genTO.superType.importedName»
319 * @param source Source object
321 public «type.name»(«genTO.superType.importedName» source) {
323 «genPatternEnforcer("getValue()")»
327 def protected defaultInstance() '''
328 «IF genTO.typedef && !allProperties.empty && !genTO.unionType»
329 «val prop = allProperties.get(0)»
330 «IF !("org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(prop.returnType.fullyQualifiedName))»
331 public static «genTO.name» getDefaultInstance(String defaultValue) {
332 «IF "byte[]".equals(prop.returnType.name)»
333 return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
334 «ELSEIF "java.lang.String".equals(prop.returnType.fullyQualifiedName)»
335 return new «genTO.name»(defaultValue);
336 «ELSEIF allProperties.size > 1»
338 «ELSEIF "java.lang.Boolean".equals(prop.returnType.fullyQualifiedName)»
339 return new «genTO.name»(«Boolean.importedName».valueOf(defaultValue));
340 «ELSEIF "java.lang.Byte".equals(prop.returnType.fullyQualifiedName)»
341 return new «genTO.name»(«Byte.importedName».valueOf(defaultValue));
342 «ELSEIF "java.lang.Short".equals(prop.returnType.fullyQualifiedName)»
343 return new «genTO.name»(«Short.importedName».valueOf(defaultValue));
344 «ELSEIF "java.lang.Integer".equals(prop.returnType.fullyQualifiedName)»
345 return new «genTO.name»(«Integer.importedName».valueOf(defaultValue));
346 «ELSEIF "java.lang.Long".equals(prop.returnType.fullyQualifiedName)»
347 return new «genTO.name»(«Long.importedName».valueOf(defaultValue));
349 return new «genTO.name»(new «prop.returnType.importedName»(defaultValue));
356 def protected bitsArgs() '''
357 «List.importedName»<«String.importedName»> properties = «Lists.importedName».newArrayList(«allProperties.propsAsArgs»);
358 if (!properties.contains(defaultValue)) {
359 throw new «IllegalArgumentException.importedName»("invalid default parameter");
362 return new «genTO.name»(
363 «FOR prop : allProperties SEPARATOR ","»
364 properties.get(i++).equals(defaultValue) ? «Boolean.importedName».TRUE : null
369 def protected propsAsArgs(Iterable<GeneratedProperty> properties) '''
370 «FOR prop : properties SEPARATOR ","»
376 * Template method which generates JAVA class declaration.
378 * @param isInnerClass boolean value which specify if generated class is|isn't inner
379 * @return string with class declaration in JAVA format
381 def protected generateClassDeclaration(boolean isInnerClass) '''
385 ELSEIF (type.abstract)»«
389 ENDIF»class «type.name»«
390 IF (genTO.superType !== null)»«
391 " extends "»«genTO.superType.importedName»«
393 «IF (!type.implements.empty)»«
395 FOR type : type.implements SEPARATOR ", "»«
402 * Template method which generates JAVA enum type.
404 * @return string with inner enum source code in JAVA format
406 def protected enumDeclarations() '''
408 «FOR e : enums SEPARATOR "\n"»
409 «new EnumTemplate(javaType.getEnclosedType(e.identifier), e).generateAsInnerClass»
414 def protected suidDeclaration() '''
415 «IF genTO.SUID !== null»
416 private static final long serialVersionUID = «genTO.SUID.value»L;
420 def protected annotationDeclaration() '''
421 «IF genTO.getAnnotations !== null»
422 «FOR e : genTO.getAnnotations»
429 * Template method which generates JAVA constants.
431 * @return string with constants in JAVA format
433 def protected constantsDeclarations() '''
436 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
437 «val cValue = c.value as Map<String, String>»
438 public static final «List.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
439 FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»);
440 «IF cValue.size == 1»
441 private static final «Pattern.importedName» «Constants.MEMBER_PATTERN_LIST» = «Pattern.importedName».compile(«TypeConstants.PATTERN_CONSTANT_NAME».get(0));
442 private static final String «Constants.MEMBER_REGEX_LIST» = "«cValue.values.get(0).escapeJava»";
444 private static final «Pattern.importedName»[] «Constants.MEMBER_PATTERN_LIST» = «CodeHelpers.importedName».compilePatterns(«TypeConstants.PATTERN_CONSTANT_NAME»);
445 private static final String[] «Constants.MEMBER_REGEX_LIST» = { «
446 FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
456 * Template method which generates JAVA class attributes.
458 * @return string with the class attributes in JAVA format
460 def protected generateFields() '''
461 «IF !properties.empty»
463 private«IF isReadOnly(f)» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
468 protected def isReadOnly(GeneratedProperty field) {
469 return field.readOnly
473 * Template method which generates the method <code>hashCode()</code>.
475 * @return string with the <code>hashCode()</code> method definition in JAVA format
477 def protected generateHashCode() {
478 val size = genTO.hashCodeIdentifiers.size
483 @«Override.importedName»
484 public int hashCode() {
486 «hashCodeResult(genTO.hashCodeIdentifiers)»
489 return «CodeHelpers.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
496 * Template method which generates the method <code>equals()</code>.
498 * @return string with the <code>equals()</code> method definition in JAVA format
500 def protected generateEquals() '''
501 «IF !genTO.equalsIdentifiers.empty»
502 @«Override.importedName»
503 public boolean equals(java.lang.Object obj) {
510 if (getClass() != obj.getClass()) {
513 «type.name» other = («type.name») obj;
514 «FOR property : genTO.equalsIdentifiers»
515 «val fieldName = property.fieldName»
516 if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {
525 def GeneratedProperty getPropByName(String name) {
526 for (GeneratedProperty prop : allProperties) {
527 if (prop.name.equals(name)) {