Improve BindingReflections.getQName() 64/102664/3
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Oct 2022 13:28:36 +0000 (15:28 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Oct 2022 13:38:27 +0000 (15:38 +0200)
BaseIdentity operates on instance methods now, hence we should not
be passing a Class here. This allows us to evolve the contract in
the future.

Change-Id: I837dbfd9dffedf190c6885202d39cf730cd08de2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/IdentityCodec.java
binding/mdsal-binding-spec-util/src/main/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflections.java
binding/mdsal-binding-spec-util/src/test/java/org/opendaylight/mdsal/binding/spec/reflect/BindingReflectionsTest.java

index a798ada357d05a63600bf0e7b96ac57468311bd3..56d880474b06bc012e459071652417b00991f8c4 100644 (file)
@@ -87,6 +87,6 @@ final class IdentityCodec extends AbstractValueCodec<QName, BaseIdentity> implem
 
     @Override
     public QName fromBinding(final BaseIdentity bindingValue) {
-        return BindingReflections.getQName(bindingValue.implementedInterface());
+        return BindingReflections.getQName(bindingValue);
     }
 }
index 332719b4e3424b188a214d70802527916c985e93..099752bd7c805b586c672e5c282001a9975d14d1 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.mdsal.binding.spec.reflect;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
-import static java.util.Objects.requireNonNull;
 
 import com.google.common.annotations.Beta;
 import com.google.common.cache.CacheBuilder;
@@ -193,9 +192,9 @@ public final class BindingReflections {
         return Optional.empty();
     }
 
-    public static @NonNull QName getQName(final Class<? extends BaseIdentity> bindingClass) {
-        return CLASS_TO_QNAME.getUnchecked(requireNonNull(bindingClass))
-            .orElseThrow(() -> new IllegalStateException("Failed to resolve QName of " + bindingClass));
+    public static @NonNull QName getQName(final BaseIdentity identity) {
+        return CLASS_TO_QNAME.getUnchecked(identity.implementedInterface())
+            .orElseThrow(() -> new IllegalStateException("Failed to resolve QName of " + identity));
     }
 
     /**
index a0355e697770a1b4e6f88cdac8aa08f8a92971a2..605b8d1727fcdd8148f6273500aea366a892cab9 100644 (file)
@@ -56,12 +56,15 @@ public class BindingReflectionsTest {
         assertFalse(BindingReflections.resolveRpcOutputClass(
                 TestImplementation.class.getDeclaredMethod("rpcMethodTest2")).isPresent());
 
-        assertEquals(QName.create("test", "test"), BindingReflections.getQName(TestIdentity.class));
+        assertEquals(QName.create("test", "test"), BindingReflections.getQName(TestIdentity.VALUE));
     }
 
     interface TestIdentity extends BaseIdentity {
         QName QNAME = QName.create("test", "test");
+        TestIdentity VALUE = () -> TestIdentity.class;
 
+        @Override
+        Class<? extends TestIdentity> implementedInterface();
     }
 
     static final class TestImplementation implements Augmentation<TestImplementation>, RpcService {