Clean up DataContainerCodecContext exception handling 27/106127/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 22 May 2023 23:13:00 +0000 (01:13 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 22 May 2023 23:17:12 +0000 (01:17 +0200)
We are getting confused between immediate throws and returns. The
exception factory methods should be returning the exception and not
throwing it.

Fix this up and add a SpotBugs annotation so that throws are guarded.
Also mark IncorrectNestingException for evolution. Final improvement
that we now have a completely-static factory method.

Change-Id: I5262c0dbb31d6935f258909828aac5331c70801b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec-api/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/IncorrectNestingException.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataContainerCodecContext.java

index 995418824e8fafb8bc074eef8648d220a20c8bf2..f4f8642f0197f2dc8a91f954579f704bd553ca75 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.mdsal.binding.dom.codec.api;
 
 import com.google.common.annotations.Beta;
 import java.io.Serial;
+import org.eclipse.jdt.annotation.NonNull;
 
 /**
  * Thrown where incorrect nesting of data structures was detected and was caused by user. This typically indicates
@@ -24,7 +25,12 @@ public class IncorrectNestingException extends IllegalArgumentException {
         super(message);
     }
 
-    public static IncorrectNestingException create(final String message, final Object... args) {
-        return new IncorrectNestingException(String.format(message, args));
+    public IncorrectNestingException(final String message, final Object... args) {
+        this(message.formatted(args));
+    }
+
+    @Deprecated(since = "12.0.0", forRemoval = true)
+    public static @NonNull IncorrectNestingException create(final String message, final Object... args) {
+        return new IncorrectNestingException(message, args);
     }
 }
index db0cc97f860e027d67d78ea9bf29ff9e6a0b2f48..7a92d50219821ac690f003bf753b5d387df855e9 100644 (file)
@@ -12,6 +12,7 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableSet;
+import edu.umd.cs.findbugs.annotations.CheckReturnValue;
 import java.io.IOException;
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.VarHandle;
@@ -210,17 +211,24 @@ abstract class DataContainerCodecContext<D extends DataObject, T extends Runtime
         return nullable;
     }
 
+    @CheckReturnValue
     private IllegalArgumentException childNullException(final QName child, final String message, final Object... args) {
         final QNameModule module = child.getModule();
         if (!factory().getRuntimeContext().getEffectiveModelContext().findModule(module).isPresent()) {
-            throw new MissingSchemaException("Module " + module + " is not present in current schema context.");
+            return new MissingSchemaException("Module " + module + " is not present in current schema context.");
         }
-        throw IncorrectNestingException.create(message, args);
+        return new IncorrectNestingException(message, args);
     }
 
-    private IllegalArgumentException childNullException(final Class<?> childClass, final String message,
+    @CheckReturnValue
+    private @NonNull IllegalArgumentException childNullException(final Class<?> childClass, final String message,
             final Object... args) {
-        final BindingRuntimeContext runtimeContext = factory().getRuntimeContext();
+        return childNullException(factory().getRuntimeContext(), childClass, message, args);
+    }
+
+    @CheckReturnValue
+    private static @NonNull IllegalArgumentException childNullException(final BindingRuntimeContext runtimeContext,
+            final Class<?> childClass, final String message, final Object... args) {
         final CompositeRuntimeType schema;
         if (Augmentation.class.isAssignableFrom(childClass)) {
             schema = runtimeContext.getAugmentationDefinition(childClass.asSubclass(Augmentation.class));
@@ -228,17 +236,17 @@ abstract class DataContainerCodecContext<D extends DataObject, T extends Runtime
             schema = runtimeContext.getSchemaDefinition(childClass);
         }
         if (schema == null) {
-            throw new MissingSchemaForClassException(childClass);
+            return new MissingSchemaForClassException(childClass);
         }
 
         try {
             runtimeContext.loadClass(Type.of(childClass));
         } catch (final ClassNotFoundException e) {
-            throw new MissingClassInLoadingStrategyException(
+            return new MissingClassInLoadingStrategyException(
                 "User supplied class " + childClass.getName() + " is not available in " + runtimeContext, e);
         }
 
-        throw IncorrectNestingException.create(message, args);
+        return new IncorrectNestingException(message, args);
     }
 
     private static QName extractName(final YangInstanceIdentifier.PathArgument child) {