Binding generator v2 - Unions - Union's builder fix 23/58923/10
authorJie Han <han.jie@zte.com.cn>
Wed, 14 Jun 2017 07:59:49 +0000 (15:59 +0800)
committerJie Han <han.jie@zte.com.cn>
Thu, 15 Jun 2017 06:23:09 +0000 (14:23 +0800)
- fix return type name of treeIdentifier with fully qualified name
  to avoid name conflict
- recursive unoin builder support
- add yang file
Change-Id: I8770b849e6e50c04633eecb067d0f1d83bffb2c8
Signed-off-by: Jie Han <han.jie@zte.com.cn>
binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AuxiliaryGenUtils.java
binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/yang/types/TypeProviderImpl.java
binding2/mdsal-binding2-generator-util/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/util/Types.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/test/resources/unions.yang [new file with mode: 0644]

index dbabe6a1fb3547740598bae50e50d3bf076b8d40..b29d5be907138460894866cf2d1c9674682b9bab 100644 (file)
@@ -17,8 +17,6 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
 import com.google.common.collect.Iterables;
-import com.google.common.collect.Sets;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -35,7 +33,6 @@ import org.opendaylight.mdsal.binding.javav2.generator.util.Types;
 import org.opendaylight.mdsal.binding.javav2.generator.util.YangSnippetCleaner;
 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
-import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
 import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
@@ -413,40 +410,9 @@ final class AuxiliaryGenUtils {
         genTOBuilder.setIsUnion(true);
         TypeProviderImpl.addUnitsToGenTO(genTOBuilder, typeDef.getUnits());
 
-        final GeneratedTOBuilder unionBuilder = createUnionBuilder(genTOBuilder, typeBuilder);
-
-        final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
-        method.setReturnType(returnType);
-        method.addParameter(Types.STRING, "defaultValue");
-        method.setAccessModifier(AccessModifier.PUBLIC);
-        method.setStatic(true);
-
-        final Set<Type> types = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(parentModule);
-        if (types == null) {
-            ((TypeProviderImpl) typeProvider).getAdditionalTypes().put(parentModule,
-                    Sets.newHashSet(unionBuilder.toInstance()));
-        } else {
-            types.add(unionBuilder.toInstance());
-        }
         return returnType.toInstance();
     }
 
