From: Robert Varga Date: Mon, 11 Apr 2022 17:58:59 +0000 (+0200) Subject: Do not indirect UnionLoading through Callable X-Git-Tag: v9.0.2~4 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F39%2F100539%2F1;hp=fd49b918a4f33aeca8a356cd591d419e778b8424;p=mdsal.git Do not indirect UnionLoading through Callable 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 --- diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java index 1d34844cfd..316b41e304 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java @@ -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 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) { diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/UnionTypeCodec.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/UnionTypeCodec.java index 1824f60f60..7b94e9bb46 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/UnionTypeCodec.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/UnionTypeCodec.java @@ -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 loader(final Class unionCls, final UnionTypeDefinition unionType, - final BindingCodecContext codecContext) { - return () -> { - final List unionProperties = extractUnionProperties(codecContext.getRuntimeContext() - .getTypeWithSchema(unionCls).javaType()); - final List> 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 unionProperties = extractUnionProperties(codecContext.getRuntimeContext() + .getTypeWithSchema(unionCls).javaType()); + final List> unionTypes = unionType.getTypes(); + verify(unionTypes.size() == unionProperties.size(), "Mismatched union types %s and properties %s", + unionTypes, unionProperties); - final List values = new ArrayList<>(unionTypes.size()); - final Iterator 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 codec = codecContext.getCodec(valueType, subtype); + final List values = new ArrayList<>(unionTypes.size()); + final Iterator 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 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 extractUnionProperties(final Type type) {