Code generator prototype - Binding specification v2 44/56144/3
authorMartin Ciglan <mciglan@cisco.com>
Wed, 26 Apr 2017 15:40:01 +0000 (17:40 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 27 Apr 2017 22:04:04 +0000 (22:04 +0000)
- fixes compilability issues in builder template
including prerequisities

Change-Id: I1653caf42139256b54a38b754f0121e749a4e61a
Signed-off-by: Martin Ciglan <mciglan@cisco.com>
(cherry picked from commit 676a097cc462ac0b22b2c05a2598e5445d13dab1)

binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java
binding2/mdsal-binding2-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/javav2/java/api/generator/renderers/BuilderRenderer.java
binding2/mdsal-binding2-java-api-generator/src/main/twirl/org/opendaylight/mdsal/binding/javav2/java/api/generator/builderTemplate.scala.txt
binding2/mdsal-binding2-spec/src/main/java/org/opendaylight/mdsal/binding/javav2/spec/structural/AugmentationHolder.java [new file with mode: 0644]

index 1e82d6e24bfce93d246794c76cdace5fb143e67e..6ba5baccc349b03cfc9c6b736b102e222145cad4 100644 (file)
@@ -396,6 +396,7 @@ final class GenHelperUtil {
             } else {
                 it.addImplementsType(parameterizedTypeFor(BindingTypes.TREE_CHILD_NODE, parent, parameterizedTypeFor
                         (BindingTypes.ITEM, parent)));
+                it.addImplementsType(parameterizedTypeFor(BindingTypes.INSTANTIABLE, it));
             }
         }
 
index ef6e926a423a744483ae52a112fae85f308dd218..9e6afb35626469d2ac34214c82cc59fed175d5c4 100644 (file)
@@ -14,11 +14,11 @@ import static org.opendaylight.mdsal.binding.javav2.java.api.generator.util.Text
 
 import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
+import com.google.common.collect.ClassToInstanceMap;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableSortedSet;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
@@ -41,8 +41,12 @@ import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.javav2.model.api.MethodSignature;
 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
+import org.opendaylight.mdsal.binding.javav2.spec.base.Instantiable;
+import org.opendaylight.mdsal.binding.javav2.spec.base.Item;
 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentable;
+import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
+import org.opendaylight.mdsal.binding.javav2.spec.structural.AugmentationHolder;
 import org.opendaylight.yangtools.concepts.Builder;
 import org.opendaylight.yangtools.concepts.Identifiable;
 
@@ -169,7 +173,7 @@ public class BuilderRenderer extends BaseRenderer {
                             importedNames.put("hashMap", importedName(HashMap.class));
                             importedNames.put("class", importedName(Class.class));
 //                            To do This is for third party, is it needed ?
-//                            importedNames.put("augmentationHolder", importedName(AugmentationHolder.class));
+                            importedNames.put("augmentationHolder", importedName(AugmentationHolder.class));
                             importedNames.put("collections", importedName(Collections.class));
                             importedNames.put("augmentFieldReturnType", importedName(augmentField.getReturnType()));
                         }
@@ -237,12 +241,21 @@ public class BuilderRenderer extends BaseRenderer {
     @Override
     protected String body() {
         importedNames.put("genType", importedName(getType()));
-        importedNames.put("arrays", importedName(Arrays.class));
         importedNames.put("objects", importedName(Objects.class));
         importedNames.put("object", importedName(Object.class));
         importedNames.put("string", importedName(String.class));
         importedNames.put("stringBuilder", importedName(StringBuilder.class));
         importedNames.put("treeNode", importedName(TreeNode.class));
+        importedNames.put("instantiable", importedName(Instantiable.class));
+        importedNames.put("item", importedName(Item.class));
+        if (getType().getParentType() != null) {
+            importedNames.put("parent", importedName(getType().getParentType()));
+        } else {
+            //TODO implement - get module as type
+        }
+        importedNames.put("augmentation", importedName(Augmentation.class));
+        importedNames.put("classInstMap", importedName(ClassToInstanceMap.class));
+
         // list for generate copy constructor
         final String copyConstructorHelper = generateListForCopyConstructor();
         List<String> getterMethods = new ArrayList<>(Collections2.transform(properties, this::getterMethod));
index 2e59d1727185031fdd7008bca8b2219276d00ed3..d659bc9b00d163c431774053111dd75023cd3280 100644 (file)
@@ -70,6 +70,8 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
         @generateEquals()
 
         @generateToString()
+
+        @generateImplementedMethods()
     }
 }
 }
