You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by li...@apache.org on 2006/11/30 07:41:23 UTC

svn commit: r480858 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/org/apache/harmony/luni/internal/reflect/ProxyMethod.java test/java/tests/api/java/lang/reflect/ProxyTest.java

Author: liangyx
Date: Wed Nov 29 22:41:22 2006
New Revision: 480858

URL: http://svn.apache.org/viewvc?view=rev&rev=480858
Log:
Apply patch for HARMONY-2355 ([classlib[luni]java.lang.reflect.Proxy.newProxyInstance throws IllegalArgumentException with two interfaces one method of which have the same signature except compatible return types.)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/reflect/ProxyMethod.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ProxyTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/reflect/ProxyMethod.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/reflect/ProxyMethod.java?view=diff&rev=480858&r1=480857&r2=480858
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/reflect/ProxyMethod.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/reflect/ProxyMethod.java Wed Nov 29 22:41:22 2006
@@ -77,9 +77,18 @@
             }
         }
 
-        if (method.getReturnType() != otherMethod.getReturnType()) {
-            throw new IllegalArgumentException(Msg.getString("K00f2", method.getName()));
-        }
+        Class<?> thisMethodReturnType = method.getReturnType();
+        Class<?> otherMethodReturnType = otherMethod.getReturnType();
+        if (!thisMethodReturnType.isAssignableFrom(otherMethodReturnType)) {
+            if (otherMethodReturnType.isAssignableFrom(thisMethodReturnType)) {
+                // substitute returnType of method with that of otherMethod
+                method = otherMethod;
+            } else {
+                throw new IllegalArgumentException(Msg.getString("K00f2",
+                        method.getName()));
+            }
+        }        
+        
         if (commonExceptions.length != 0) {
             Class[] otherExceptions = otherMethod.getExceptionTypes();
             if (otherExceptions.length == 0) {
@@ -119,4 +128,5 @@
         }
         return true;
     }
+
 }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ProxyTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ProxyTest.java?view=diff&rev=480858&r1=480857&r2=480858
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ProxyTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ProxyTest.java Wed Nov 29 22:41:22 2006
@@ -220,6 +220,64 @@
 		}
 		assertTrue("Did not detect non proxy object ", aborted);
 	}
+        
+    //Regression Test for HARMONY-2355
+    public void test_newProxyInstance_withCompatibleReturnTypes() {
+        Object o = Proxy
+                .newProxyInstance(this.getClass().getClassLoader(),
+                        new Class[] { ITestReturnObject.class,
+                                ITestReturnString.class },
+                        new TestProxyHandler(new TestProxyImpl()));
+        assertNotNull(o);
+    }
+
+    public void test_newProxyInstance_withNonCompatibleReturnTypes() {
+        try {
+            Proxy.newProxyInstance(this.getClass().getClassLoader(),
+                    new Class[] { ITestReturnInteger.class,
+                            ITestReturnString.class }, new TestProxyHandler(
+                            new TestProxyImpl()));
+            fail("should throw IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+    }
+
+    public static interface ITestReturnObject {
+        Object f();
+    }
+
+    public static interface ITestReturnString {
+        String f();
+    }
+
+    public static interface ITestReturnInteger {
+        Integer f();
+    }
+
+    public static class TestProxyImpl implements ITestReturnObject,
+            ITestReturnString {
+        public String f() {
+            // do nothing
+            return null;
+        }
+    }
+
+    public static class TestProxyHandler implements InvocationHandler {
+        private Object proxied;
+
+        public TestProxyHandler(Object object) {
+            proxied = object;
+        }
+
+        public Object invoke(Object object, Method method, Object[] args)
+                throws Throwable {
+            // do nothing
+            return method.invoke(proxied, args);
+        }
+
+    }
 
 	protected void setUp() {
 	}