1

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

2
  • Can you share the Player class, or at least its getHealth methods? Having two methods that only differ by their return type doesn't sound like valid Java Commented Jul 15, 2022 at 13:27
  • @Mureinik While this might not be valid java, a class file can do such a thing. Commented Jul 15, 2022 at 15:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.