-    private static GeneratedTOBuilder createUnionBuilder(final GeneratedTOBuilder genTOBuilder, final GeneratedTypeBuilder typeBuilder) {
-        final String outerCls = Types.getOuterClassName(genTOBuilder);
-        final StringBuilder name;
-        if (outerCls != null) {
-            name = new StringBuilder(outerCls);
-        } else {
-            name = new StringBuilder();
-        }
-        name.append(genTOBuilder.getName());
-        name.append("Builder");
-        final GeneratedTOBuilderImpl unionBuilder = new GeneratedTOBuilderImpl(typeBuilder.getPackageName(),
-            name.toString(), true);
-        unionBuilder.setIsUnionBuilder(true);
-        return unionBuilder;
-    }
-
     static boolean isInnerType(final LeafSchemaNode leaf, final TypeDefinition<?> type) {
         if (leaf.getPath().equals(type.getPath())) {
             return true;
index 7f2f52de0c287baebfb1f886b8ae0fc9be8c198a..26ef65b2d1b8ae438b909d74a3e09614156ed977 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.mdsal.binding.javav2.generator.yang.types;
 
 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
+import static org.opendaylight.mdsal.binding.javav2.generator.util.Types.getOuterClassPackageName;
 import static org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeGenHelper.addStringRegExAsConstant;
 import static org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeGenHelper.baseTypeDefForExtendedType;
 import static org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeGenHelper.getAllTypedefs;
@@ -504,22 +505,6 @@ public final class TypeProviderImpl implements TypeProvider {
                     addUnitsToGenTO(genTOBuilder, typedef.getUnits());
                     makeSerializable((GeneratedTOBuilderImpl) genTOBuilder);
                     returnType = genTOBuilder.toInstance();
-                    // union builder
-                    final GeneratedTOBuilder unionBuilder = new GeneratedTOBuilderImpl(genTOBuilder.getPackageName(),
-                            genTOBuilder.getName() + "Builder", true, true);
-                    unionBuilder.setIsUnionBuilder(true);
-                    final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
-                    method.setReturnType(returnType);
-                    method.addParameter(Types.STRING, "defaultValue");
-                    method.setAccessModifier(AccessModifier.PUBLIC);
-                    method.setStatic(true);
-                    Set<Type> types = additionalTypes.get(module);
-                    if (types == null) {
-                        types = Sets.newHashSet(unionBuilder.toInstance());
-                        additionalTypes.put(module, types);
-                    } else {
-                        types.add(unionBuilder.toInstance());
-                    }
                 } else if (innerTypeDefinition instanceof EnumTypeDefinition) {
                     // enums are automatically Serializable
                     final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) innerTypeDefinition;
@@ -939,9 +924,44 @@ public final class TypeProviderImpl implements TypeProvider {
         resultTOBuilder.addHashIdentity(genPropBuilder);
         resultTOBuilder.addToStringProperty(genPropBuilder);
 
+        provideGeneratedTOBuilderForUnionBuilder(findParentModule(schemaContext, parentNode), resultTOBuilder);
+
         return resultTOBuilder;
     }
 
+
+    private GeneratedTOBuilder provideGeneratedTOBuilderForUnionBuilder(final Module parentModule,
+                                                 final GeneratedTOBuilder genTOBuilder) {
+        final String outerCls = Types.getOuterClassName(genTOBuilder);
+        final StringBuilder name;
+        if (outerCls != null) {
+            name = new StringBuilder(outerCls);
+        } else {
+            name = new StringBuilder();
+        }
+        name.append(genTOBuilder.getName());
+        name.append("Builder");
+        final GeneratedTOBuilderImpl unionBuilder = new GeneratedTOBuilderImpl(getOuterClassPackageName(genTOBuilder),
+                name.toString(), true);
+        unionBuilder.setIsUnionBuilder(true);
+
+        final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
+        method.setReturnType(genTOBuilder);
+        method.addParameter(Types.STRING, "defaultValue");
+        method.setAccessModifier(AccessModifier.PUBLIC);
+        method.setStatic(true);
+
+        final Set<Type> types = this.getAdditionalTypes().get(parentModule);
+        if (types == null) {
+            this.getAdditionalTypes().put(parentModule,
+                    Sets.newHashSet(unionBuilder.toInstance()));
+        } else {
+            types.add(unionBuilder.toInstance());
+        }
+
+        return unionBuilder;
+    }
+
     /**
      * Wraps code which handle case when union subtype is of the type
      * <code>ExtendedType</code>.
index 6ab7b47da0aa62fcad28e017b070f0354029aa85..6f55283cb13fc867b53b806d698cabef73737cdf 100644 (file)
@@ -231,13 +231,24 @@ public final class Types {
     @Nullable
     public static String getOuterClassName(final Type valueType) {
         final String pkgName = valueType.getPackageName();
-        if(CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName) >= 0) {
+        final int index = CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName);
+        if (index >= 0) {
             // It is inner class.
-            return Iterables.getLast(DOT_SPLITTER.split(pkgName));
+            return Iterables.getFirst(DOT_SPLITTER.split(pkgName.substring(index)), null);
         }
         return null;
     }
 
+    @Nullable
+    public static String getOuterClassPackageName(final Type valueType) {
+        final String pkgName = valueType.getPackageName();
+        final int index = CharMatcher.JAVA_UPPER_CASE.indexIn(pkgName);
+        if (index >= 1) {
+            return pkgName.substring(0, index - 1);
+        }
+        return pkgName;
+    }
+
     /**
      *
      * Represents concrete JAVA type.
index d81280d44e80943d28a999b8845762758747a9b5..5355a4054d7bca08ea8b9410f66af9ab6147fbf6 100644 (file)
@@ -252,10 +252,10 @@ public class BuilderRenderer extends BaseRenderer {
         importedNames.put("item", importedName(Item.class));
         if (getType().getParentType() != null) {
             importedNames.put("parent", importedName(getType().getParentType()));
-            parentTypeForBuilderName = getType().getParentType().getName();
+            parentTypeForBuilderName = getType().getParentType().getFullyQualifiedName();
         } else if (getType().getParentTypeForBuilder() != null) {
             importedNames.put("parentTypeForBuilder", importedName(getType().getParentTypeForBuilder()));
-            parentTypeForBuilderName = getType().getParentTypeForBuilder().getName();
+            parentTypeForBuilderName = getType().getParentTypeForBuilder().getFullyQualifiedName();
         } else {
             parentTypeForBuilderName = null;
         }
diff --git a/binding2/mdsal-binding2-java-api-generator/src/test/resources/unions.yang b/binding2/mdsal-binding2-java-api-generator/src/test/resources/unions.yang
new file mode 100644 (file)
index 0000000..17b3449
--- /dev/null
@@ -0,0 +1,45 @@
+module test-recursive-unions{
+    namespace "urn:test:unions";
+    prefix unions;
+    revision 2017-01-01;
+
+    typedef my-binary {
+        type binary;
+    }
+
+    typedef ext-binary {
+        type my-binary;
+    }
+
+    typedef my-bits {
+        type bits {
+            bit ctrl;
+            bit alt {
+                position 5;
+            }
+            bit delete;
+        }
+    }
+
+    grouping apple {
+        container apple {
+            leaf apple {
+                type union {
+                    type my-bits;
+                    type ext-binary;
+                    type instance-identifier;
+                    type int32;
+                    type string;
+                    type boolean;
+                    type union {
+                        type int64;
+                        type union {
+                           type int64;
+                           type int32;
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
\ No newline at end of file