X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fjava%2Fapi%2Fgenerator%2FBuilderTemplate.xtend;h=7dfd93a55154be5201a35d9904e9ed7052af6b74;hb=f735dcbc86962f6b51abecadec0c00f595649984;hp=bd844616df4382e4620de3219595c8c253492d23;hpb=50f1c9414bd781441978237545e44d84c6f53b5d;p=mdsal.git diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderTemplate.xtend index bd844616df..7dfd93a551 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderTemplate.xtend @@ -8,12 +8,14 @@ package org.opendaylight.mdsal.binding.java.api.generator import static extension org.apache.commons.text.StringEscapeUtils.escapeJava -import static org.opendaylight.mdsal.binding.model.util.BindingTypes.DATA_OBJECT +import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.DATA_OBJECT import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME import com.google.common.collect.ImmutableList +import com.google.common.collect.ImmutableSet +import com.google.common.collect.Sets import java.util.ArrayList import java.util.Collection import java.util.HashSet @@ -25,23 +27,18 @@ import org.opendaylight.mdsal.binding.model.api.GeneratedProperty import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject import org.opendaylight.mdsal.binding.model.api.GeneratedType import org.opendaylight.mdsal.binding.model.api.JavaTypeName +import org.opendaylight.mdsal.binding.model.api.MethodSignature; import org.opendaylight.mdsal.binding.model.api.ParameterizedType import org.opendaylight.mdsal.binding.model.api.Type -import org.opendaylight.mdsal.binding.model.util.TypeConstants -import org.opendaylight.mdsal.binding.model.util.Types +import org.opendaylight.mdsal.binding.model.ri.TypeConstants +import org.opendaylight.mdsal.binding.model.ri.Types import org.opendaylight.mdsal.binding.spec.naming.BindingMapping -import org.opendaylight.yangtools.concepts.Builder /** * Template for generating JAVA builder classes. */ class BuilderTemplate extends AbstractBuilderTemplate { - /** - * Constant used as suffix for builder name. - */ - package static val BUILDER_STR = "Builder"; - - static val BUILDER = JavaTypeName.create(Builder) + val BuilderImplTemplate implTemplate /** * Constructs new instance of this class. @@ -49,6 +46,7 @@ class BuilderTemplate extends AbstractBuilderTemplate { */ new(GeneratedType genType, GeneratedType targetType, Type keyType) { super(genType, targetType, keyType) + implTemplate = new BuilderImplTemplate(this, type.enclosedTypes.get(0)) } override isLocalInnerClass(JavaTypeName name) { @@ -64,7 +62,8 @@ class BuilderTemplate extends AbstractBuilderTemplate { override body() ''' «wrapToDocumentation(formatDataForJavaDoc(targetType))» «targetType.annotations.generateDeprecatedAnnotation» - public class «type.name» implements «BUILDER.importedName»<«targetType.importedName»> { + «generatedAnnotation» + public class «type.name» { «generateFields(false)» @@ -88,12 +87,16 @@ class BuilderTemplate extends AbstractBuilderTemplate { «generateSetters» - @«OVERRIDE.importedName» + /** + * A new {@link «targetType.name»} instance. + * + * @return A new {@link «targetType.name»} instance. + */ public «targetType.name» build() { return new «type.enclosedTypes.get(0).importedName»(this); } - «new BuilderImplTemplate(this, type.enclosedTypes.get(0)).body» + «implTemplate.body» } ''' @@ -111,8 +114,9 @@ class BuilderTemplate extends AbstractBuilderTemplate { def private generateConstructorsFromIfcs() ''' public «type.name»() { } + «IF (!(targetType instanceof GeneratedTransferObject))» - «FOR impl : targetType.implements» + «FOR impl : targetType.implements SEPARATOR "\n"» «generateConstructorFromIfc(impl)» «ENDFOR» «ENDIF» @@ -124,7 +128,7 @@ class BuilderTemplate extends AbstractBuilderTemplate { def private Object generateConstructorFromIfc(Type impl) ''' «IF (impl instanceof GeneratedType)» «IF impl.hasNonDefaultMethods» - public «type.name»(«impl.fullyQualifiedName» arg) { + public «type.name»(«impl.importedName» arg) { «printConstructorPropertySetter(impl)» } «ENDIF» @@ -139,15 +143,41 @@ class BuilderTemplate extends AbstractBuilderTemplate { «val ifc = implementedIfc as GeneratedType» «FOR getter : ifc.nonDefaultMethods» «IF BindingMapping.isGetterMethodName(getter.name)» - this._«getter.propertyNameFromGetter» = arg.«getter.name»(); + «val propertyName = getter.propertyNameFromGetter» + «printPropertySetter(getter, '''arg.«getter.name»()''', propertyName)»; «ENDIF» «ENDFOR» «FOR impl : ifc.implements» - «printConstructorPropertySetter(impl)» + «printConstructorPropertySetter(impl, getSpecifiedGetters(ifc))» + «ENDFOR» + «ENDIF» + ''' + + def private Object printConstructorPropertySetter(Type implementedIfc, Set alreadySetProperties) ''' + «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))» + «val ifc = implementedIfc as GeneratedType» + «FOR getter : ifc.nonDefaultMethods» + «IF BindingMapping.isGetterMethodName(getter.name) && getterByName(alreadySetProperties, getter.name).isEmpty» + «val propertyName = getter.propertyNameFromGetter» + «printPropertySetter(getter, '''arg.«getter.name»()''', propertyName)»; + «ENDIF» + «ENDFOR» + «FOR descendant : ifc.implements» + «printConstructorPropertySetter(descendant, Sets.union(alreadySetProperties, getSpecifiedGetters(ifc)))» «ENDFOR» «ENDIF» ''' + def static Set getSpecifiedGetters(GeneratedType type) { + val ImmutableSet.Builder setBuilder = new ImmutableSet.Builder + for (MethodSignature method : type.getMethodDefinitions()) { + if (method.hasOverrideAnnotation) { + setBuilder.add(method) + } + } + return setBuilder.build() + } + /** * Generate 'fieldsFrom' method to set builder properties based on type of given argument. */ @@ -172,12 +202,12 @@ class BuilderTemplate extends AbstractBuilderTemplate { * Set fields from given grouping argument. Valid argument is instance of one of following types: *
    «FOR impl : type.getAllIfcs» - *
  • «impl.fullyQualifiedName»
  • + *
  • «impl.importedName»
  • «ENDFOR» *
* * @param arg grouping object - * @throws IllegalArgumentException if given argument is none of valid types + * @throws IllegalArgumentException if given argument is none of valid types or has property with incompatible value */ ''' @@ -197,7 +227,7 @@ class BuilderTemplate extends AbstractBuilderTemplate { def private generateIfCheck(Type impl, List done) ''' «IF (impl instanceof GeneratedType && (impl as GeneratedType).hasNonDefaultMethods)» «val implType = impl as GeneratedType» - if (arg instanceof «implType.fullyQualifiedName») { + if (arg instanceof «implType.importedName») { «printPropertySetter(implType)» isValidArg = true; } @@ -208,13 +238,54 @@ class BuilderTemplate extends AbstractBuilderTemplate { «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))» «val ifc = implementedIfc as GeneratedType» «FOR getter : ifc.nonDefaultMethods» - «IF BindingMapping.isGetterMethodName(getter.name)» - this._«getter.propertyNameFromGetter» = ((«implementedIfc.fullyQualifiedName»)arg).«getter.name»(); + «IF BindingMapping.isGetterMethodName(getter.name) && !hasOverrideAnnotation(getter)» + «printPropertySetter(getter, '''((«ifc.importedName»)arg).«getter.name»()''', getter.propertyNameFromGetter)»; «ENDIF» «ENDFOR» «ENDIF» ''' + def private printPropertySetter(MethodSignature getter, String retrieveProperty, String propertyName) { + val ownGetter = implTemplate.findGetter(getter.name) + val ownGetterType = ownGetter.returnType + if (Types.strictTypeEquals(getter.returnType, ownGetterType)) { + return "this._" + propertyName + " = " + retrieveProperty + } + if (ownGetterType instanceof ParameterizedType) { + val itemType = ownGetterType.actualTypeArguments.get(0) + if (Types.isListType(ownGetterType)) { + val importedClass = importedClass(itemType) + if (importedClass !== null) { + return printPropertySetter(retrieveProperty, propertyName, "checkListFieldCastIdentity", importedClass) + } + return printPropertySetter(retrieveProperty, propertyName, "checkListFieldCast", itemType.importedName) + } + if (Types.isSetType(ownGetterType)) { + val importedClass = importedClass(itemType) + if (importedClass !== null) { + return printPropertySetter(retrieveProperty, propertyName, "checkSetFieldCastIdentity", importedClass) + } + return printPropertySetter(retrieveProperty, propertyName, "checkSetFieldCast", itemType.importedName) + } + if (Types.CLASS.equals(ownGetterType)) { + return printPropertySetter(retrieveProperty, propertyName, "checkFieldCastIdentity", itemType.identifier.importedName) + } + } + return printPropertySetter(retrieveProperty, propertyName, "checkFieldCast", ownGetterType.importedName) + } + + def private printPropertySetter(String retrieveProperty, String propertyName, String checkerName, String className) ''' + this._«propertyName» = «CODEHELPERS.importedName».«checkerName»(«className».class, "«propertyName»", «retrieveProperty»)''' + + private def importedClass(Type type) { + if (type instanceof ParameterizedType) { + if (Types.CLASS.equals(type.rawType)) { + return type.actualTypeArguments.get(0).identifier.importedName + } + } + return null + } + private def List getBaseIfcs(GeneratedType type) { val List baseIfcs = new ArrayList(); for (ifc : type.implements) { @@ -242,7 +313,7 @@ class BuilderTemplate extends AbstractBuilderTemplate { private def List toListOfNames(Collection types) { val List names = new ArrayList for (type : types) { - names.add(type.fullyQualifiedName) + names.add(type.importedName) } return names } @@ -272,8 +343,12 @@ class BuilderTemplate extends AbstractBuilderTemplate { def private generateSetter(GeneratedProperty field) { val returnType = field.returnType if (returnType instanceof ParameterizedType) { - if (Types.isListType(returnType)) { - return generateListSetter(field, returnType.actualTypeArguments.get(0)) + if (Types.isListType(returnType) || Types.isSetType(returnType)) { + val arguments = returnType.actualTypeArguments + if (arguments.isEmpty) { + return generateListSetter(field, Types.objectType) + } + return generateListSetter(field, arguments.get(0)) } else if (Types.isMapType(returnType)) { return generateMapSetter(field, returnType.actualTypeArguments.get(1)) } @@ -300,18 +375,15 @@ class BuilderTemplate extends AbstractBuilderTemplate { ''' - // FIXME: MDSAL-540: remove the migration setter def private generateMapSetter(GeneratedProperty field, Type actualType) ''' «val restrictions = restrictionsForSetter(actualType)» - «val actualTypeRef = actualType.importedName» - «val setterName = "set" + field.name.toFirstUpper» «IF restrictions !== null» «generateCheckers(field, restrictions, actualType)» «ENDIF» - public «type.getName» «setterName»(final «field.returnType.importedName» values) { + public «type.getName» set«field.name.toFirstUpper»(final «field.returnType.importedName» values) { «IF restrictions !== null» if (values != null) { - for («actualTypeRef» value : values.values()) { + for («actualType.importedName» value : values.values()) { «checkArgument(field, restrictions, actualType, "value")» } } @@ -319,24 +391,6 @@ class BuilderTemplate extends AbstractBuilderTemplate { this.«field.fieldName» = values; return this; } - - /** - * Utility migration setter. - * - * IMPORTANT NOTE: This method does not completely match previous mechanics, as the list is processed as - * during this method's execution. Any future modifications of the list are NOT - * reflected in this builder nor its products. - * - * @param values Legacy List of values - * @return this builder - * @throws IllegalArgumentException if the list contains entries with the same key - * @throws NullPointerException if the list contains a null entry - * @deprecated Use {#link #«setterName»(«JU_MAP.importedName»)} instead. - */ - @«DEPRECATED.importedName»(forRemoval = true) - public «type.getName» «setterName»(final «JU_LIST.importedName»<«actualTypeRef»> values) { - return «setterName»(«CODEHELPERS.importedName».compatMap(values)); - } ''' def private generateSimpleSetter(GeneratedProperty field, Type actualType) ''' @@ -356,21 +410,6 @@ class BuilderTemplate extends AbstractBuilderTemplate { this.«field.fieldName» = value; return this; } - «val uintType = UINT_TYPES.get(field.returnType)» - «IF uintType !== null» - - /** - * Utility migration setter. - * - * @param value field value in legacy type - * @return this builder - * @deprecated Use {#link «setterName»(«field.returnType.importedJavadocName»)} instead. - */ - @Deprecated(forRemoval = true) - public «type.getName» «setterName»(final «uintType.importedName» value) { - return «setterName»(«CODEHELPERS.importedName».compatUint(value)); - } - «ENDIF» ''' /** @@ -427,7 +466,7 @@ class BuilderTemplate extends AbstractBuilderTemplate { ''' private def createDescription(GeneratedType targetType) { - val target = type.importedName + val target = targetType.importedName return ''' Class that builds {@link «target»} instances. Overall design of the class is that of a fluent interface, where method chaining is used. @@ -436,7 +475,7 @@ class BuilderTemplate extends AbstractBuilderTemplate { In general, this class is supposed to be used like this template:
           
-            «target» createTarget(int fooXyzzy, int barBaz) {
+            «target» create«target»(int fooXyzzy, int barBaz) {
                 return new «target»Builder()
                     .setFoo(new FooBuilder().setXyzzy(fooXyzzy).build())
                     .setBar(new BarBuilder().setBaz(barBaz).build())
@@ -457,14 +496,13 @@ class BuilderTemplate extends AbstractBuilderTemplate {
               invocation, which is terminated by {@link #build()}, which is then returned from the method
           
  • better understanding by humans, as the scope of mutable state (the builder) is kept to a minimum and is very localized
  • -
  • better optimization oportunities, as the object scope is minimized in terms of invocation (rather than +
  • better optimization opportunities, as the object scope is minimized in terms of invocation (rather than method) stack, making escape analysis a lot easier. Given enough compiler (JIT/AOT) prowess, the cost of th builder object can be completely eliminated
  • @see «target» - @see «BUILDER.importedName» ''' } @@ -508,12 +546,4 @@ class BuilderTemplate extends AbstractBuilderTemplate { } ''' } - - private static def hasNonDefaultMethods(GeneratedType type) { - !type.methodDefinitions.isEmpty && type.methodDefinitions.exists([def | !def.isDefault]) - } - - private static def nonDefaultMethods(GeneratedType type) { - type.methodDefinitions.filter([def | !def.isDefault]) - } }