I'm trying to dynamically load a java class. The basic idea is, that a jar contains modules which get loaded dynamically at runtime. This is how I do it (I know it's hacky, but there is no other method to dynamically add a jar to an already existing classloader afaik):
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
method.setAccessible(true);
method.invoke(moduleLoader, new Object[] { file.toURI().toURL() });
Class fooClass = moduleLoader.loadClass("com.coderunner.Foo");
Object foo = fooClass.newInstance();
Every module is annotated with an @Module annotation. So in order to gain further informations about the module, I try to get the annotation. The problem is that the annotation on foo is of type com.sun.$Proxy$27 instead of com.coderunner.Module and therefore I get a
ClassCastException: Cannot cast com.sun.proxy.$Proxy42 (id=64) to com.coderunner.Module
I have to say I'm a bit confused what happens here. Is what I want to do possible? How?
Edit: I maybe also should mention I'm trying this in a spring/spring-mvc and tomcat environment.