From 4c92b26c7cc6dde613ae04e752560f9c4f802825 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 25 Nov 2022 03:02:55 +0100 Subject: [PATCH] Clean up SchemaRootCodecContext We have a few warnings from Sonar, perform general modernization. Change-Id: Id817e176a8968afacbc4bdd7c56f25b7240af370 Signed-off-by: Robert Varga --- .../codec/impl/SchemaRootCodecContext.java | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/SchemaRootCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/SchemaRootCodecContext.java index 25745c3ed4..df88bfd955 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/SchemaRootCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/SchemaRootCodecContext.java @@ -221,8 +221,8 @@ final class SchemaRootCodecContext extends DataContainerCo @Override @SuppressWarnings("unchecked") public DataContainerCodecContext streamChild(final Class childClass) { - final DataContainerCodecContext result = Notification.class.isAssignableFrom(childClass) - ? getNotificationImpl(childClass) : getOrRethrow(childrenByClass, childClass); + final var result = Notification.class.isAssignableFrom(childClass) ? getNotificationImpl(childClass) + : getOrRethrow(childrenByClass, childClass); return (DataContainerCodecContext) result; } @@ -324,13 +324,17 @@ final class SchemaRootCodecContext extends DataContainerCo } ChoiceNodeCodecContext createChoiceDataContext(final Class caseType) { - final Class choiceClass = findCaseChoice(caseType); - checkArgument(choiceClass != null, "Class %s is not a valid case representation", caseType); - final CompositeRuntimeType schema = factory().getRuntimeContext().getSchemaDefinition(choiceClass); - checkArgument(schema instanceof ChoiceRuntimeType, "Class %s does not refer to a choice", caseType); + final var choiceClass = findCaseChoice(caseType); + if (choiceClass == null) { + throw new IllegalArgumentException(caseType + " is not a valid case representation"); + } + + final var runtimeType = factory().getRuntimeContext().getSchemaDefinition(choiceClass); + if (!(runtimeType instanceof ChoiceRuntimeType choiceType)) { + throw new IllegalArgumentException(caseType + " does not refer to a choice"); + } - final DataContainerCodecContext choice = DataContainerCodecPrototype.from(choiceClass, - (ChoiceRuntimeType)schema, factory()).get(); + final var choice = DataContainerCodecPrototype.from(choiceClass, choiceType, factory()).get(); verify(choice instanceof ChoiceNodeCodecContext); return (ChoiceNodeCodecContext) choice; } @@ -355,12 +359,12 @@ final class SchemaRootCodecContext extends DataContainerCo @Override public DataContainerCodecContext bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg, final List builder) { - final Optional> caseType = arg.getCaseType(); + final var caseType = arg.getCaseType(); if (caseType.isPresent()) { final @NonNull Class type = caseType.orElseThrow(); - final ChoiceNodeCodecContext choice = choicesByClass.getUnchecked(type); + final var choice = choicesByClass.getUnchecked(type); choice.addYangPathArgument(arg, builder); - final DataContainerCodecContext caze = choice.streamChild(type); + final var caze = choice.streamChild(type); caze.addYangPathArgument(arg, builder); return caze.bindingPathArgumentChild(arg, builder); } @@ -369,11 +373,9 @@ final class SchemaRootCodecContext extends DataContainerCo } private static Class findCaseChoice(final Class caseClass) { - for (Type type : caseClass.getGenericInterfaces()) { - if (type instanceof Class typeClass) { - if (ChoiceIn.class.isAssignableFrom(typeClass)) { - return typeClass.asSubclass(ChoiceIn.class); - } + for (var type : caseClass.getGenericInterfaces()) { + if (type instanceof Class typeClass && ChoiceIn.class.isAssignableFrom(typeClass)) { + return typeClass.asSubclass(ChoiceIn.class); } } return null; @@ -382,8 +384,8 @@ final class SchemaRootCodecContext extends DataContainerCo private static V getOrRethrow(final LoadingCache cache, final K key) { try { return cache.getUnchecked(key); - } catch (final UncheckedExecutionException e) { - final Throwable cause = e.getCause(); + } catch (UncheckedExecutionException e) { + final var cause = e.getCause(); if (cause != null) { Throwables.throwIfUnchecked(cause); } -- 2.36.6