Remove RpcMethodInvoker specializations 39/99839/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 22 Feb 2022 16:41:49 +0000 (17:41 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 22 Feb 2022 16:48:32 +0000 (17:48 +0100)
RPCs always contain an input, hence we do not need specializations.
Collapse the three classes into a single one.

Change-Id: Icdd126a8d6b1c6d352eaedf3efdd7b85e96c5f18
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvoker.java
binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithInput.java [deleted file]
binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithoutInput.java [deleted file]
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/AbstractMappedRpcInvokerTest.java
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerTest.java [moved from binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithInputTest.java with 76% similarity]
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithoutInputTest.java [deleted file]

index 8ed16db9100e255eeebb3eca5107fae77c4c5deb..8aee04df25e456d75a3c156f34000d8442dc2e80 100644 (file)
@@ -7,36 +7,50 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter.invoke;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Throwables;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodHandles.Lookup;
+import java.lang.invoke.MethodType;
 import java.lang.reflect.Method;
-import java.util.Optional;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
-import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.RpcService;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
-abstract class RpcMethodInvoker {
+final class RpcMethodInvoker {
+    private static final MethodType INVOCATION_SIGNATURE = MethodType.methodType(ListenableFuture.class,
+        RpcService.class, DataObject.class);
 
-    private static final Lookup LOOKUP = MethodHandles.publicLookup();
+    private final MethodHandle handle;
 
-    abstract ListenableFuture<RpcResult<?>> invokeOn(RpcService impl, DataObject input);
+    @VisibleForTesting
+    RpcMethodInvoker(final MethodHandle handle) {
+        this.handle = handle.asType(INVOCATION_SIGNATURE);
+    }
+
+    static RpcMethodInvoker from(final Method method) {
+        BindingReflections.resolveRpcInputClass(method)
+            .orElseThrow(() -> new IllegalArgumentException("Method " + method + " does not have an input argument"));
 
-    protected static RpcMethodInvoker from(final Method method) {
         final MethodHandle methodHandle;
         try {
-            methodHandle = LOOKUP.unreflect(method);
+            methodHandle = MethodHandles.publicLookup().unreflect(method);
         } catch (IllegalAccessException e) {
-            throw new IllegalStateException("Lookup on public method failed.",e);
+            throw new IllegalStateException("Lookup on public method failed.", e);
         }
 
-        final Optional<Class<? extends DataContainer>> input = BindingReflections.resolveRpcInputClass(method);
-        if (input.isPresent()) {
-            return new RpcMethodInvokerWithInput(methodHandle);
+        return new RpcMethodInvoker(methodHandle);
+    }
+
+    @SuppressWarnings("checkstyle:illegalCatch")
+    ListenableFuture<RpcResult<?>> invokeOn(final RpcService impl, final DataObject input) {
+        try {
+            return (ListenableFuture<RpcResult<?>>) handle.invokeExact(impl,input);
+        } catch (Throwable e) {
+            Throwables.throwIfUnchecked(e);
+            throw new IllegalStateException(e);
         }
-        return new RpcMethodInvokerWithoutInput(methodHandle);
     }
 }
diff --git a/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithInput.java b/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithInput.java
deleted file mode 100644 (file)
index cb85e00..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.mdsal.binding.dom.adapter.invoke;
-
-import com.google.common.base.Throwables;
-import com.google.common.util.concurrent.ListenableFuture;
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodType;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.RpcService;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-
-class RpcMethodInvokerWithInput extends RpcMethodInvoker {
-    private static final MethodType INVOCATION_SIGNATURE = MethodType.methodType(ListenableFuture.class,
-        RpcService.class, DataObject.class);
-
-    private final MethodHandle handle;
-
-    RpcMethodInvokerWithInput(final MethodHandle methodHandle) {
-        this.handle = methodHandle.asType(INVOCATION_SIGNATURE);
-    }
-
-    @Override
-    @SuppressWarnings("checkstyle:illegalCatch")
-    ListenableFuture<RpcResult<?>> invokeOn(final RpcService impl, final DataObject input) {
-        try {
-            return (ListenableFuture<RpcResult<?>>) handle.invokeExact(impl,input);
-        } catch (Throwable e) {
-            Throwables.throwIfUnchecked(e);
-            throw new IllegalStateException(e);
-        }
-    }
-}
\ No newline at end of file
diff --git a/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithoutInput.java b/binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithoutInput.java
deleted file mode 100644 (file)
index cb5c001..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.mdsal.binding.dom.adapter.invoke;
-
-import com.google.common.base.Throwables;
-import com.google.common.util.concurrent.ListenableFuture;
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodType;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.RpcService;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-
-class RpcMethodInvokerWithoutInput extends RpcMethodInvoker {
-
-    private static final MethodType INVOCATION_SIGNATURE = MethodType.methodType(ListenableFuture.class,
-        RpcService.class);
-    private final MethodHandle handle;
-
-    RpcMethodInvokerWithoutInput(final MethodHandle methodHandle) {
-        this.handle = methodHandle.asType(INVOCATION_SIGNATURE);
-    }
-
-    @Override
-    @SuppressWarnings("checkstyle:illegalCatch")
-    ListenableFuture<RpcResult<?>> invokeOn(final RpcService impl, final DataObject input) {
-        try {
-            return (ListenableFuture<RpcResult<?>>) handle.invokeExact(impl);
-        } catch (Throwable e) {
-            Throwables.throwIfUnchecked(e);
-            throw new IllegalStateException(e);
-        }
-    }
-}
\ No newline at end of file
index bcd495c24a80a0d511ce5e394a3a42783c5b2eb5..4a6201af831b88d890bbf6e5c239aa5734631bc3 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.mdsal.binding.dom.adapter.invoke;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 
@@ -26,32 +25,20 @@ import org.opendaylight.yangtools.yang.binding.RpcService;
 import org.opendaylight.yangtools.yang.common.QName;
 
 public class AbstractMappedRpcInvokerTest {
-
     @Test
     public void invokeRpcTest() throws Exception {
-        final Method methodWithoutInput =
-                TestRpcService.class.getDeclaredMethod("methodWithoutInput", RpcService.class);
         final Method methodWithInput =
                 TestRpcService.class.getDeclaredMethod("methodWithInput", RpcService.class, DataObject.class);
 
         methodWithInput.setAccessible(true);
-        methodWithoutInput.setAccessible(true);
 
         final RpcService rpcService = new TestRpcService();
 
         final TestRpcInvokerImpl testRpcInvoker =
                 new TestRpcInvokerImpl(ImmutableMap.of(
-                        "(test)tstWithoutInput", methodWithoutInput,
                         "(test)tstWithInput", methodWithInput));
 
-        assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvokerWithInput);
-        assertTrue(testRpcInvoker.map.get("(test)tstWithoutInput") instanceof RpcMethodInvokerWithoutInput);
-
-        final Crate crateWithoutInput =
-                (Crate) testRpcInvoker.invokeRpc(rpcService, QName.create("test", "tstWithoutInput"), null).get();
-        assertEquals(TestRpcService.methodWithoutInput(rpcService).get().getRpcService(),
-                crateWithoutInput.getRpcService());
-        assertFalse(crateWithoutInput.getDataObject().isPresent());
+        assertTrue(testRpcInvoker.map.get("(test)tstWithInput") instanceof RpcMethodInvoker);
 
         final DataObject dataObject = mock(DataObject.class);
         final Crate crateWithInput =
@@ -62,8 +49,7 @@ public class AbstractMappedRpcInvokerTest {
         assertEquals(dataObject, crateWithInput.getDataObject().get());
     }
 
-    private class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
-
+    private static class TestRpcInvokerImpl extends AbstractMappedRpcInvoker<String> {
         TestRpcInvokerImpl(final Map<String, Method> map) {
             super(map);
         }
@@ -85,19 +71,15 @@ public class AbstractMappedRpcInvokerTest {
         }
 
         RpcService getRpcService() {
-            return this.rpcService;
+            return rpcService;
         }
 
         Optional<DataObject> getDataObject() {
-            return this.dataObject.get();
+            return dataObject.get();
         }
     }
 
     static class TestRpcService implements RpcService {
-        static ListenableFuture<Crate> methodWithoutInput(final RpcService testArgument) {
-            return Futures.immediateFuture(new Crate(testArgument, null));
-        }
-
         static ListenableFuture<Crate> methodWithInput(final RpcService testArgument, final DataObject testArgument2) {
             return Futures.immediateFuture(new Crate(testArgument, testArgument2));
         }
@@ -8,6 +8,7 @@
 package org.opendaylight.mdsal.binding.dom.adapter.invoke;
 
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
 
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
@@ -17,34 +18,33 @@ import org.junit.Test;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.RpcService;
 
-public class RpcMethodInvokerWithInputTest {
-
+public class RpcMethodInvokerTest {
     private static final TestImplClassWithInput TEST_IMPL_CLASS = new TestImplClassWithInput();
 
     @Test
     public void invokeOnTest() throws Exception {
         final MethodHandle methodHandle = MethodHandles.lookup().unreflect(
                 TestImplClassWithInput.class.getDeclaredMethod("testMethod", RpcService.class, DataObject.class));
-        final RpcMethodInvokerWithInput rpcMethodInvokerWithInput = new RpcMethodInvokerWithInput(methodHandle);
+        final RpcMethodInvoker rpcMethodInvokerWithInput = new RpcMethodInvoker(methodHandle);
         assertNotNull(rpcMethodInvokerWithInput.invokeOn(TEST_IMPL_CLASS, null));
     }
 
-    @Test(expected = InternalError.class)
+    @Test
     public void invokeOnWithException() throws Exception {
         final MethodHandle methodHandle = MethodHandles.lookup().unreflect(TestImplClassWithInput.class
                 .getDeclaredMethod("testMethodWithException", RpcService.class, DataObject.class));
-        final RpcMethodInvokerWithInput rpcMethodInvokerWithInput = new RpcMethodInvokerWithInput(methodHandle);
-        rpcMethodInvokerWithInput.invokeOn(TEST_IMPL_CLASS, null);
+        final RpcMethodInvoker rpcMethodInvokerWithInput = new RpcMethodInvoker(methodHandle);
+
+        assertThrows(InternalError.class, () -> rpcMethodInvokerWithInput.invokeOn(TEST_IMPL_CLASS, null));
     }
 
-    private static final class TestImplClassWithInput implements RpcService {
+    static final class TestImplClassWithInput implements RpcService {
 
         static ListenableFuture<?> testMethod(final RpcService testArg, final DataObject data) {
             return Futures.immediateFuture(null);
         }
 
-        static ListenableFuture<?> testMethodWithException(final RpcService testArg, final DataObject data)
-                throws Exception {
+        static ListenableFuture<?> testMethodWithException(final RpcService testArg, final DataObject data) {
             throw new InternalError();
         }
     }
diff --git a/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithoutInputTest.java b/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/invoke/RpcMethodInvokerWithoutInputTest.java
deleted file mode 100644 (file)
index e4515ba..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.mdsal.binding.dom.adapter.invoke;
-
-import static org.junit.Assert.assertNotNull;
-
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.binding.RpcService;
-
-public class RpcMethodInvokerWithoutInputTest {
-
-    private static final TestImplClassWithoutInput TEST_IMPL_CLASS = new TestImplClassWithoutInput();
-
-    @Test
-    public void invokeOnTest() throws Exception {
-        final MethodHandle methodHandle = MethodHandles.lookup().unreflect(
-                TestImplClassWithoutInput.class.getDeclaredMethod("testMethod", RpcService.class));
-        final RpcMethodInvokerWithoutInput invokerWithoutInput = new RpcMethodInvokerWithoutInput(methodHandle);
-        assertNotNull(invokerWithoutInput.invokeOn(TEST_IMPL_CLASS, null));
-    }
-
-    @Test(expected = InternalError.class)
-    public void invokeOnWithException() throws Exception {
-        final MethodHandle methodHandle = MethodHandles.lookup().unreflect(
-                TestImplClassWithoutInput.class.getDeclaredMethod("testMethodWithException", RpcService.class));
-        final RpcMethodInvokerWithoutInput invokerWithoutInput = new RpcMethodInvokerWithoutInput(methodHandle);
-        invokerWithoutInput.invokeOn(TEST_IMPL_CLASS, null);
-    }
-
-    private static final class TestImplClassWithoutInput implements RpcService {
-
-        @SuppressWarnings("unused")
-        static ListenableFuture<?> testMethod(final RpcService testArgument) {
-            return Futures.immediateFuture(null);
-        }
-
-        @SuppressWarnings("unused")
-        static ListenableFuture<?> testMethodWithException(final RpcService testArgument) throws Exception {
-            throw new InternalError();
-        }
-    }
-}
\ No newline at end of file