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.yangtools.sal.java.api.generator
10 import java.util.Arrays;
11 import java.util.LinkedHashSet
15 import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl
16 import org.opendaylight.yangtools.binding.generator.util.Types
17 import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl
18 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
19 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
21 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature
22 import org.opendaylight.yangtools.sal.binding.model.api.Type
23 import org.opendaylight.yangtools.yang.binding.Augmentable
24 import static org.opendaylight.yangtools.binding.generator.util.Types.*
25 import java.util.HashMap
26 import java.util.Collections
27 import org.opendaylight.yangtools.yang.binding.DataObject
28 import java.util.ArrayList
29 import java.util.HashSet
30 import java.util.Collection
31 import org.opendaylight.yangtools.yang.binding.Identifiable
34 * Template for generating JAVA builder classes.
37 class BuilderTemplate extends BaseTemplate {
40 * Constant with the name of the concrete method.
42 val static GET_AUGMENTATION_METHOD_NAME = "getAugmentation"
45 * Constant with the suffix for builder classes.
47 val static BUILDER = 'Builder'
50 * Constant with suffix for the classes which are generated from the builder classes.
52 val static IMPL = 'Impl'
55 * Generated property is set if among methods is found one with the name GET_AUGMENTATION_METHOD_NAME
57 var GeneratedProperty augmentField
60 * Set of class attributes (fields) which are derived from the getter methods names
62 val Set<GeneratedProperty> properties
65 * Constructs new instance of this class.
66 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
68 new(GeneratedType genType) {
70 this.properties = propertiesFromMethods(createMethods)
74 * Returns set of method signature instances which contains all the methods of the <code>genType</code>
75 * and all the methods of the implemented interfaces.
77 * @returns set of method signature instances
79 def private Set<MethodSignature> createMethods() {
80 val Set<MethodSignature> methods = new LinkedHashSet
81 methods.addAll(type.methodDefinitions)
82 collectImplementedMethods(methods, type.implements)
87 * Adds to the <code>methods</code> set all the methods of the <code>implementedIfcs</code>
88 * and recursivelly their implemented interfaces.
90 * @param methods set of method signatures
91 * @param implementedIfcs list of implemented interfaces
93 def private void collectImplementedMethods(Set<MethodSignature> methods, List<Type> implementedIfcs) {
94 if (implementedIfcs == null || implementedIfcs.empty) {
97 for (implementedIfc : implementedIfcs) {
98 if ((implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))) {
99 val ifc = implementedIfc as GeneratedType
100 methods.addAll(ifc.methodDefinitions)
101 collectImplementedMethods(methods, ifc.implements)
102 } else if (implementedIfc.fullyQualifiedName == Augmentable.name) {
103 for (m : Augmentable.methods) {
104 if (m.name == GET_AUGMENTATION_METHOD_NAME) {
105 val fullyQualifiedName = m.returnType.name
106 val pkg = fullyQualifiedName.package
107 val name = fullyQualifiedName.name
108 val tmpGenTO = new GeneratedTOBuilderImpl(pkg, name)
109 val refType = new ReferencedTypeImpl(pkg, name)
110 val generic = new ReferencedTypeImpl(type.packageName, type.name)
111 val parametrizedReturnType = Types.parameterizedTypeFor(refType, generic)
112 tmpGenTO.addMethod(m.name).setReturnType(parametrizedReturnType)
113 augmentField = tmpGenTO.toInstance.methodDefinitions.first.propertyFromGetter
121 * Returns the first element of the list <code>elements</code>.
123 * @param list of elements
125 def private <E> first(List<E> elements) {
130 * Returns the name of the package from <code>fullyQualifiedName</code>.
132 * @param fullyQualifiedName string with fully qualified type name (package + type)
133 * @return string with the package name
135 def private String getPackage(String fullyQualifiedName) {
136 val lastDotIndex = fullyQualifiedName.lastIndexOf(Constants.DOT)
137 return if (lastDotIndex == -1) "" else fullyQualifiedName.substring(0, lastDotIndex)
141 * Returns the name of tye type from <code>fullyQualifiedName</code>
143 * @param fullyQualifiedName string with fully qualified type name (package + type)
144 * @return string with the name of the type
146 def private String getName(String fullyQualifiedName) {
147 val lastDotIndex = fullyQualifiedName.lastIndexOf(Constants.DOT)
148 return if (lastDotIndex == -1) fullyQualifiedName else fullyQualifiedName.substring(lastDotIndex + 1)
152 * Creates set of generated property instances from getter <code>methods</code>.
154 * @param set of method signature instances which should be transformed to list of properties
155 * @return set of generated property instances which represents the getter <code>methods</code>
157 def private propertiesFromMethods(Set<MethodSignature> methods) {
158 if (methods == null || methods.isEmpty()) {
159 return Collections.emptySet
161 val Set<GeneratedProperty> result = new LinkedHashSet
163 val createdField = m.propertyFromGetter
164 if (createdField != null) {
165 result.add(createdField)
172 * Creates generated property instance from the getter <code>method</code> name and return type.
174 * @param method method signature from which is the method name and return type obtained
175 * @return generated property instance for the getter <code>method</code>
176 * @throws IllegalArgumentException<ul>
177 * <li>if the <code>method</code> equals <code>null</code></li>
178 * <li>if the name of the <code>method</code> equals <code>null</code></li>
179 * <li>if the name of the <code>method</code> is empty</li>
180 * <li>if the return type of the <code>method</code> equals <code>null</code></li>
183 def private GeneratedProperty propertyFromGetter(MethodSignature method) {
184 if (method == null || method.name == null || method.name.empty || method.returnType == null) {
185 throw new IllegalArgumentException("Method, method name, method return type reference cannot be NULL or empty!")
188 if(BOOLEAN.equals(method.returnType)) {
191 if (method.name.startsWith(prefix)) {
192 val fieldName = method.getName().substring(prefix.length()).toFirstLower
193 val tmpGenTO = new GeneratedTOBuilderImpl("foo", "foo")
194 tmpGenTO.addProperty(fieldName).setReturnType(method.returnType)
195 return tmpGenTO.toInstance.properties.first
200 * Template method which generates JAVA class body for builder class and for IMPL class.
202 * @return string with JAVA source code
206 public class «type.name»«BUILDER» {
208 «generateFields(false)»
210 «generateConstructorsFromIfcs(type)»
212 «generateMethodFieldsFrom(type)»
214 «generateGetters(false)»
218 public «type.name» build() {
219 return new «type.name»«IMPL»(this);
222 private static final class «type.name»«IMPL» implements «type.name» {
224 «implementedInterfaceGetter»
226 «generateFields(true)»
228 «generateConstructor»
230 «generateGetters(true)»
236 «generateToString(properties)»
243 * Generate default constructor and constructor for every implemented interface from uses statements.
245 def private generateConstructorsFromIfcs(Type type) '''
246 public «type.name»«BUILDER»() {
248 «IF (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject))»
249 «val ifc = type as GeneratedType»
250 «FOR impl : ifc.implements»
251 «generateConstructorFromIfc(impl)»
257 * Generate constructor with argument of given type.
259 def private generateConstructorFromIfc(Type impl) '''
260 «IF (impl instanceof GeneratedType)»
261 «val implType = impl as GeneratedType»
263 «IF !(implType.methodDefinitions.empty)»
264 public «type.name»«BUILDER»(«implType.fullyQualifiedName» arg) {
265 «printConstructorPropertySetter(implType)»
268 «FOR implTypeImplement : implType.implements»
269 «generateConstructorFromIfc(implTypeImplement)»
274 def private printConstructorPropertySetter(Type implementedIfc) '''
275 «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
276 «val ifc = implementedIfc as GeneratedType»
277 «FOR getter : ifc.methodDefinitions»
278 this._«getter.propertyNameFromGetter» = arg.«getter.name»();
280 «FOR impl : ifc.implements»
281 «printConstructorPropertySetter(impl)»
287 * Generate 'fieldsFrom' method to set builder properties based on type of given argument.
289 def private generateMethodFieldsFrom(Type type) '''
290 «IF (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject))»
291 «val ifc = type as GeneratedType»
292 «IF ifc.hasImplementsFromUses»
293 «val List<Type> done = ifc.getBaseIfcs»
294 «generateMethodFieldsFromComment(ifc)»
295 public void fieldsFrom(«DataObject.importedName» arg) {
296 boolean isValidArg = false;
297 «FOR impl : ifc.getAllIfcs»
298 «generateIfCheck(impl, done)»
301 throw new IllegalArgumentException(
302 "expected one of: «ifc.getAllIfcs.toListOfNames» \n" +
311 def private generateMethodFieldsFromComment(GeneratedType type) '''
313 Set fields from given grouping argument. Valid argument is instance of one of following types:
315 «FOR impl : type.getAllIfcs»
316 * <li>«impl.fullyQualifiedName»</li>
320 * @param arg grouping object
321 * @throws IllegalArgumentException if given argument is none of valid types
326 * Method is used to find out if given type implements any interface from uses.
328 def boolean hasImplementsFromUses(GeneratedType type) {
330 for (impl : type.getAllIfcs) {
331 if ((impl instanceof GeneratedType) && !((impl as GeneratedType).methodDefinitions.empty)) {
338 def private generateIfCheck(Type impl, List<Type> done) '''
339 «IF (impl instanceof GeneratedType) && !((impl as GeneratedType).methodDefinitions.empty)»
340 «val implType = impl as GeneratedType»
341 if (arg instanceof «implType.fullyQualifiedName») {
342 «printPropertySetter(implType)»
348 def private printPropertySetter(Type implementedIfc) '''
349 «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
350 «val ifc = implementedIfc as GeneratedType»
351 «FOR getter : ifc.methodDefinitions»
352 this._«getter.propertyNameFromGetter» = ((«implementedIfc.fullyQualifiedName»)arg).«getter.name»();
357 private def List<Type> getBaseIfcs(GeneratedType type) {
358 val List<Type> baseIfcs = new ArrayList();
359 for (ifc : type.implements) {
360 if (ifc instanceof GeneratedType && !(ifc as GeneratedType).methodDefinitions.empty) {
367 private def Set<Type> getAllIfcs(Type type) {
368 val Set<Type> baseIfcs = new HashSet()
369 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
370 val ifc = type as GeneratedType
371 for (impl : ifc.implements) {
372 if (impl instanceof GeneratedType && !(impl as GeneratedType).methodDefinitions.empty) {
375 baseIfcs.addAll(impl.getAllIfcs)
381 private def List<String> toListOfNames(Collection<Type> types) {
382 val List<String> names = new ArrayList
384 names.add(type.fullyQualifiedName)
390 * Template method which generates class attributes.
392 * @param boolean value which specify whether field is|isn't final
393 * @return string with class attributes and their types
395 def private generateFields(boolean _final) '''
396 «IF !properties.empty»
398 private«IF _final» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
401 «IF augmentField != null»
402 private «Map.importedName»<Class<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> «augmentField.name» = new «HashMap.importedName»<>();
407 * Template method which generates setter methods
409 * @return string with the setter methods
411 def private generateSetters() '''
412 «FOR field : properties SEPARATOR '\n'»
413 public «type.name»«BUILDER» set«field.name.toFirstUpper»(«field.returnType.importedName» value) {
414 «generateRestrictions(field, "value")»
416 this.«field.fieldName» = value;
420 «IF augmentField != null»
422 public «type.name»«BUILDER» add«augmentField.name.toFirstUpper»(Class<? extends «augmentField.returnType.importedName»> augmentationType, «augmentField.returnType.importedName» augmentation) {
423 this.«augmentField.name».put(augmentationType, augmentation);
430 * Template method which generate constructor for IMPL class.
432 * @return string with IMPL class constructor
434 def private generateConstructor() '''
435 private «type.name»«IMPL»(«type.name»«BUILDER» builder) {
436 «val allProps = new ArrayList(properties)»
437 «val isList = implementsIfc(type, Types.parameterizedTypeFor(Types.typeForClass(Identifiable), type))»
438 «val keyType = type.getKey»
439 «IF isList && keyType != null»
440 «val keyProps = new ArrayList((keyType as GeneratedTransferObject).properties)»
441 «Collections.sort(keyProps,
443 return p1.name.compareTo(p2.name)
446 «FOR field : keyProps»
447 «removeProperty(allProps, field.name)»
449 «removeProperty(allProps, "key")»
450 if (builder.getKey() == null) {
451 this._key = new «keyType.importedName»(
452 «FOR keyProp : keyProps SEPARATOR ", "»
453 builder.«keyProp.getterMethodName»()
456 «FOR field : keyProps»
457 this.«field.fieldName» = builder.«field.getterMethodName»();
460 this._key = builder.getKey();
461 «FOR field : keyProps»
462 this.«field.fieldName» = _key.«field.getterMethodName»();
466 «FOR field : allProps»
467 this.«field.fieldName» = builder.«field.getterMethodName»();
469 «IF augmentField != null»
470 this.«augmentField.name».putAll(builder.«augmentField.name»);
475 private def boolean implementsIfc(GeneratedType type, Type impl) {
476 for (Type ifc : type.implements) {
477 if (ifc.equals(impl)) {
484 private def Type getKey(GeneratedType type) {
485 for (m : type.methodDefinitions) {
486 if ("getKey".equals(m.name)) {
493 private def void removeProperty(Collection<GeneratedProperty> props, String name) {
494 var GeneratedProperty toRemove = null
496 if (p.name.equals(name)) {
500 if (toRemove != null) {
501 props.remove(toRemove);
506 * Template method which generate getter methods for IMPL class.
508 * @return string with getter methods
510 def private generateGetters(boolean addOverride) '''
511 «IF !properties.empty»
512 «FOR field : properties SEPARATOR '\n'»
513 «IF addOverride»@Override«ENDIF»
517 «IF augmentField != null»
519 @SuppressWarnings("unchecked")
520 «IF addOverride»@Override«ENDIF»
521 public <E extends «augmentField.returnType.importedName»> E get«augmentField.name.toFirstUpper»(Class<E> augmentationType) {
522 if (augmentationType == null) {
523 throw new IllegalArgumentException("Augmentation Type reference cannot be NULL!");
525 return (E) «augmentField.name».get(augmentationType);
531 * Template method which generates the method <code>hashCode()</code>.
533 * @return string with the <code>hashCode()</code> method definition in JAVA format
535 def protected generateHashCode() '''
536 «IF !properties.empty || augmentField != null»
538 public int hashCode() {
539 final int prime = 31;
541 «FOR property : properties»
542 «IF property.returnType.name.contains("[")»
543 result = prime * result + ((«property.fieldName» == null) ? 0 : «Arrays.importedName».hashCode(«property.fieldName»));
545 result = prime * result + ((«property.fieldName» == null) ? 0 : «property.fieldName».hashCode());
548 «IF augmentField != null»
549 result = prime * result + ((«augmentField.name» == null) ? 0 : «augmentField.name».hashCode());
557 * Template method which generates the method <code>equals()</code>.
559 * @return string with the <code>equals()</code> method definition in JAVA format
561 def protected generateEquals() '''
562 «IF !properties.empty || augmentField != null»
564 public boolean equals(java.lang.Object obj) {
571 if (getClass() != obj.getClass()) {
574 «type.name»«IMPL» other = («type.name»«IMPL») obj;
575 «FOR property : properties»
576 «val fieldName = property.fieldName»
577 if («fieldName» == null) {
578 if (other.«fieldName» != null) {
581 «IF property.returnType.name.contains("[")»
582 } else if(!«Arrays.importedName».equals(«fieldName», other.«fieldName»)) {
584 } else if(!«fieldName».equals(other.«fieldName»)) {
589 «IF augmentField != null»
590 «val fieldName = augmentField.name»
591 if («fieldName» == null) {
592 if (other.«fieldName» != null) {
595 } else if(!«fieldName».equals(other.«fieldName»)) {
604 def override generateToString(Collection<GeneratedProperty> properties) '''
605 «IF !(properties === null)»
607 public String toString() {
608 StringBuilder builder = new StringBuilder("«type.name» [");
609 boolean first = true;
611 «FOR property : properties»
612 if («property.fieldName» != null) {
616 builder.append(", ");
618 builder.append("«property.fieldName»=");
619 «IF property.returnType.name.contains("[")»
620 builder.append(«Arrays.importedName».toString(«property.fieldName»));
622 builder.append(«property.fieldName»);
626 «IF augmentField != null»
630 builder.append(", ");
632 builder.append("«augmentField.name»=");
633 builder.append(«augmentField.name».values());
635 return builder.append(']').toString();
640 override protected getFullyQualifiedName() {
641 '''«type.fullyQualifiedName»Builder'''.toString
644 def implementedInterfaceGetter() '''
645 public «Class.importedName»<«type.importedName»> getImplementedInterface() {
646 return «type.importedName».class;