Simplify resolveLeafSchemaNodeAsMethod() logic 99/29999/6
authorRobert Varga <rovarga@cisco.com>
Fri, 20 Nov 2015 16:42:03 +0000 (17:42 +0100)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 23 Nov 2015 08:53:28 +0000 (08:53 +0000)
The logic is deeply nested, with unclear dependencies. Refactor it to
make the flow more linear.

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

index b7815a6ca85d7fe00986798f8c2f032684dc39a6..83f5bb92b8a9f25a9f4450639ad2ef6142dd158c 100644 (file)
@@ -1346,49 +1346,60 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *         </ul>
      */
     private Type resolveLeafSchemaNodeAsMethod(final GeneratedTypeBuilder typeBuilder, final LeafSchemaNode leaf, final Module module) {
+        if (leaf == null || typeBuilder == null || leaf.isAddedByUses()) {
+            return null;
+        }
+
+        final String leafName = leaf.getQName().getLocalName();
+        if (leafName == null) {
+            return null;
+        }
+
         Type returnType = null;
-        if ((leaf != null) && (typeBuilder != null)) {
-            final String leafName = leaf.getQName().getLocalName();
-            String leafDesc = leaf.getDescription();
-            if (leafDesc == null) {
-                leafDesc = "";
+        final Module parentModule = findParentModule(schemaContext, leaf);
+
+        // FIXME: cascade assumes that ExtendedType does not resolve into any of the TypeDefinitions and
+        //        will pass on to the default case. That default case will be also taken for base types.
+        //        There is another twist here, which is the fact that the new parser will wrap the type
+        //        if we have redefined the default value -- which is not something that was done before.
+
+        final TypeDefinition<?> typeDef = leaf.getType();
+        if (typeDef instanceof EnumTypeDefinition) {
+            returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
+            final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
+            final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
+                typeBuilder, module);
+
+            if (enumBuilder != null) {
+                returnType = enumBuilder.toInstance(typeBuilder);
             }
+            ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
+        } else if (typeDef instanceof UnionTypeDefinition) {
+            GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
+            if (genTOBuilder != null) {
+                returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
+            }
+        } else if (typeDef instanceof BitsTypeDefinition) {
+            GeneratedTOBuilder genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
+            if (genTOBuilder != null) {
+                returnType = genTOBuilder.toInstance();
+            }
+        } else {
+            final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
+            returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
+        }
 
-            final Module parentModule = findParentModule(schemaContext, leaf);
-            if (leafName != null && !leaf.isAddedByUses()) {
-                final TypeDefinition<?> typeDef = leaf.getType();
-
-                GeneratedTOBuilder genTOBuilder;
-                if (typeDef instanceof EnumTypeDefinition) {
-                    returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf);
-                    final EnumTypeDefinition enumTypeDef = (EnumTypeDefinition) typeDef;
-                    final EnumBuilder enumBuilder = resolveInnerEnumFromTypeDefinition(enumTypeDef, leaf.getQName(),
-                            typeBuilder,module);
+        if (returnType == null) {
+            return null;
+        }
 
-                    if (enumBuilder != null) {
-                        returnType = enumBuilder.toInstance(typeBuilder);
-                    }
-                    ((TypeProviderImpl) typeProvider).putReferencedType(leaf.getPath(), returnType);
-                } else if (typeDef instanceof UnionType) {
-                    genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
-                    if (genTOBuilder != null) {
-                        returnType = createReturnTypeForUnion(genTOBuilder, typeDef, typeBuilder, parentModule);
-                    }
-                } else if (typeDef instanceof BitsTypeDefinition) {
-                    genTOBuilder = addTOToTypeBuilder(typeDef, typeBuilder, leaf, parentModule);
-                    if (genTOBuilder != null) {
-                        returnType = genTOBuilder.toInstance();
-                    }
-                } else {
-                    final Restrictions restrictions = BindingGeneratorUtil.getRestrictions(typeDef);
-                    returnType = typeProvider.javaTypeForSchemaDefinitionType(typeDef, leaf, restrictions);
-                }
-                if (returnType != null) {
-                    final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
-                    processContextRefExtension(leaf, getter, parentModule);
-                }
-            }
+        String leafDesc = leaf.getDescription();
+        if (leafDesc == null) {
+            leafDesc = "";
         }
+
+        final MethodSignatureBuilder getter = constructGetter(typeBuilder, leafName, leafDesc, returnType);
+        processContextRefExtension(leaf, getter, parentModule);
         return returnType;
     }