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;
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;
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;
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;
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;
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>.
@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.
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;
}
--- /dev/null
+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