I am seeing the following error when I tried to using proxy to wrap an interface.
1) [Guice/ErrorInjectingConstructor]: ClassCastException: class jdk.proxy2.$Proxy111 cannot be cast to class A (jdk.proxy2.$Proxy111 is in module jdk.proxy2 of loader 'app'; A is in unnamed module of loader 'app')
......
We have two Modules X and Y, Y is depended on X by the following:
protected void configure() {
......
install(new moduleX(config.getModuleXConfig(), config));
....
Wrapper and handler are defined in Module X:
public class MyProxyWrapper {
public static <T> T wrap(T clazz) {
T proxy = (T) Proxy.newProxyInstance(
clazz.getClass().getClassLoader(),
clazz.getClass().getInterfaces(),
new MyProxyWrapper<>(clazz)
);
return proxy;
}
}
class MyProxyWrapper<T> implements InvocationHandler {
private final T realObject;
public MyProxyWrapper(T realObject) {
this.realObject = realObject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
......
Object result = method.invoke(realObject, args);
return result;
} catch (Exception e) {
throw e.getCause();
}
}
}
The class A is defined in Module X the injection happened in Module Y.
@Inject
public UpdateDnsEntriesStep(
......
SomeClient someClient,
AnotherClient anotherClient,
) {
this.dnsUpdateClient = MyProxyWrapper.wrap(dnsUpdateClient); //This line return the error
this.hopsClient = hopsClient; // This is the current working version
......
}
In my test the Interface B defined in Module Y will work. However the Interface A defined in Module X does not. I guess it could by some class load hierarchy issue but not sure what it is. Does anyone has anyidea? Any help is appreciated.
I tried updated the Cuice to the lattest as I saw this thread mentioned that Guice may not work with the JDK17: https://github.com/google/guice/issues/1536 Also tried define the wrapper in Module Y but get the same error.