J.Doe
J.Doe

Reputation: 11

Java Reflection. Unable to create java.reflect.Proxy because of 'Methods with same signature'

I am trying to make a Proxy class that implements a class called Player.

public Player asPlayer() {
    return (Player) Proxy.newProxyInstance(Player.class.getClassLoader(),
                    new Class[]{Player.class},
                    new TestPlayerInvocationHandler());
}

However I get the following error when I attempt to run it.

methods with same signature getHealth() but incompatible return types: double and others'
java.lang.IllegalArgumentException: methods with same signature getHealth() but incompatible return types: double and others
at sun.misc.ProxyGenerator.checkReturnTypes(ProxyGenerator.java:656)
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 me.deltaorion.test.TestPlayer.asPlayer(TestPlayer.java:45)
at me.deltaorion.test.Tester.runLaunchTests(Tests.java:86)

I believe this is caused by the fact that player has two methods called getHealth(), one of which returns an int and the other returns a double.

I am unable to modify the original Player class

I am wondering if there is any work around in creating the Player Proxy instance or any fix to this issue.

Thanks, DeltaOrion

Upvotes: 1

Views: 228

Answers (0)

Related Questions