我有2个java注释类型,比方说XA和YA。两者都有一些方法()。我解析源代码并检索Annotation对象。现在我想动态地将注释转换为它的实际类型以便能够调用method()。如何在没有instanceof
声明的情况下执行此操作?我真的想避免像开关一样的来源。我需要这样的:Java注释动态类型转换
Annotation annotation = getAnnotation(); // I recieve the Annotation object here
String annotationType = annotation.annotationType().getName();
?_? myAnnotation = (Class.forName(annotationType)) annotation;
annotation.method(); // this is what I need, get the method() called
?_?意味着我不知道什么是myAnnotation类型。由于注释中的继承是不允许的,因此我无法将基类用于我的XA和YA批注。或者有可能做些什么?
感谢您的任何建议或帮助。
这也适用于方法和领域。 – 2011-04-29 14:08:56
这并不能解决我的问题。我需要为我使用的每个MyAnnotationType声明这一行。 classType.getAnnotation(YourAnnotationType.class);所以它会再次看起来像开关。 – 2011-04-29 18:19:01