Bug 2374 - YANG Binding: Added support for AugmentationHolder interface
[mdsal.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / BuilderTemplate.xtend
index 3a53ad2a9396f602818caa90c049fa249230c43b..710f1022104491baf8853ad89d4a2806be45a2eb 100644 (file)
@@ -32,9 +32,10 @@ import org.opendaylight.yangtools.yang.binding.Augmentable
 import org.opendaylight.yangtools.yang.binding.DataObject
 import org.opendaylight.yangtools.yang.binding.Identifiable
 import org.opendaylight.yangtools.concepts.Builder
+import org.opendaylight.yangtools.yang.binding.AugmentationHolder
 
 /**
- * Template for generating JAVA builder classes. 
+ * Template for generating JAVA builder classes.
  */
 
 class BuilderTemplate extends BaseTemplate {
@@ -84,7 +85,7 @@ class BuilderTemplate extends BaseTemplate {
     /**
      * Returns set of method signature instances which contains all the methods of the <code>genType</code>
      * and all the methods of the implemented interfaces.
-     * 
+     *
      * @returns set of method signature instances
      */
     def private Set<MethodSignature> createMethods() {
@@ -97,9 +98,9 @@ class BuilderTemplate extends BaseTemplate {
     }
 
     /**
-     * Adds to the <code>methods</code> set all the methods of the <code>implementedIfcs</code> 
+     * Adds to the <code>methods</code> set all the methods of the <code>implementedIfcs</code>
      * and recursively their implemented interfaces.
-     * 
+     *
      * @param methods set of method signatures
      * @param implementedIfcs list of implemented interfaces
      */
@@ -132,7 +133,7 @@ class BuilderTemplate extends BaseTemplate {
 
     /**
      * Returns the first element of the list <code>elements</code>.
-     * 
+     *
      * @param list of elements
      */
     def private <E> first(List<E> elements) {
@@ -141,7 +142,7 @@ class BuilderTemplate extends BaseTemplate {
 
     /**
      * Returns the name of the package from <code>fullyQualifiedName</code>.
-     * 
+     *
      * @param fullyQualifiedName string with fully qualified type name (package + type)
      * @return string with the package name
      */
@@ -163,8 +164,8 @@ class BuilderTemplate extends BaseTemplate {
 
     /**
      * Creates set of generated property instances from getter <code>methods</code>.
-     * 
-     * @param set of method signature instances which should be transformed to list of properties 
+     *
+     * @param set of method signature instances which should be transformed to list of properties
      * @return set of generated property instances which represents the getter <code>methods</code>
      */
     def private propertiesFromMethods(Collection<MethodSignature> methods) {
@@ -183,7 +184,7 @@ class BuilderTemplate extends BaseTemplate {
 
     /**
      * Creates generated property instance from the getter <code>method</code> name and return type.
-     * 
+     *
      * @param method method signature from which is the method name and return type obtained
      * @return generated property instance for the getter <code>method</code>
      * @throws IllegalArgumentException<ul>
@@ -200,7 +201,7 @@ class BuilderTemplate extends BaseTemplate {
         var prefix = "get";
         if(Types.BOOLEAN.equals(method.returnType)) {
             prefix = "is";
-        } 
+        }
         if (method.name.startsWith(prefix)) {
             val fieldName = method.getName().substring(prefix.length()).toFirstLower
             val tmpGenTO = new GeneratedTOBuilderImpl("foo", "foo")
@@ -210,8 +211,8 @@ class BuilderTemplate extends BaseTemplate {
     }
 
     /**
-     * Template method which generates JAVA class body for builder class and for IMPL class. 
-     * 
+     * Template method which generates JAVA class body for builder class and for IMPL class.
+     *
      * @return string with JAVA source code
      */
     override body() '''
@@ -251,7 +252,7 @@ class BuilderTemplate extends BaseTemplate {
                 «generateHashCode()»
 
                 «generateEquals()»
-                
+
                 «generateToString(properties)»
             }
 
@@ -263,7 +264,7 @@ class BuilderTemplate extends BaseTemplate {
      */
     def private generateConstructorsFromIfcs(Type type) '''
         public «type.name»«BUILDER»() {
-        } 
+        }
         «IF (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject))»
             «val ifc = type as GeneratedType»
             «FOR impl : ifc.implements»
@@ -277,14 +278,12 @@ class BuilderTemplate extends BaseTemplate {
      */
     def private Object generateConstructorFromIfc(Type impl) '''
         «IF (impl instanceof GeneratedType)»
-            «val implType = impl as GeneratedType»
-
-            «IF !(implType.methodDefinitions.empty)»
-                public «type.name»«BUILDER»(«implType.fullyQualifiedName» arg) {
-                    «printConstructorPropertySetter(implType)»
+            «IF !(impl.methodDefinitions.empty)»
+                public «type.name»«BUILDER»(«impl.fullyQualifiedName» arg) {
+                    «printConstructorPropertySetter(impl)»
                 }
             «ENDIF»
-            «FOR implTypeImplement : implType.implements»
+            «FOR implTypeImplement : impl.implements»
                 «generateConstructorFromIfc(implTypeImplement)»
             «ENDFOR»
         «ENDIF»
@@ -380,7 +379,7 @@ class BuilderTemplate extends BaseTemplate {
                 baseIfcs.add(ifc)
             }
         }
-        return baseIfcs 
+        return baseIfcs
     }
 
     private def Set<Type> getAllIfcs(Type type) {
@@ -394,7 +393,7 @@ class BuilderTemplate extends BaseTemplate {
                 baseIfcs.addAll(impl.getAllIfcs)
             }
         }
-        return baseIfcs 
+        return baseIfcs
     }
 
     private def List<String> toListOfNames(Collection<Type> types) {
@@ -558,25 +557,28 @@ class BuilderTemplate extends BaseTemplate {
                 this.«field.fieldName» = base.«field.getterMethodName»();
             «ENDFOR»
             «IF augmentField != null»
-                «IF !impl»if (base instanceof «type.name»«IMPL») {«ENDIF»
-                    «IF !impl»«type.name»«IMPL» _impl = («type.name»«IMPL») base;«ENDIF»
-                    «val prop = if (impl) "base" else "_impl"»
-                    «IF impl»
-                        switch («prop».«augmentField.name».size()) {
-                        case 0:
-                            this.«augmentField.name» = «Collections.importedName».emptyMap();
-                            break;
-                            case 1:
-                                final «Map.importedName».Entry<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> e = «prop».«augmentField.name».entrySet().iterator().next();
-                                this.«augmentField.name» = «Collections.importedName».<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue());       
-                            break;
-                        default :
-                            this.«augmentField.name» = new «HashMap.importedName»<>(«prop».«augmentField.name»);
-                        }
-                    «ELSE»
-                        this.«augmentField.name» = new «HashMap.importedName»<>(«prop».«augmentField.name»);
-                    «ENDIF»
-                «IF !impl»}«ENDIF»
+                «IF impl»
+                    switch (base.«augmentField.name».size()) {
+                    case 0:
+                        this.«augmentField.name» = «Collections.importedName».emptyMap();
+                        break;
+                        case 1:
+                            final «Map.importedName».Entry<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> e = base.«augmentField.name».entrySet().iterator().next();
+                            this.«augmentField.name» = «Collections.importedName».<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue());
+                        break;
+                    default :
+                        this.«augmentField.name» = new «HashMap.importedName»<>(base.«augmentField.name»);
+                    }
+                «ELSE»
+                    if (base instanceof «type.name»«IMPL») {
+                        «type.name»«IMPL» impl = («type.name»«IMPL») base;
+                        this.«augmentField.name» = new «HashMap.importedName»<>(impl.«augmentField.name»);
+                    } else if (base instanceof «AugmentationHolder.importedName») {
+                        @SuppressWarnings("unchecked")
+                        «AugmentationHolder.importedName»<«type.importedName»> casted =(«AugmentationHolder.importedName»<«type.importedName»>) base;
+                        this.«augmentField.name» = new «HashMap.importedName»<>(casted.augmentations());
+                    }
+                «ENDIF»
             «ENDIF»
         }
     '''
@@ -613,7 +615,7 @@ class BuilderTemplate extends BaseTemplate {
 
     /**
      * Template method which generate getter methods for IMPL class.
-     * 
+     *
      * @return string with getter methods
      */
     def private generateGetters(boolean addOverride) '''
@@ -638,7 +640,7 @@ class BuilderTemplate extends BaseTemplate {
 
     /**
      * Template method which generates the method <code>hashCode()</code>.
-     * 
+     *
      * @return string with the <code>hashCode()</code> method definition in JAVA format
      */
     def protected generateHashCode() '''
@@ -664,8 +666,8 @@ class BuilderTemplate extends BaseTemplate {
 
     /**
      * Template method which generates the method <code>equals()</code>.
-     * 
-     * @return string with the <code>equals()</code> method definition in JAVA format     
+     *
+     * @return string with the <code>equals()</code> method definition in JAVA format
      */
     def protected generateEquals() '''
         «IF !properties.empty || augmentField != null»
@@ -770,15 +772,15 @@ class BuilderTemplate extends BaseTemplate {
         return «type.importedName».class;
     }
     '''
-    
+
     private def createDescription(GeneratedType type) {
         return '''
         Class that builds {@link «type.importedName»} instances.
-        
+
         @see «type.importedName»
     '''
     }
-    
+
     override def protected String formatDataForJavaDoc(GeneratedType type) {
         val typeDescription = createDescription(type)