获取在自定义注释中定义的ElementType枚举@Target注释

时间:2015-11-09 22:41:38

标签: java reflection

假设我有这个注释:

@Target(METHOD)
@Retention(RUNTIME)
public @interface LifeTime {
    long minutes();
    long seconds();
}

有没有办法检索使用ElementType类定义的Annotation枚举?

Annotation[] annotations = method.getDeclaredAnnotations();

for (Annotation annotation: annotations) {
    if (annotation.WHAT? == ElementType.ANNOTATION_TYPE) {

    }
}

感谢。

1 个答案:

答案 0 :(得分:1)

使用LifeTime注释的@Target类型,而不是method注释。

从代表Class的{​​{1}}对象中检索注释,并使用其value方法。

Target