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.Collections
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 {
43 protected val List<GeneratedProperty> properties
44 protected val List<GeneratedProperty> finalProperties
45 protected val List<GeneratedProperty> parentProperties
46 protected val Iterable<GeneratedProperty> allProperties
47 protected val Restrictions restrictions
50 * List of enumeration which are generated as JAVA enum type.
52 protected val List<Enumeration> enums
55 * List of constant instances which are generated as JAVA public static final attributes.
57 protected val List<Constant> consts
59 protected val GeneratedTransferObject genTO
61 val AbstractRangeGenerator<?> rangeGenerator
64 * Creates instance of this class with concrete <code>genType</code>.
66 * @param genType generated transfer object which will be transformed to JAVA class source code
68 new(GeneratedTransferObject genType) {
69 this(new TopLevelJavaGeneratedType(genType), genType)
73 * Creates instance of this class with concrete <code>genType</code>.
75 * @param genType generated transfer object which will be transformed to JAVA class source code
77 new(AbstractJavaGeneratedType javaType, GeneratedTransferObject genType) {
78 super(javaType, genType)
80 this.properties = genType.properties
81 this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties)
82 this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
83 this.restrictions = genType.restrictions
85 var List<GeneratedProperty> sorted = new ArrayList<GeneratedProperty>();
86 sorted.addAll(properties);
87 sorted.addAll(parentProperties);
88 Collections.sort(sorted, [p1, p2|
89 p1.name.compareTo(p2.name)
92 this.allProperties = sorted
93 this.enums = genType.enumerations
94 this.consts = genType.constantDefinitions
96 if (restrictions !== null && restrictions.rangeConstraint.present) {
97 rangeGenerator = requireNonNull(AbstractRangeGenerator.forType(findProperty(genType, "value").returnType))
104 * Generates JAVA class source code (class body only).
106 * @return string with JAVA class body source code
108 def CharSequence generateAsInnerClass() {
109 return generateBody(true)
112 override protected body() {
117 * Template method which generates class body.
119 * @param isInnerClass boolean value which specify if generated class is|isn't inner
120 * @return string with class source code in JAVA format
122 def protected generateBody(boolean isInnerClass) '''
123 «wrapToDocumentation(formatDataForJavaDoc(type))»
124 «annotationDeclaration»
125 «generateClassDeclaration(isInnerClass)» {
127 «innerClassesDeclarations»
129 «constantsDeclarations»
132 «IF restrictions !== null»
133 «IF restrictions.lengthConstraint.present»
134 «LengthGenerator.generateLengthChecker("_value", findProperty(genTO, "value").returnType, restrictions.lengthConstraint.get, this)»
136 «IF restrictions.rangeConstraint.present»
137 «rangeGenerator.generateRangeChecker("_value", restrictions.rangeConstraint.get, this)»
145 «FOR field : properties SEPARATOR "\n"»
152 «IF (genTO.isTypedef() && genTO.getBaseType instanceof BitsTypeDefinition)»
153 «generateGetValueForBitsTypeDef»
160 «generateToString(genTO.toStringIdentifiers)»
166 * Template method which generates the method <code>getValue()</code> for typedef,
167 * which base type is BitsDefinition.
169 * @return string with the <code>getValue()</code> method definition in JAVA format
171 def protected generateGetValueForBitsTypeDef() '''
173 public boolean[] getValue() {
174 return new boolean[]{
175 «FOR property: genTO.properties SEPARATOR ','»
183 * Template method which generates inner classes inside this interface.
185 * @return string with the source code for inner classes in JAVA format
187 def protected innerClassesDeclarations() '''
188 «IF !type.enclosedTypes.empty»
189 «FOR innerClass : type.enclosedTypes SEPARATOR "\n"»
190 «generateInnerClass(innerClass)»
195 def protected constructors() '''
197 «genUnionConstructor»
199 «allValuesConstructor»
201 «IF !allProperties.empty»
204 «IF properties.empty && !parentProperties.empty »
209 def protected allValuesConstructor() '''
210 «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
211 @«ConstructorProperties.importedName»("value")
213 public «type.name»(«allProperties.asArgumentsDeclaration») {
214 «IF false == parentProperties.empty»
215 super(«parentProperties.asArguments»);
217 «FOR p : allProperties»
218 «generateRestrictions(type, p.fieldName.toString, p.returnType)»
222 * If we have patterns, we need to apply them to the value field. This is a sad
223 * consequence of how this code is structured.
225 IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
226 «CodeHelpers.importedName».requireValue(_value);
227 «genPatternEnforcer("_value")»
231 «IF p.returnType.importedName.contains("[]")»
232 «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name
234 this.«p.fieldName» = «p.fieldName».clone();
236 this.«p.fieldName» = «p.fieldName» == null ? null : «p.fieldName».clone();
239 this.«p.fieldName» = «p.fieldName»;
246 def protected genUnionConstructor() '''
247 «FOR p : allProperties»
248 «val List<GeneratedProperty> other = new ArrayList(properties)»
250 «genConstructor(p, other)»
256 def protected genConstructor(GeneratedProperty property, GeneratedProperty... other) '''
257 public «type.name»(«property.returnType.importedName + " " + property.name») {
258 «IF false == parentProperties.empty»
259 super(«parentProperties.asArguments»);
262 «generateRestrictions(type, property.fieldName.toString, property.returnType)»
264 this.«property.fieldName» = «property.name»;
266 this.«p.fieldName» = null;
271 def private genPatternEnforcer(String ref) '''
273 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
274 «CodeHelpers.importedName».checkPattern(«ref», «Constants.MEMBER_PATTERN_LIST», «Constants.MEMBER_REGEX_LIST»);
279 def private static paramValue(Type returnType, String paramName) {
280 if (returnType instanceof ConcreteType) {
283 return paramName + ".getValue()"
287 def private generateRestrictions(Type type, String paramName, Type returnType) '''
288 «val restrictions = type.restrictions»
289 «IF restrictions !== null»
290 «IF restrictions.lengthConstraint.present || restrictions.rangeConstraint.present»
291 if («paramName» != null) {
292 «IF restrictions.lengthConstraint.present»
293 «LengthGenerator.generateLengthCheckerCall(paramName, paramValue(returnType, paramName))»
295 «IF restrictions.rangeConstraint.present»
296 «rangeGenerator.generateRangeCheckerCall(paramName, paramValue(returnType, paramName))»
303 def protected copyConstructor() '''
305 * Creates a copy from Source Object.
307 * @param source Source object
309 public «type.name»(«type.name» source) {
310 «IF false == parentProperties.empty»
314 this.«p.fieldName» = source.«p.fieldName»;
319 def protected parentConstructor() '''
321 * Creates a new instance from «genTO.superType.importedName»
323 * @param source Source object
325 public «type.name»(«genTO.superType.importedName» source) {
327 «genPatternEnforcer("getValue()")»
331 def protected defaultInstance() '''
332 «IF genTO.typedef && !allProperties.empty && !genTO.unionType»
333 «val prop = allProperties.get(0)»
334 «IF !("org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(prop.returnType.fullyQualifiedName))»
335 public static «genTO.name» getDefaultInstance(String defaultValue) {
336 «IF BYTE_ARRAY.equals(prop.returnType)»
337 return new «genTO.name»(«Base64.importedName».getDecoder().decode(defaultValue));
338 «ELSEIF STRING.equals(prop.returnType)»
339 return new «genTO.name»(defaultValue);
340 «ELSEIF Constants.EMPTY.equals(prop.returnType)»
341 «Preconditions.importedName».checkArgument(defaultValue.isEmpty(), "Invalid value %s", defaultValue);
342 return new «genTO.name»(«Empty.importedName».getInstance());
343 «ELSEIF allProperties.size > 1»
345 «ELSEIF BOOLEAN.equals(prop.returnType)»
346 return new «genTO.name»(«Boolean.importedName».valueOf(defaultValue));
347 «ELSEIF "java.lang.Byte".equals(prop.returnType.fullyQualifiedName)»
348 return new «genTO.name»(«Byte.importedName».valueOf(defaultValue));
349 «ELSEIF "java.lang.Short".equals(prop.returnType.fullyQualifiedName)»
350 return new «genTO.name»(«Short.importedName».valueOf(defaultValue));
351 «ELSEIF "java.lang.Integer".equals(prop.returnType.fullyQualifiedName)»
352 return new «genTO.name»(«Integer.importedName».valueOf(defaultValue));
353 «ELSEIF "java.lang.Long".equals(prop.returnType.fullyQualifiedName)»
354 return new «genTO.name»(«Long.importedName».valueOf(defaultValue));
356 return new «genTO.name»(new «prop.returnType.importedName»(defaultValue));
363 def protected bitsArgs() '''
364 «List.importedName»<«String.importedName»> properties = «Lists.importedName».newArrayList(«allProperties.propsAsArgs»);
365 if (!properties.contains(defaultValue)) {
366 throw new «IllegalArgumentException.importedName»("invalid default parameter");
369 return new «genTO.name»(
370 «FOR prop : allProperties SEPARATOR ","»
371 properties.get(i++).equals(defaultValue) ? «Boolean.importedName».TRUE : null
376 def protected propsAsArgs(Iterable<GeneratedProperty> properties) '''
377 «FOR prop : properties SEPARATOR ","»
383 * Template method which generates JAVA class declaration.
385 * @param isInnerClass boolean value which specify if generated class is|isn't inner
386 * @return string with class declaration in JAVA format
388 def protected generateClassDeclaration(boolean isInnerClass) '''
392 ELSEIF (type.abstract)»«
396 ENDIF»class «type.name»«
397 IF (genTO.superType !== null)»«
398 " extends "»«genTO.superType.importedName»«
400 «IF (!type.implements.empty)»«
402 FOR type : type.implements SEPARATOR ", "»«
409 * Template method which generates JAVA enum type.
411 * @return string with inner enum source code in JAVA format
413 def protected enumDeclarations() '''
415 «FOR e : enums SEPARATOR "\n"»
416 «new EnumTemplate(javaType.getEnclosedType(e.identifier), e).generateAsInnerClass»
421 def protected suidDeclaration() '''
422 «IF genTO.SUID !== null»
423 private static final long serialVersionUID = «genTO.SUID.value»L;
427 def protected annotationDeclaration() '''
428 «IF genTO.getAnnotations !== null»
429 «FOR e : genTO.getAnnotations»
436 * Template method which generates JAVA constants.
438 * @return string with constants in JAVA format
440 def protected constantsDeclarations() '''
443 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
444 «val cValue = c.value as Map<String, String>»
445 public static final «List.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «ImmutableList.importedName».of(«
446 FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»);
447 «IF cValue.size == 1»
448 private static final «Pattern.importedName» «Constants.MEMBER_PATTERN_LIST» = «Pattern.importedName».compile(«TypeConstants.PATTERN_CONSTANT_NAME».get(0));
449 private static final String «Constants.MEMBER_REGEX_LIST» = "«cValue.values.get(0).escapeJava»";
451 private static final «Pattern.importedName»[] «Constants.MEMBER_PATTERN_LIST» = «CodeHelpers.importedName».compilePatterns(«TypeConstants.PATTERN_CONSTANT_NAME»);
452 private static final String[] «Constants.MEMBER_REGEX_LIST» = { «
453 FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
463 * Template method which generates JAVA class attributes.
465 * @return string with the class attributes in JAVA format
467 def protected generateFields() '''
468 «IF !properties.empty»
470 private«IF isReadOnly(f)» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
475 protected def isReadOnly(GeneratedProperty field) {
476 return field.readOnly
480 * Template method which generates the method <code>hashCode()</code>.
482 * @return string with the <code>hashCode()</code> method definition in JAVA format
484 def protected generateHashCode() {
485 val size = genTO.hashCodeIdentifiers.size
490 @«Override.importedName»
491 public int hashCode() {
493 «hashCodeResult(genTO.hashCodeIdentifiers)»
496 return «CodeHelpers.importedName».wrapperHashCode(«genTO.hashCodeIdentifiers.get(0).fieldName»);
503 * Template method which generates the method <code>equals()</code>.
505 * @return string with the <code>equals()</code> method definition in JAVA format
507 def protected generateEquals() '''
508 «IF !genTO.equalsIdentifiers.empty»
509 @«Override.importedName»
510 public boolean equals(java.lang.Object obj) {
517 if (getClass() != obj.getClass()) {
520 «type.name» other = («type.name») obj;
521 «FOR property : genTO.equalsIdentifiers»
522 «val fieldName = property.fieldName»
523 if (!«property.importedUtilClass».equals(«fieldName», other.«fieldName»)) {
532 def GeneratedProperty getPropByName(String name) {
533 for (GeneratedProperty prop : allProperties) {
534 if (prop.name.equals(name)) {