Confirm bug in java.lang.reflect.Proxy wrt static methods in interfaces

mandy chung mandy.chung at oracle.com
Mon Oct 2 20:33:10 UTC 2017


I believe it is a bug.   To invoke a static method in the proxy 
interface, it will have to do "I1.foo()" or "I2.foo()".  It won't invoke 
through the proxy object, i.e. it's not interceptible.  Even creating a 
proxy for I1, the generated proxy class includes an instance method 
named "foo" and no static method is generated in the proxy class.

Mandy

On 10/2/17 11:44 AM, Eric McCorkle wrote:
> Hello everyone,
>
> A colleague of mine discovered what seems to be a bug in
> java.lang.reflect.Proxy#newProxyInstance.  I'd like to confirm that this
> is indeed incorrect behavior before I go and fix it.
>
> Consider the following two interfaces:
>
> interface I1 {
>    static I1 foo() {
>      return null;
>    }
> }
>
> interface I2 {
>    static I2 foo() {
>      return null;
>    }
> }
>
> JLS 9.4.1 states that static methods are not inherited in interfaces,
> thus it should be perfectly legal to define some class which inherits I1
> and I2.
>
> The following proxy creation fails:
>
> public class ProxyBug {
>    public static void main(String... args) {
>      I2 i2 =(I2)Proxy.newProxyInstance(
>                   ClassLoader.getSystemClassLoader(),
>                   new Class[] {I1.class, I2.class},
>                   new InvocationHandler() {
>                     @Override
>                     public Object invoke(Object proxy,
>                                          Method method,
>                                          Object[] args)
>                       throws Throwable {
>                       return null;
>                     }
>                   });
>    }
> }
>
> The exception generated is as follows:
>
> Exception in thread "main" java.lang.IllegalArgumentException: methods
> with same signature foo() but incompatible return types: [interface I1,
> interface I2]
> 	at sun.misc.ProxyGenerator.checkReturnTypes(ProxyGenerator.java:712)
> 	at sun.misc.ProxyGenerator.generateClassFile(ProxyGenerator.java:461)
> 	at sun.misc.ProxyGenerator.generateProxyClass(ProxyGenerator.java:339)
> 	at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:639)
> 	at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:557)
> 	at java.lang.reflect.WeakCache$Factory.get(WeakCache.java:230)
> 	at java.lang.reflect.WeakCache.get(WeakCache.java:127)
> 	at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:419)
> 	at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:719)
> 	at ProxyBug.main(ProxyBug.java:19)
>
> So it would seem that newProxyInstance is incorrectly including static
> methods in its check for incompatible return types.
>
> Can I get someone from core-libs to confirm that this is not intended
> before I file a bug and start on the fix?



More information about the core-libs-dev mailing list