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 extension org.apache.commons.text.StringEscapeUtils.escapeJava
11 import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.DATA_OBJECT
12 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
13 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
14 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME
16 import com.google.common.collect.ImmutableList
17 import com.google.common.collect.ImmutableSet
18 import com.google.common.collect.Sets
19 import java.util.ArrayList
20 import java.util.Collection
21 import java.util.HashSet
25 import org.opendaylight.mdsal.binding.model.api.AnnotationType
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.GeneratedType
29 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
30 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
31 import org.opendaylight.mdsal.binding.model.api.ParameterizedType
32 import org.opendaylight.mdsal.binding.model.api.Type
33 import org.opendaylight.mdsal.binding.model.ri.TypeConstants
34 import org.opendaylight.mdsal.binding.model.ri.Types
35 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
38 * Template for generating JAVA builder classes.
40 class BuilderTemplate extends AbstractBuilderTemplate {
41 val BuilderImplTemplate implTemplate
44 * Constructs new instance of this class.
45 * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
47 new(GeneratedType genType, GeneratedType targetType, Type keyType) {
48 super(genType, targetType, keyType)
49 implTemplate = new BuilderImplTemplate(this, type.enclosedTypes.get(0))
52 override isLocalInnerClass(JavaTypeName name) {
53 // Builders do not have inner types
58 * Template method which generates JAVA class body for builder class and for IMPL class.
60 * @return string with JAVA source code
63 «wrapToDocumentation(formatDataForJavaDoc(targetType))»
64 «targetType.annotations.generateDeprecatedAnnotation»
66 public class «type.name» {
68 «generateFields(false)»
70 «constantsDeclarations()»
72 «IF augmentType !== null»
73 «val augmentTypeRef = augmentType.importedName»
74 «val mapTypeRef = JU_MAP.importedName»
75 «mapTypeRef»<«CLASS.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> «AUGMENTATION_FIELD» = «mapTypeRef».of();
79 * Construct an empty builder.
81 public «type.name»() {
85 «generateConstructorsFromIfcs()»
87 «val targetTypeName = targetType.importedName»
89 * Construct a builder initialized with state from specified {@link «targetTypeName»}.
91 * @param base «targetTypeName» from which the builder should be initialized
93 public «generateCopyConstructor(targetType, type.enclosedTypes.get(0))»
95 «generateMethodFieldsFrom()»
97 «generateGetters(false)»
98 «IF augmentType !== null»
100 «generateAugmentation()»
106 * A new {@link «targetTypeName»} instance.
108 * @return A new {@link «targetTypeName»} instance.
110 public «targetType.importedNonNull» build() {
111 return new «type.enclosedTypes.get(0).importedName»(this);
118 override generateDeprecatedAnnotation(AnnotationType ann) {
119 val forRemoval = ann.getParameter("forRemoval")
120 if (forRemoval !== null) {
121 return "@" + DEPRECATED.importedName + "(forRemoval = " + forRemoval.value + ")"
123 return "@" + SUPPRESS_WARNINGS.importedName + "(\"deprecation\")"
127 * Generate default constructor and constructor for every implemented interface from uses statements.
129 def private generateConstructorsFromIfcs() '''
130 «IF (!(targetType instanceof GeneratedTransferObject))»
131 «FOR impl : targetType.implements SEPARATOR "\n"»
132 «generateConstructorFromIfc(impl)»
138 * Generate constructor with argument of given type.
140 def private Object generateConstructorFromIfc(Type impl) '''
141 «IF (impl instanceof GeneratedType)»
142 «IF impl.hasNonDefaultMethods»
143 «val typeName = impl.importedName»
145 * Construct a new builder initialized from specified {@link «typeName»}.
147 * @param arg «typeName» from which the builder should be initialized
149 public «type.name»(«typeName» arg) {
150 «printConstructorPropertySetter(impl)»
154 «FOR implTypeImplement : impl.implements»
155 «generateConstructorFromIfc(implTypeImplement)»
160 def private Object printConstructorPropertySetter(Type implementedIfc) '''
161 «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
162 «val ifc = implementedIfc as GeneratedType»
163 «FOR getter : ifc.nonDefaultMethods»
164 «IF BindingMapping.isGetterMethodName(getter.name)»
165 «val propertyName = getter.propertyNameFromGetter»
166 «printPropertySetter(getter, '''arg.«getter.name»()''', propertyName)»;
169 «FOR impl : ifc.implements»
170 «printConstructorPropertySetter(impl, getSpecifiedGetters(ifc))»
175 def private Object printConstructorPropertySetter(Type implementedIfc, Set<MethodSignature> alreadySetProperties) '''
176 «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
177 «val ifc = implementedIfc as GeneratedType»
178 «FOR getter : ifc.nonDefaultMethods»
179 «IF BindingMapping.isGetterMethodName(getter.name) && getterByName(alreadySetProperties, getter.name).isEmpty»
180 «val propertyName = getter.propertyNameFromGetter»
181 «printPropertySetter(getter, '''arg.«getter.name»()''', propertyName)»;
184 «FOR descendant : ifc.implements»
185 «printConstructorPropertySetter(descendant, Sets.union(alreadySetProperties, getSpecifiedGetters(ifc)))»
190 def static Set<MethodSignature> getSpecifiedGetters(GeneratedType type) {
191 val ImmutableSet.Builder<MethodSignature> setBuilder = new ImmutableSet.Builder
192 for (MethodSignature method : type.getMethodDefinitions()) {
193 if (method.hasOverrideAnnotation) {
194 setBuilder.add(method)
197 return setBuilder.build()
201 * Generate 'fieldsFrom' method to set builder properties based on type of given argument.
203 def private generateMethodFieldsFrom() '''
204 «IF (!(targetType instanceof GeneratedTransferObject))»
205 «IF targetType.hasImplementsFromUses»
206 «val List<Type> done = targetType.getBaseIfcs»
207 «generateMethodFieldsFromComment(targetType)»
208 public void fieldsFrom(«DATA_OBJECT.importedName» arg) {
209 boolean isValidArg = false;
210 «FOR impl : targetType.getAllIfcs»
211 «generateIfCheck(impl, done)»
213 «CODEHELPERS.importedName».validValue(isValidArg, arg, "«targetType.getAllIfcs.toListOfNames»");
219 def private generateMethodFieldsFromComment(GeneratedType type) '''
221 * Set fields from given grouping argument. Valid argument is instance of one of following types:
223 «FOR impl : type.getAllIfcs»
224 * <li>{@link «impl.importedName»}</li>
228 * @param arg grouping object
229 * @throws «IAE.importedName» if given argument is none of valid types or has property with incompatible value
234 * Method is used to find out if given type implements any interface from uses.
236 def boolean hasImplementsFromUses(GeneratedType type) {
238 for (impl : type.getAllIfcs) {
239 if (impl instanceof GeneratedType && (impl as GeneratedType).hasNonDefaultMethods) {
246 def private generateIfCheck(Type impl, List<Type> done) '''
247 «IF (impl instanceof GeneratedType && (impl as GeneratedType).hasNonDefaultMethods)»
248 «val implType = impl as GeneratedType»
249 if (arg instanceof «implType.importedName» castArg) {
250 «printPropertySetter(implType)»
256 def private printPropertySetter(Type implementedIfc) '''
257 «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
258 «val ifc = implementedIfc as GeneratedType»
259 «FOR getter : ifc.nonDefaultMethods»
260 «IF BindingMapping.isGetterMethodName(getter.name) && !hasOverrideAnnotation(getter)»
261 «printPropertySetter(getter, '''castArg.«getter.name»()''', getter.propertyNameFromGetter)»;
267 def private printPropertySetter(MethodSignature getter, String retrieveProperty, String propertyName) {
268 val ownGetter = implTemplate.findGetter(getter.name)
269 val ownGetterType = ownGetter.returnType
270 if (Types.strictTypeEquals(getter.returnType, ownGetterType)) {
271 return "this._" + propertyName + " = " + retrieveProperty
273 if (ownGetterType instanceof ParameterizedType) {
274 val itemType = ownGetterType.actualTypeArguments.get(0)
275 if (Types.isListType(ownGetterType)) {
276 return printPropertySetter(retrieveProperty, propertyName, "checkListFieldCast", itemType.importedName)
278 if (Types.isSetType(ownGetterType)) {
279 return printPropertySetter(retrieveProperty, propertyName, "checkSetFieldCast", itemType.importedName)
282 return printPropertySetter(retrieveProperty, propertyName, "checkFieldCast", ownGetterType.importedName)
285 def private printPropertySetter(String retrieveProperty, String propertyName, String checkerName, String className) '''
286 this._«propertyName» = «CODEHELPERS.importedName».«checkerName»(«className».class, "«propertyName»", «retrieveProperty»)'''
288 private def List<Type> getBaseIfcs(GeneratedType type) {
289 val List<Type> baseIfcs = new ArrayList();
290 for (ifc : type.implements) {
291 if (ifc instanceof GeneratedType && (ifc as GeneratedType).hasNonDefaultMethods) {
298 private def Set<Type> getAllIfcs(Type type) {
299 val Set<Type> baseIfcs = new HashSet()
300 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
301 val ifc = type as GeneratedType
302 for (impl : ifc.implements) {
303 if (impl instanceof GeneratedType && (impl as GeneratedType).hasNonDefaultMethods) {
306 baseIfcs.addAll(impl.getAllIfcs)
312 private def List<String> toListOfNames(Collection<Type> types) {
313 val List<String> names = new ArrayList
315 names.add(type.importedName)
320 def private constantsDeclarations() '''
321 «FOR c : type.getConstantDefinitions»
322 «IF c.getName.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
323 «val cValue = c.value as Map<String, String>»
324 «val String fieldSuffix = c.getName.substring(TypeConstants.PATTERN_CONSTANT_NAME.length)»
325 «val jurPatternRef = JUR_PATTERN.importedName»
326 «IF cValue.size == 1»
327 «val firstEntry = cValue.entrySet.iterator.next»
328 private static final «jurPatternRef» «Constants.MEMBER_PATTERN_LIST»«fieldSuffix» = «jurPatternRef».compile("«firstEntry.key.escapeJava»");
329 private static final String «Constants.MEMBER_REGEX_LIST»«fieldSuffix» = "«firstEntry.value.escapeJava»";
331 private static final «jurPatternRef»[] «Constants.MEMBER_PATTERN_LIST»«fieldSuffix» = «CODEHELPERS.importedName».compilePatterns(«ImmutableList.importedName».of(
332 «FOR v : cValue.keySet SEPARATOR ", "»"«v.escapeJava»"«ENDFOR»));
333 private static final String[] «Constants.MEMBER_REGEX_LIST»«fieldSuffix» = { «
334 FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
342 def private generateSetter(BuilderGeneratedProperty field) {
343 val returnType = field.returnType
344 if (returnType instanceof ParameterizedType) {
345 if (Types.isListType(returnType) || Types.isSetType(returnType)) {
346 val arguments = returnType.actualTypeArguments
347 if (arguments.isEmpty) {
348 return generateListSetter(field, Types.objectType)
350 return generateListSetter(field, arguments.get(0))
351 } else if (Types.isMapType(returnType)) {
352 return generateMapSetter(field, returnType.actualTypeArguments.get(1))
355 return generateSimpleSetter(field, returnType)
358 def private generateListSetter(BuilderGeneratedProperty field, Type actualType) '''
359 «val restrictions = restrictionsForSetter(actualType)»
360 «IF restrictions !== null»
361 «generateCheckers(field, restrictions, actualType)»
365 * Set the property corresponding to {@link «targetType.importedName»#«field.getterName»()} to the specified
368 * @param values desired value
369 * @return this builder
371 public «type.getName» set«field.getName.toFirstUpper»(final «field.returnType.importedName» values) {
372 «IF restrictions !== null»
373 if (values != null) {
374 for («actualType.importedName» value : values) {
375 «checkArgument(field, restrictions, actualType, "value")»
379 this.«field.fieldName» = values;
385 def private generateMapSetter(BuilderGeneratedProperty field, Type actualType) '''
386 «val restrictions = restrictionsForSetter(actualType)»
387 «IF restrictions !== null»
388 «generateCheckers(field, restrictions, actualType)»
392 * Set the property corresponding to {@link «targetType.importedName»#«field.getterName»()} to the specified
395 * @param values desired value
396 * @return this builder
398 public «type.getName» set«field.name.toFirstUpper»(final «field.returnType.importedName» values) {
399 «IF restrictions !== null»
400 if (values != null) {
401 for («actualType.importedName» value : values.values()) {
402 «checkArgument(field, restrictions, actualType, "value")»
406 this.«field.fieldName» = values;
411 def private generateSimpleSetter(BuilderGeneratedProperty field, Type actualType) '''
412 «val restrictions = restrictionsForSetter(actualType)»
413 «IF restrictions !== null»
415 «generateCheckers(field, restrictions, actualType)»
419 * Set the property corresponding to {@link «targetType.importedName»#«field.getterName»()} to the specified
422 * @param value desired value
423 * @return this builder
425 «val setterName = "set" + field.getName.toFirstUpper»
426 public «type.getName» «setterName»(final «field.returnType.importedName» value) {
427 «IF restrictions !== null»
429 «checkArgument(field, restrictions, actualType, "value")»
432 this.«field.fieldName» = value;
438 * Template method which generates setter methods
440 * @return string with the setter methods
442 def private generateSetters() '''
443 «IF keyType !== null»
444 public «type.getName» withKey(final «keyType.importedName» key) {
449 «FOR property : properties»
450 «generateSetter(property)»
453 «IF augmentType !== null»
454 «val augmentTypeRef = augmentType.importedName»
455 «val hashMapRef = JU_HASHMAP.importedName»
457 * Add an augmentation to this builder's product.
459 * @param augmentation augmentation to be added
460 * @return this builder
461 * @throws «NPE.importedName» if {@code augmentation} is null
463 public «type.name» addAugmentation(«augmentTypeRef» augmentation) {
464 if (!(this.«AUGMENTATION_FIELD» instanceof «hashMapRef»)) {
465 this.«AUGMENTATION_FIELD» = new «hashMapRef»<>();
468 this.«AUGMENTATION_FIELD».put(augmentation.«BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»(), augmentation);
473 * Remove an augmentation from this builder's product. If this builder does not track such an augmentation
474 * type, this method does nothing.
476 * @param augmentationType augmentation type to be removed
477 * @return this builder
479 public «type.name» removeAugmentation(«CLASS.importedName»<? extends «augmentTypeRef»> augmentationType) {
480 if (this.«AUGMENTATION_FIELD» instanceof «hashMapRef») {
481 this.«AUGMENTATION_FIELD».remove(augmentationType);
488 private def createDescription(GeneratedType targetType) {
489 val target = targetType.importedName
491 Class that builds {@link «target»} instances. Overall design of the class is that of a
492 <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a>, where method chaining is used.
495 In general, this class is supposed to be used like this template:
498 «target» create«target»(int fooXyzzy, int barBaz) {
499 return new «target»Builder()
500 .setFoo(new FooBuilder().setXyzzy(fooXyzzy).build())
501 .setBar(new BarBuilder().setBaz(barBaz).build())
508 This pattern is supported by the immutable nature of «target», as instances can be freely passed around without
509 worrying about synchronization issues.
512 As a side note: method chaining results in:
514 <li>very efficient Java bytecode, as the method invocation result, in this case the Builder reference, is
515 on the stack, so further method invocations just need to fill method arguments for the next method
516 invocation, which is terminated by {@link #build()}, which is then returned from the method</li>
517 <li>better understanding by humans, as the scope of mutable state (the builder) is kept to a minimum and is
519 <li>better optimization opportunities, as the object scope is minimized in terms of invocation (rather than
520 method) stack, making <a href="https://en.wikipedia.org/wiki/Escape_analysis">escape analysis</a> a lot
521 easier. Given enough compiler (JIT/AOT) prowess, the cost of th builder object can be completely
529 override protected String formatDataForJavaDoc(GeneratedType type) {
530 val typeDescription = createDescription(type)
533 «IF !typeDescription.nullOrEmpty»
539 private def generateAugmentation() '''
541 * Return the specified augmentation, if it is present in this builder.
543 * @param <E$$> augmentation type
544 * @param augmentationType augmentation type class
545 * @return Augmentation object from this builder, or {@code null} if not present
546 * @throws «NPE.importedName» if {@code augmentType} is {@code null}
548 @«SUPPRESS_WARNINGS.importedName»({ "unchecked", "checkstyle:methodTypeParameterName"})
549 public <E$$ extends «augmentType.importedName»> E$$ «AUGMENTABLE_AUGMENTATION_NAME»(«CLASS.importedName»<E$$> augmentationType) {
550 return (E$$) «AUGMENTATION_FIELD».get(«JU_OBJECTS.importedName».requireNonNull(augmentationType));
554 override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
555 this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
556 «FOR field : keyProps»
557 this.«field.fieldName» = base.«field.getterMethodName»();
561 override protected CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props) '''
563 this.«field.fieldName» = base.«field.getterName»();
567 override protected generateCopyAugmentation(Type implType) '''
568 final var aug = base.augmentations();
569 if (!aug.isEmpty()) {
570 this.«AUGMENTATION_FIELD» = new «JU_HASHMAP.importedName»<>(aug);