我有 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 注释,因为不允许在注释中继承。或者有可能以某种方式做吗?
感谢您的任何建议或帮助。