- final String moduleName = module.getName();
- final Optional<Revision> moduleRevision = module.getRevision();
- if (basePackageName != null && moduleName != null && typedef != null && typedef.getQName() != null) {
- final String typedefName = typedef.getQName().getLocalName();
- final TypeDefinition<?> innerTypeDefinition = typedef.getBaseType();
- // See generatedTypeForExtendedDefinitionType() above for rationale behind this special case.
- if (!(innerTypeDefinition instanceof LeafrefTypeDefinition)
- && !(innerTypeDefinition instanceof IdentityrefTypeDefinition)) {
- Type returnType = null;
- if (innerTypeDefinition.getBaseType() != null) {
- returnType = provideGeneratedTOFromExtendedType(typedef, innerTypeDefinition, basePackageName,
- module.getName());
- } else if (innerTypeDefinition instanceof UnionTypeDefinition) {
- final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(basePackageName,
- (UnionTypeDefinition) innerTypeDefinition, typedefName, typedef);
- genTOBuilder.setTypedef(true);
- genTOBuilder.setIsUnion(true);
- addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
- makeSerializable(genTOBuilder);
- returnType = genTOBuilder.build();
- // union builder
- final GeneratedTOBuilder unionBuilder = newGeneratedTOBuilder(genTOBuilder.getPackageName(),
- genTOBuilder.getName() + "Builder");
- 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.<Type> newHashSet(unionBuilder.build());
- additionalTypes.put(module, types);
- } else {
- types.add(unionBuilder.build());
- }
- } else if (innerTypeDefinition instanceof EnumTypeDefinition) {
- // enums are automatically Serializable
- final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) innerTypeDefinition;
- // TODO units for typedef enum
- returnType = provideTypeForEnum(enumTypeDef, typedefName, typedef);
- } else if (innerTypeDefinition instanceof BitsTypeDefinition) {
- final BitsTypeDefinition bitsTypeDefinition = (BitsTypeDefinition) innerTypeDefinition;
- final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForBitsTypeDefinition(
- basePackageName, bitsTypeDefinition, typedefName, module.getName());
- genTOBuilder.setTypedef(true);
- addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
- makeSerializable(genTOBuilder);
- returnType = genTOBuilder.build();
- } else {
- final Type javaType = javaTypeForSchemaDefinitionType(innerTypeDefinition, typedef);
- returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
- }
- if (returnType != null) {
- final Map<Optional<Revision>, Map<String, Type>> modulesByDate =
- genTypeDefsContextMap.get(moduleName);
- Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
- if (typeMap != null) {
- if (typeMap.isEmpty()) {
- typeMap = new HashMap<>(4);
- modulesByDate.put(moduleRevision, typeMap);
- }
- typeMap.put(typedefName, returnType);
- }
- return returnType;
+ final TypeDefinition<?> baseTypedef = typedef.getBaseType();
+
+ // See generatedTypeForExtendedDefinitionType() above for rationale behind this special case.
+ if (baseTypedef instanceof LeafrefTypeDefinition || baseTypedef instanceof IdentityrefTypeDefinition) {
+ return null;
+ }
+
+ final String typedefName = typedef.getQName().getLocalName();
+
+ final Type returnType;
+ if (baseTypedef.getBaseType() != null) {
+ returnType = provideGeneratedTOFromExtendedType(typedef, baseTypedef, basePackageName,
+ module.getName());
+ } else if (baseTypedef instanceof UnionTypeDefinition) {
+ final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForUnionTypeDef(
+ JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
+ (UnionTypeDefinition) baseTypedef, typedef);
+ genTOBuilder.setTypedef(true);
+ genTOBuilder.setIsUnion(true);
+ addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
+ makeSerializable(genTOBuilder);
+ returnType = genTOBuilder.build();
+
+ // Define a corresponding union builder. Typedefs are always anchored at a Java package root,
+ // so we are placing the builder alongside the union.
+ final GeneratedTOBuilder unionBuilder = newGeneratedTOBuilder(
+ JavaTypeName.create(genTOBuilder.getPackageName(), genTOBuilder.getName() + "Builder"));
+ unionBuilder.setIsUnionBuilder(true);
+ final MethodSignatureBuilder method = unionBuilder.addMethod("getDefaultInstance");
+ method.setReturnType(returnType);
+ method.addParameter(Types.STRING, "defaultValue");
+ method.setAccessModifier(AccessModifier.PUBLIC);
+ method.setStatic(true);
+ additionalTypes.computeIfAbsent(module, key -> new HashSet<>()).add(unionBuilder.build());
+ } else if (baseTypedef instanceof EnumTypeDefinition) {
+ // enums are automatically Serializable
+ final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) baseTypedef;
+ // TODO units for typedef enum
+ returnType = provideTypeForEnum(enumTypeDef, typedefName, typedef);
+ } else if (baseTypedef instanceof BitsTypeDefinition) {
+ final GeneratedTOBuilder genTOBuilder = provideGeneratedTOBuilderForBitsTypeDefinition(
+ JavaTypeName.create(basePackageName, BindingMapping.getClassName(typedef.getQName())),
+ (BitsTypeDefinition) baseTypedef, module.getName());
+ genTOBuilder.setTypedef(true);
+ addUnitsToGenTO(genTOBuilder, typedef.getUnits().orElse(null));
+ makeSerializable(genTOBuilder);
+ returnType = genTOBuilder.build();
+ } else {
+ final Type javaType = javaTypeForSchemaDefinitionType(baseTypedef, typedef);
+ returnType = wrapJavaTypeIntoTO(basePackageName, typedef, javaType, module.getName());
+ }
+ if (returnType != null) {
+ final Map<Optional<Revision>, Map<String, Type>> modulesByDate =
+ genTypeDefsContextMap.get(module.getName());
+ final Optional<Revision> moduleRevision = module.getRevision();
+ Map<String, Type> typeMap = modulesByDate.get(moduleRevision);
+ if (typeMap != null) {
+ if (typeMap.isEmpty()) {
+ typeMap = new HashMap<>(4);
+ modulesByDate.put(moduleRevision, typeMap);