Take advantage of AbstractAugmentable 99/82899/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 1 May 2019 14:34:35 +0000 (16:34 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 2 Jul 2019 15:52:38 +0000 (17:52 +0200)
Rather than hand-rolling Augmentable implementations, take advantage
of AbstractAugmentable as the holder of augmentations -- reducing
the amount of generated code.

Since this leads to generated code not being exposed to augmentation(),
we can now rename the generic argument there to a full-compliant
name.

JIRA: MDSAL-445
Change-Id: I2459489074e9e50e82bd9d1c8eb051f9833b7a0b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 4fe6932b0f3911ee4a7684d3c9616efbbeb053a1)

binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/AbstractBuilderTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderImplTemplate.xtend
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderTemplate.xtend
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/BuilderGeneratorTest.java
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/AbstractAugmentable.java
binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/Augmentable.java

index e596643ace5ee70ef5dc34c8de99948d4634d3bb..8965a3b963a749ee657f0e957d04a5c9ee1c2489 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator
 
-import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
 
 import com.google.common.base.MoreObjects
@@ -98,7 +97,7 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
                     «CodeHelpers.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
                 «ENDFOR»
                 «IF augmentType !== null»
-                    «CodeHelpers.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", «AUGMENTATION_FIELD».values());
+                    «CodeHelpers.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", augmentations().values());
                 «ENDIF»
                 return helper.toString();
             }
@@ -124,18 +123,13 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
                 «field.getterMethod»
             «ENDFOR»
         «ENDIF»
-        «IF augmentType !== null»
-
-            @«SuppressWarnings.importedName»({ "unchecked", "checkstyle:methodTypeParameterName"})
-            «IF addOverride»@«Override.importedName»«ENDIF»
-            public <E$$ extends «augmentType.importedName»> E$$ «AUGMENTABLE_AUGMENTATION_NAME»(«Class.importedName»<E$$> augmentationType) {
-                return (E$$) «AUGMENTATION_FIELD».get(«CodeHelpers.importedName».nonNullValue(augmentationType, "augmentationType"));
-            }
-        «ENDIF»
     '''
 
     def protected final CharSequence generateCopyConstructor(Type fromType, Type implType) '''
         «type.name»(«fromType.importedName» base) {
+            «IF augmentType !== null»
+                «generateCopyAugmentation(implType)»
+            «ENDIF»
             «val allProps = new ArrayList(properties)»
             «val isList = implementsIfc(targetType, Types.parameterizedTypeFor(Types.typeForClass(Identifiable), targetType))»
             «IF isList && keyType !== null»
@@ -149,9 +143,6 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
             «FOR field : allProps»
                 this.«field.fieldName» = base.«field.getterMethodName»();
             «ENDFOR»
-            «IF augmentType !== null»
-                «generateCopyAugmentation(implType)»
-            «ENDIF»
         }
     '''
 
index 1f7a83ea2bdf4a3bc15614127465106c5b8d6cfc..693f61036b8875c3f012e5479e752e7b28a80c4b 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.mdsal.binding.java.api.generator
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
 
-import com.google.common.collect.ImmutableMap
 import java.util.List
 import java.util.Map
 import java.util.Objects
@@ -18,6 +17,7 @@ import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
 import org.opendaylight.mdsal.binding.model.api.GeneratedType
 import org.opendaylight.mdsal.binding.model.api.Type
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
+import org.opendaylight.yangtools.yang.binding.AbstractAugmentable
 import org.opendaylight.yangtools.yang.binding.DataObject
 
 class BuilderImplTemplate extends AbstractBuilderTemplate {
@@ -30,13 +30,14 @@ class BuilderImplTemplate extends AbstractBuilderTemplate {
     }
 
     override body() '''
