如何获取 class 及其 superclass 的注释列表
How to get the list of annotations of a class and its superclass
我正在编写一个方法来检索声明 class 及其超classes 的特定方法的所有注释。
通过在声明 class 上使用方法 getAnnotations()
,生成的 table 仅包含声明 class 注释和 superclass 注释忽略。
如果我删除声明 class 的注释,则存在 superclass 注释。
我在这里错过了什么?
检索注释的简化方法:
public void check(Method invokedMethod) {
for (Annotation annotation : invokedMethod.getDeclaringClass().getAnnotations()) {
// Do something ...
}
}
(我尝试获取的所有注释都有 @Inherited
注释)
如果您需要处理多个相同类型的注释,标准方法是行不通的,因为注释存储在 Map
中,注释类型作为键。 (查看更多 here)。以下是我将如何解决这个问题(只需手动检查所有超级 类):
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
public class AnnotationReflectionTest {
public static void main(String[] args) throws Exception {
check(Class2.class.getMethod("num", new Class[0]));
}
public static void check(Method invokedMethod) {
Class<?> type = invokedMethod.getDeclaringClass();
while (type != null) {
for (Annotation annotation : type.getDeclaredAnnotations()) {
System.out.println(annotation.toString());
}
type = type.getSuperclass();
}
}
}
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Annot1 {
int num();
}
@Annot1(num = 5)
class Class1 {
public int num() {
return 1;
}
}
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Annot2 {
String text();
}
@Annot2(text = "ttt")
class Class2 extends Class1 {
public int num() {
return super.num() + 1;
}
}
您使用什么版本的 Java 和什么 OS?
我不得不用
写一个简单的方法
private <A extends Annotation> A getAnnotationFromType(Class<?> classType, final Class<A> annotationClass) {
while ( !classType.getName().equals(Object.class.getName()) ) {
if ( classType.isAnnotationPresent(annotationClass)) {
return classType.getAnnotation(annotationClass);
}
classType = classType.getSuperclass();
}
return null;
}
这对大多数人来说可能是显而易见的,但如果您是来这里寻找 fields of a class and its superclasses,您可以使用
myClass.getFields()
获取所有字段,也是超classes,而不是
myClass.getDeclaredFields()
其中只有 returns class 本身的字段。对于方法和构造函数也是如此。
我正在编写一个方法来检索声明 class 及其超classes 的特定方法的所有注释。
通过在声明 class 上使用方法 getAnnotations()
,生成的 table 仅包含声明 class 注释和 superclass 注释忽略。
如果我删除声明 class 的注释,则存在 superclass 注释。
我在这里错过了什么?
检索注释的简化方法:
public void check(Method invokedMethod) {
for (Annotation annotation : invokedMethod.getDeclaringClass().getAnnotations()) {
// Do something ...
}
}
(我尝试获取的所有注释都有 @Inherited
注释)
如果您需要处理多个相同类型的注释,标准方法是行不通的,因为注释存储在 Map
中,注释类型作为键。 (查看更多 here)。以下是我将如何解决这个问题(只需手动检查所有超级 类):
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
public class AnnotationReflectionTest {
public static void main(String[] args) throws Exception {
check(Class2.class.getMethod("num", new Class[0]));
}
public static void check(Method invokedMethod) {
Class<?> type = invokedMethod.getDeclaringClass();
while (type != null) {
for (Annotation annotation : type.getDeclaredAnnotations()) {
System.out.println(annotation.toString());
}
type = type.getSuperclass();
}
}
}
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Annot1 {
int num();
}
@Annot1(num = 5)
class Class1 {
public int num() {
return 1;
}
}
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Annot2 {
String text();
}
@Annot2(text = "ttt")
class Class2 extends Class1 {
public int num() {
return super.num() + 1;
}
}
您使用什么版本的 Java 和什么 OS?
我不得不用
写一个简单的方法private <A extends Annotation> A getAnnotationFromType(Class<?> classType, final Class<A> annotationClass) {
while ( !classType.getName().equals(Object.class.getName()) ) {
if ( classType.isAnnotationPresent(annotationClass)) {
return classType.getAnnotation(annotationClass);
}
classType = classType.getSuperclass();
}
return null;
}
这对大多数人来说可能是显而易见的,但如果您是来这里寻找 fields of a class and its superclasses,您可以使用
myClass.getFields()
获取所有字段,也是超classes,而不是
myClass.getDeclaredFields()
其中只有 returns class 本身的字段。对于方法和构造函数也是如此。