Do not indirect UnionLoading through Callable 39/100539/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Apr 2022 17:58:59 +0000 (19:58 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Apr 2022 17:59:56 +0000 (19:59 +0200)
Let's just declare Exception to be thrown, forcing the caller to deal
with it -- which it already does.

JIRA: MDSAL-704
Change-Id: I21e26b4f40814bb62a03b5e4a46eed3c29f27882
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/UnionTypeCodec.java

index 1d34844cfd9802f9fecef3aeb94ad54b28400358..316b41e3046dbea6306fe58162d57c5e30e391cf 100644 (file)
@@ -34,7 +34,6 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.ServiceLoader;
-import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
 import javax.inject.Inject;
 import javax.inject.Singleton;
@@ -447,11 +446,9 @@ public final class BindingCodecContext extends AbstractBindingNormalizedNodeSeri
         } else if (typeDef instanceof InstanceIdentifierTypeDefinition) {
             return new CompositeValueCodec(SchemaUnawareCodec.of(valueType, typeDef), instanceIdentifierCodec);
         } else if (typeDef instanceof UnionTypeDefinition) {
-            final Callable<UnionTypeCodec> unionLoader = UnionTypeCodec.loader(valueType, (UnionTypeDefinition) typeDef,
-                this);
             try {
-                return unionLoader.call();
-            } catch (final Exception e) {
+                return UnionTypeCodec.of(valueType, (UnionTypeDefinition) typeDef, this);
+            } catch (Exception e) {
                 throw new IllegalStateException("Unable to load codec for " + valueType, e);
             }
         } else if (typeDef instanceof LeafrefTypeDefinition) {
index 1824f60f60d15cc02ad2ae34e7c4327e393c1a18..7b94e9bb46cdbfb2d03cdd72de1217e70575366a 100644 (file)
@@ -16,7 +16,6 @@ import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
-import java.util.concurrent.Callable;
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.runtime.api.RuntimeGeneratedUnion;
@@ -35,28 +34,26 @@ final class UnionTypeCodec extends ValueTypeCodec {
         this.typeCodecs = ImmutableSet.copyOf(typeCodecs);
     }
 
-    static Callable<UnionTypeCodec> loader(final Class<?> unionCls, final UnionTypeDefinition unionType,
-            final BindingCodecContext codecContext) {
-        return () -> {
-            final List<String> unionProperties = extractUnionProperties(codecContext.getRuntimeContext()
-                .getTypeWithSchema(unionCls).javaType());
-            final List<TypeDefinition<?>> unionTypes = unionType.getTypes();
-            verify(unionTypes.size() == unionProperties.size(), "Mismatched union types %s and properties %s",
-                unionTypes, unionProperties);
+    static UnionTypeCodec of(final Class<?> unionCls, final UnionTypeDefinition unionType,
+            final BindingCodecContext codecContext) throws Exception {
+        final List<String> unionProperties = extractUnionProperties(codecContext.getRuntimeContext()
+            .getTypeWithSchema(unionCls).javaType());
+        final List<TypeDefinition<?>> unionTypes = unionType.getTypes();
+        verify(unionTypes.size() == unionProperties.size(), "Mismatched union types %s and properties %s",
+            unionTypes, unionProperties);
 
-            final List<UnionValueOptionContext> values = new ArrayList<>(unionTypes.size());
-            final Iterator<String> it = unionProperties.iterator();
-            for (final TypeDefinition<?> subtype : unionTypes) {
-                final String getterName = BindingMapping.GETTER_PREFIX + BindingMapping.toFirstUpper(it.next());
-                final Method valueGetter = unionCls.getMethod(getterName);
-                final Class<?> valueType = valueGetter.getReturnType();
-                final IllegalArgumentCodec<Object, Object> codec = codecContext.getCodec(valueType, subtype);
+        final List<UnionValueOptionContext> values = new ArrayList<>(unionTypes.size());
+        final Iterator<String> it = unionProperties.iterator();
+        for (final TypeDefinition<?> subtype : unionTypes) {
+            final String getterName = BindingMapping.GETTER_PREFIX + BindingMapping.toFirstUpper(it.next());
+            final Method valueGetter = unionCls.getMethod(getterName);
+            final Class<?> valueType = valueGetter.getReturnType();
+            final IllegalArgumentCodec<Object, Object> codec = codecContext.getCodec(valueType, subtype);
 
-                values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, codec));
-            }
+            values.add(new UnionValueOptionContext(unionCls, valueType, valueGetter, codec));
+        }
 
-            return new UnionTypeCodec(unionCls, values);
-        };
+        return new UnionTypeCodec(unionCls, values);
     }
 
     private static List<String> extractUnionProperties(final Type type) {