Menu

#34 Proxy.newProxyInstance uses wrong classloader

open
nobody
None
5
2014-08-25
2010-07-21
szetheli
No

Repro:
Create a situation where the interfaces parameter of Proxy.newProxyInstance contains classes not visible to the classloader that owns CGLib. In my case, this is happening through OSGi bundles. This can also be done with just using URLClassLoaders to load another jar as well.
Pass in the interface that isn't visible, with the classloader that creates it.
Proxy.newProxyInstance(myInterface.getClassLoader(), new ClassLoader[] {myInterface}, myHandler)

Result:
java.lang.reflect.InvocationTargetException wrapping java.lang.ClassNotFoundException for myInterface

Reason:
Proxy.getProxyClass is failing to pass the loader parameter to its created Enhancer, so the Enhancer goes to use the CGLib's classloader instead of the classloader that actually knew about the myInterface.

Expected:
Proxy.getProxyClass should respect the loader it is passed and use it to resolve classes instead of the classloader that owns CGLib.

Problem is present in CGLib 2.2.0, and I've also looked at the current CVS branch.

Discussion