BUG-4638: fix javaTypeForSchemaDefinitionType() 24/30024/7
authorRobert Varga <rovarga@cisco.com>
Fri, 20 Nov 2015 22:39:36 +0000 (23:39 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 30 Nov 2015 16:13:14 +0000 (16:13 +0000)
Rework generation to not check for ExtendedType, but rather reorder the
checks. We first deal with leafrefs/identityrefs. Then we deal with base
types. Once we have cleared those, we know we are dealing with a derived
type, no matter how it is expressed.

Change-Id: I7c0f9f18cdee7f6159b6d4b92f9bccb7682b6f6e
Signed-off-by: Robert Varga <rovarga@cisco.com>
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/yang/types/TypeProviderImpl.java

index 2d034ecaf8e70a971eaf263d30241cbbdcad94ba..55693323fd78c7fc69ed03e5b9a10dc92a5145f3 100644 (file)
@@ -93,8 +93,11 @@ import org.opendaylight.yangtools.yang.model.util.Uint64;
 import org.opendaylight.yangtools.yang.model.util.Uint8;
 import org.opendaylight.yangtools.yang.model.util.UnionType;
 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class TypeProviderImpl implements TypeProvider {
+    private static final Logger LOG = LoggerFactory.getLogger(TypeProviderImpl.class);
     private static final Pattern NUMBERS_PATTERN = Pattern.compile("[0-9]+\\z");
 
     /**
@@ -192,32 +195,41 @@ public final class TypeProviderImpl implements TypeProvider {
     @Override
     public Type javaTypeForSchemaDefinitionType(final TypeDefinition<?> typeDefinition, final SchemaNode parentNode,
             final Restrictions r) {
-        Type returnType;
         Preconditions.checkArgument(typeDefinition != null, "Type Definition cannot be NULL!");
         Preconditions.checkArgument(typeDefinition.getQName() != null,
                 "Type Definition cannot have non specified QName (QName cannot be NULL!)");
         String typedefName = typeDefinition.getQName().getLocalName();
         Preconditions.checkArgument(typedefName != null, "Type Definitions Local Name cannot be NULL!");
 
-        if (typeDefinition instanceof ExtendedType) {
-            returnType = javaTypeForExtendedType(typeDefinition);
-            if (r != null && returnType instanceof GeneratedTransferObject) {
-                GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
-                Module module = findParentModule(schemaContext, parentNode);
-                String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
-                String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
-                    typeDefinition.getPath());
-                String genTOName = BindingMapping.getClassName(typedefName);
-                String name = packageName + "." + genTOName;
-                if (!(returnType.getFullyQualifiedName().equals(name))) {
-                    returnType = shadedTOWithRestrictions(gto, r);
-                }
-            }
-        } else {
-            returnType = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode);
+        // Deal with leafrefs/identityrefs first
+        Type returnType = javaTypeForLeafrefOrIdentityRef(typeDefinition, parentNode);
+        if (returnType != null) {
+            return returnType;
+        }
+
+        if (typeDefinition.getBaseType() == null) {
+            // Now deal with base types
+            returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(typeDefinition.getQName()
+                .getLocalName());
             if (returnType == null) {
-                returnType = BaseYangTypes.BASE_YANG_TYPES_PROVIDER.javaTypeForYangType(typeDefinition.getQName()
-                        .getLocalName());
+                LOG.debug("Failed to resolve Java type for {}", typeDefinition);
+            }
+
+            // FIXME: what about base types with restrictions?
+            return returnType;
+        }
+
+        returnType = javaTypeForExtendedType(typeDefinition);
+        if (r != null && returnType instanceof GeneratedTransferObject) {
+            GeneratedTransferObject gto = (GeneratedTransferObject) returnType;
+            Module module = findParentModule(schemaContext, parentNode);
+            String basePackageName = BindingMapping.getRootPackageName(module.getQNameModule());
+            String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName,
+                typeDefinition.getPath());
+            String genTOName = BindingMapping.getClassName(typedefName);
+            String name = packageName + "." + genTOName;
+            if (!(returnType.getFullyQualifiedName().equals(name))) {
+                returnType = shadedTOWithRestrictions(gto, r);
             }
         }
         return returnType;