URLClassLoader的负载注解为的com.sun。$ $代理27日 [英] URLClassLoader loads Annotation as com.sun.$Proxy$27
问题描述
我试图动态加载的Java类。的基本思路是,一个罐子包含获得在运行时动态加载的模块。这就是我要做的事(我知道这是哈克,但有没有其他的方法来一个罐子动态地添加到已经存在的类加载器据我所知):
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();
每个模块都标注有一个@Module注释。因此,为了获得有关模块的进一步的信息,我试图让注释。问题是,在富注释的类型的com.sun的。$ $代理,而不是27日的融为一体。coderunner.Module,因此我得到了
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?
编辑:我也许还应该提到我在春天尝试这种/弹簧MVC和tomcat的环境
I maybe also should mention I'm trying this in a spring/spring-mvc and tomcat environment.
推荐答案
这反射返回代理对象的事实,并不prevent你收集有关注释和值的信息。
The fact that the reflection returns a proxy object does not prevent you from gathering information about the annotation and its values.
本的getclass方法返回一个代理对象:
The getclass method returns a proxy object:
log.info("annotation class:" + annotation.getClass());
输出:
[INFO] annotation class:class com.sun.proxy.$Proxy16class
的输出是相同的实施例,但是这是没有问题的。具有方法(或场)是不够的。额外的部分是为只是调用标注方法
public void analyseClass(Class myClass) {
for (Method method: myClass.getMethods()) {
System.out.println("aanotations :" + Arrays.toString(field.getAnnotations()));
for (Annotation annotation : method.getAnnotations()) {
log.info("annotation class:" + annotation.getClass());
log.info("annotation class type:" + annotation.annotationType());
Class<Annotation> type = (Class<Annotation>) annotation.annotationType();
/* extract info only for a certain annotation */
if(type.getName().equals(MyAnnotation.class.getName())) {
String annotationValue =
(String) type.getMethod("MY_ANNOTATION_CERTAIN_METHOD_NAME").invoke(annotation);
log.info("annotationValue :" + annotationValue);
break;
}
}
}
//do the same for the fields of the class
for (Field field : myClass.getFields()) {
//...
}
}
要来此解决方案,我用了下面的帖子:
<一href=\"http://stackoverflow.com/questions/20362493/how-to-get-annotation-class-name-attribute-values-using-reflection\">How获得注记类名,属性使用反射值
To come to this solution, I used the following post: How to get annotation class name, attribute values using reflection
这篇关于URLClassLoader的负载注解为的com.sun。$ $代理27日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!