@@ -105,7 +107,8 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
 }
 
 @implementedInterfaceGetter() = {
-    public @{importedNames.get("class")}<@{importedNames.get("genType")}> getImplementedInterface() {
+    @@Override
+    public @{importedNames.get("class")}<@{importedNames.get("genType")}> implementedInterface() {
     return @{importedNames.get("genType")}.class;
     }
 }
@@ -191,12 +194,27 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
     }
 }
 
+@generateImplType() = {
+@defining(genType.getName + "Impl") {typeImpl => @typeImpl}
+}
+
+@generateBuilderType() = {
+@defining(genType.getName + "Builder") {typeBuilder => @typeBuilder}
+}
+
+@generateInnerBuilderType() = {
+@defining("(" + genType.getName + "Builder base)") {typeInnerBuilder => @typeInnerBuilder}
+}
+
+@generateInnerType() = {
+@defining("(" + genType.getName + " base)") {innerType => @innerType}
+}
+
 @generateCopyConstructor(impl: Boolean) = {
     @if(impl) {private} else {public}
-    @{genType.getName}
-    @if(impl) {Impl} else {Builder}
-    (@{genType.getName}
-    @if(impl) {Builder} base) {
+    @if(impl) {@generateImplType()} else {@generateBuilderType()}
+    @if(impl) {@generateInnerBuilderType()} else {@generateInnerType()}
+    {
     @{copyConstructorHelper}
     @if(augmentField != null) {
         @if(impl) {
@@ -299,7 +317,7 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
     @if(augmentField != null) {
         @@SuppressWarnings("unchecked")
         @if(addOverride) {@@Override}
-        public <E extends @{importedNames.get("augmentFieldReturnType")}> E get@{toFirstUpper(augmentField.getName)}
+        public <E extends @{importedNames.get("augmentation")}<? super @{genType.getName}>> E get@{toFirstUpper(augmentField.getName)}
         (@{importedNames.get("class")}<E> augmentationType) {
             if (augmentationType == null) {
                 throw new IllegalArgumentException("Augmentation Type reference cannot be NULL!");
@@ -381,6 +399,21 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
     }
 }
 
+@generateImplementedMethods() = {
+        @@Override
+        public @{importedNames.get("item")}<@{genType.getParentType.getName}> treeIdentifier() {
+            //TODO implement
+            return null;
+        }
+
+        @@Override
+        public @{importedNames.get("classInstMap")}<@{importedNames.get("augmentation")}<? super @{genType.getName}>>
+        augments() {
+            //TODO implement
+            return null;
+        }
+}
+
 @generateEquals() = {
     @if(!properties.isEmpty || augmentField != null) {
         @@Override
@@ -391,7 +424,11 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
             if (!(obj instanceof @{importedNames.get("treeNode")})) {
                 return false;
             }
-            if (!@{importedNames.get("genType")}.class.equals(((@{importedNames.get("treeNode")})obj).getImplementedInterface)) {
+            if (!(obj instanceof @{importedNames.get("instantiable")})) {
+                return false;
+            }
+            if (!@{importedNames.get("genType")}.class.equals(((@{importedNames.get("instantiable")})obj)
+            .implementedInterface())) {
                 return false;
             }
             @{importedNames.get("genType")} other = (@{importedNames.get("genType")})obj;
diff --git a/binding2/mdsal-binding2-spec/src/main/java/org/opendaylight/mdsal/binding/javav2/spec/structural/AugmentationHolder.java b/binding2/mdsal-binding2-spec/src/main/java/org/opendaylight/mdsal/binding/javav2/spec/structural/AugmentationHolder.java
new file mode 100644 (file)
index 0000000..231f5f3
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.mdsal.binding.javav2.spec.structural;
+
+import java.util.Map;
+
+/**
+ *
+ * Augmentable (extensible) object which could carry additional data defined by
+ * third-party extensions, without introducing conflict between various
+ * extensions.
+ *
+ * @param <T>
+ *            Base class which should be target
+ *            for augmentations.
+ */
+public interface AugmentationHolder<T> {
+
+    /**
+     * Returns map of all augmentations.
+     *
+     * @return map of all augmentations.
+     */
+    Map<Class<? extends Augmentation<T>>,Augmentation<T>> augmentations();
+}
\ No newline at end of file