I am trying to dynamically load a java class. The main idea is that the bank contains modules that are dynamically loaded at run time. Here's how I do it (I know this is hacked, but there is no other way to dynamically add a jar of an 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();
Each module is annotated with @Module annotation. Therefore, in order to get additional information about the module, I am trying to get annotation. The problem is that the annotation on foo is of type com.sun. $ Proxy $ 27 instead of com.coderunner.Module and so I get
ClassCastException: Cannot cast com.sun.proxy.$Proxy42 (id=64) to com.coderunner.Module
I have to say that I'm a little confused about what is going on here. Is this what I want to make possible? How?
Edit: Perhaps I should also mention that I'm trying to do this in spring / spring-mvc and tomcat environments.
source share