-        private static final class «type.name» implements «targetType.importedName» {
-
-            «generateFields(true)»
-
+        private static final class «type.name»
+            «val impIface = targetType.importedName»
             «IF augmentType !== null»
-                private «generateAugmentField()»
+                extends «AbstractAugmentable.importedName»<«impIface»>
             «ENDIF»
+            implements «impIface» {
+
+            «generateFields(true)»
 
             «generateCopyConstructor(builderType, type)»
 
@@ -73,7 +74,7 @@ class BuilderImplTemplate extends AbstractBuilderTemplate {
 
                 «hashCodeResult(properties)»
                 «IF augmentType !== null»
-                    result = prime * result + «Objects.importedName».hashCode(«AUGMENTATION_FIELD»);
+                    result = prime * result + «Objects.importedName».hashCode(augmentations());
                 «ENDIF»
 
                 hash = result;
@@ -112,12 +113,12 @@ class BuilderImplTemplate extends AbstractBuilderTemplate {
                     if (getClass() == obj.getClass()) {
                         // Simple case: we are comparing against self
                         «type.name» otherImpl = («type.name») obj;
-                        if (!«Objects.importedName».equals(«AUGMENTATION_FIELD», otherImpl.«AUGMENTATION_FIELD»)) {
+                        if (!«Objects.importedName».equals(augmentations(), otherImpl.augmentations())) {
                             return false;
                         }
                     } else {
                         // Hard case: compare our augments with presence there...
-                        for («Map.importedName».Entry<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : «AUGMENTATION_FIELD».entrySet()) {
+                        for («Map.importedName».Entry<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> e : augmentations().entrySet()) {
                             if (!e.getValue().equals(other.«AUGMENTABLE_AUGMENTATION_NAME»(e.getKey()))) {
                                 return false;
                             }
@@ -145,6 +146,6 @@ class BuilderImplTemplate extends AbstractBuilderTemplate {
     '''
 
     override protected generateCopyAugmentation(Type implType) '''
-        this.«AUGMENTATION_FIELD» = «ImmutableMap.importedName».copyOf(base.«AUGMENTATION_FIELD»);
+        super(base.«AUGMENTATION_FIELD»);
     '''
 }
\ No newline at end of file
index ceac5eae09808f7d64e9c9c54b23aea6767e01e1..b6c9f741f1917cc547a49b8b54c3222b851fd615 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.mdsal.binding.java.api.generator
 
 import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
 
 import com.google.common.collect.ImmutableList
@@ -80,6 +81,10 @@ class BuilderTemplate extends AbstractBuilderTemplate {
             «generateMethodFieldsFrom()»
 
             «generateGetters(false)»
+            «IF augmentType !== null»
+
+                «generateAugmentation()»
+            «ENDIF»
 
             «generateSetters»
 
@@ -355,6 +360,13 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         '''.toString
     }
 
+    private def generateAugmentation() '''
+        @«SuppressWarnings.importedName»({ "unchecked", "checkstyle:methodTypeParameterName"})
+        public <E$$ extends «augmentType.importedName»> E$$ «AUGMENTABLE_AUGMENTATION_NAME»(«Class.importedName»<E$$> augmentationType) {
+            return (E$$) «AUGMENTATION_FIELD».get(«CodeHelpers.importedName».nonNullValue(augmentationType, "augmentationType"));
+        }
+    '''
+
     override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
         this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
         «FOR field : keyProps»
@@ -363,18 +375,12 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     '''
 
     override protected generateCopyAugmentation(Type implType) {
-        val implTypeRef = implType.importedName
         val augmentationHolderRef = AugmentationHolder.importedName
         val typeRef = targetType.importedName
         val hashMapRef = HashMap.importedName
         val augmentTypeRef = augmentType.importedName
         return '''
-            if (base instanceof «implTypeRef») {
-                «implTypeRef» impl = («implTypeRef») base;
-                if (!impl.«AUGMENTATION_FIELD».isEmpty()) {
-                    this.«AUGMENTATION_FIELD» = new «hashMapRef»<>(impl.«AUGMENTATION_FIELD»);
-                }
-            } else if (base instanceof «augmentationHolderRef») {
+            if (base instanceof «augmentationHolderRef») {
                 @SuppressWarnings("unchecked")
                 «Map.importedName»<«Class.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> aug =((«augmentationHolderRef»<«typeRef»>) base).augmentations();
                 if (!aug.isEmpty()) {
index 0715b762c3e37085f1beee96426c13127c59ddb3..1ba702e44cc09ffa45c029684605dbf8d9decd23 100644 (file)
@@ -68,7 +68,7 @@ public class BuilderGeneratorTest {
         assertEquals("@Override\n"
                 + "public String toString() {\n"
                 + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
-                + "    CodeHelpers.appendValue(helper, \"augmentation\", augmentation.values());\n"
+                + "    CodeHelpers.appendValue(helper, \"augmentation\", augmentations().values());\n"
                 + "    return helper.toString();\n"
                 + "}\n", genToString(mockAugment(mockGenType(TEST))).toString());
     }
@@ -79,7 +79,7 @@ public class BuilderGeneratorTest {
                 + "public String toString() {\n"
                 + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
                 + "    CodeHelpers.appendValue(helper, \"_test\", _test);\n"
-                + "    CodeHelpers.appendValue(helper, \"augmentation\", augmentation.values());\n"
+                + "    CodeHelpers.appendValue(helper, \"augmentation\", augmentations().values());\n"
                 + "    return helper.toString();\n"
                 + "}\n", genToString(mockAugment(mockGenType("get" + TEST))).toString());
     }
@@ -91,7 +91,7 @@ public class BuilderGeneratorTest {
                 + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
                 + "    CodeHelpers.appendValue(helper, \"_test1\", _test1);\n"
                 + "    CodeHelpers.appendValue(helper, \"_test2\", _test2);\n"
-                + "    CodeHelpers.appendValue(helper, \"augmentation\", augmentation.values());\n"
+                + "    CodeHelpers.appendValue(helper, \"augmentation\", augmentations().values());\n"
                 + "    return helper.toString();\n"
                 + "}\n", genToString(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
     }
index fa57186f351765440be836cb8a6978f51caf0438..ddc04ef00751db763a9e679d100122abcc0619c5 100644 (file)
@@ -42,10 +42,10 @@ public abstract class AbstractAugmentable<T extends Augmentable<T>> implements A
         this(other.augmentations);
     }
 
-    @SuppressWarnings({ "unchecked", "checkstyle:methodTypeParameterName"})
+    @SuppressWarnings("unchecked")
     @Override
-    public final <E$$ extends Augmentation<T>> E$$ augmentation(final Class<E$$> augmentationType) {
-        return (E$$) augmentations.get(CodeHelpers.nonNullValue(augmentationType, "augmentationType"));
+    public final <A extends Augmentation<T>> A augmentation(final Class<A> augmentationType) {
+        return (A) augmentations.get(CodeHelpers.nonNullValue(augmentationType, "augmentationType"));
     }
 
     @Override
index 5b8fee8cd8ce1510424eb4e7bdc6abee7dbdbf92..e6eee6959cc9dd10913a3b7c96e3bba129ad79e0 100644 (file)
@@ -27,10 +27,8 @@ public interface Augmentable<T> {
      * Returns instance of augmentation.
      *
      * @param augmentationType Type of augmentation to be returned.
-     * @param <E$$> Type capture for augmentation type
+     * @param <A> Type capture for augmentation type
      * @return instance of augmentation.
      */
-    // E$$ is an identifier which cannot be generated from models.
-    @SuppressWarnings("checkstyle:methodTypeParameterName")
-    <E$$ extends Augmentation<T>> @Nullable E$$ augmentation(Class<E$$> augmentationType);
+    <A extends Augmentation<T>> @Nullable A augmentation(Class<A> augmentationType);
 }