【发布时间】:2011-04-29 13:08:21
【问题描述】:
我有 2 种 java 注释类型,比如说 XA 和 YA。两者都有一些方法()。我解析源代码并检索 Annotation 对象。现在我想动态地将注释转换为它的真实类型,以便能够调用方法()。如果没有instanceof 语句,我该怎么做?我真的很想避免类似开关的来源。我需要这样的东西:
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 注释,因为不允许在注释中继承。还是有可能以某种方式做到?
感谢您的任何建议或帮助。
【问题讨论】:
标签: java casting annotations dynamic-typing