This is an automated email from the ASF dual-hosted git repository.

amichai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-rsa.git

commit 4f4e00a11e1bff297ac9e71a303dc97f9bdd025d
Author: Amichai Rothman <[email protected]>
AuthorDate: Thu Sep 14 01:32:14 2023 +0300

    ARIES-2114 - Validate number of parameters and throw NoSuchMethodException 
if there is no matching method
---
 .../aries/rsa/provider/tcp/MethodInvoker.java      | 12 +++---
 .../aries/rsa/provider/tcp/MethodInvokerTest.java  | 48 ++++++++++++++++++++++
 2 files changed, 54 insertions(+), 6 deletions(-)

diff --git 
a/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/MethodInvoker.java
 
b/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/MethodInvoker.java
index 7ebe96f1..8ef6a0d3 100644
--- 
a/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/MethodInvoker.java
+++ 
b/provider/tcp/src/main/java/org/apache/aries/rsa/provider/tcp/MethodInvoker.java
@@ -48,7 +48,7 @@ public class MethodInvoker {
         return method.invoke(service, args);
     }
 
-    private Method getMethod(String methodName, Class<?>[] parameterTypesAr) {
+    private Method getMethod(String methodName, Class<?>[] parameterTypesAr) 
throws NoSuchMethodException {
         try {
             return service.getClass().getMethod(methodName, parameterTypesAr);
         } catch (NoSuchMethodException e) {
@@ -61,18 +61,18 @@ public class MethodInvoker {
                     return method;
                 }
             }
-            throw new IllegalArgumentException(String.format("No method found 
that matches name %s, types %s",
+            throw new NoSuchMethodException(String.format("No method found 
that matches name %s, types %s",
                                                              methodName, 
Arrays.toString(parameterTypesAr)));
         }
     }
 
     private boolean allParamsMatch(Class<?>[] methodParamTypes, Class<?>[] 
parameterTypesAr) {
-        int c = 0;
-        for (Class<?> type : methodParamTypes) {
-            if (!matches(type, parameterTypesAr[c])) {
+        if (parameterTypesAr.length != methodParamTypes.length)
+            return false;
+        for (int i = 0; i < methodParamTypes.length; i++) {
+            if (!matches(methodParamTypes[i], parameterTypesAr[i])) {
                 return false;
             }
-            c++;
         }
         return true;
     }
diff --git 
a/provider/tcp/src/test/java/org/apache/aries/rsa/provider/tcp/MethodInvokerTest.java
 
b/provider/tcp/src/test/java/org/apache/aries/rsa/provider/tcp/MethodInvokerTest.java
index ada05d6f..2c2c6e3d 100644
--- 
a/provider/tcp/src/test/java/org/apache/aries/rsa/provider/tcp/MethodInvokerTest.java
+++ 
b/provider/tcp/src/test/java/org/apache/aries/rsa/provider/tcp/MethodInvokerTest.java
@@ -55,4 +55,52 @@ public class MethodInvokerTest {
         assertEquals(UnsupportedOperationException.class, 
invoker.invoke("returnSomething", null).getClass());
     }
 
+    @Test
+    public void testOverloadedNumberOfParams() throws Exception {
+        class Tester {
+            public int sum() { return 0; }
+            public int sum(int i) { return i; }
+            public int sum(int i, int j) { return i + j; }
+        }
+
+        Tester tester = new Tester();
+        tester.sum((short)1);
+        tester.sum(Short.valueOf((short)1));
+
+        MethodInvoker invoker = new MethodInvoker(tester);
+        assertEquals(0, invoker.invoke("sum", null));
+        assertEquals(0, invoker.invoke("sum", new Object[] {}));
+        assertEquals(1, invoker.invoke("sum", new Object[] { 1 }));
+        assertEquals(3, invoker.invoke("sum", new Object[] { 1, 2 }));
+    }
+
+    @Test
+    public void testNoParams() throws Exception {
+        class Tester {
+            public int f() { return 0; }
+        }
+        Tester service = new Tester();
+        MethodInvoker invoker = new MethodInvoker(service);
+        assertEquals(0, invoker.invoke("f", new Object[] {}));
+        assertEquals(0, invoker.invoke("f", null));
+    }
+
+    @Test
+    public void testTooFewParams() {
+        class Tester {
+            public int f(int i) { return i; }
+        }
+        MethodInvoker invoker = new MethodInvoker(new Tester());
+        assertThrows(NoSuchMethodException.class, () -> invoker.invoke("f", 
new Object[] {}));
+    }
+
+    @Test
+    public void testTooManyParams() {
+        class Tester {
+            public int f(int i) { return i; }
+        }
+        MethodInvoker invoker = new MethodInvoker(new Tester());
+        assertThrows(NoSuchMethodException.class, () -> invoker.invoke("f", 
new Object[] { 1, 2 }));
+    }
+
 }
\ No newline at end of file

Reply via email to