获取自定义注释中定义的 ElementType 枚举 @Target 注释
Get the ElementType enum defined in a custom annotation thought @Target annotation
假设我有这个注释:
@Target(METHOD)
@Retention(RUNTIME)
public @interface LifeTime {
long minutes();
long seconds();
}
有没有办法检索使用 Annotation
class 定义的 ElementType
枚举?
Annotation[] annotations = method.getDeclaredAnnotations();
for (Annotation annotation: annotations) {
if (annotation.WHAT? == ElementType.ANNOTATION_TYPE) {
}
}
谢谢。
用@Target
注解的是LifeTime
类型,不是某些method
.
从表示 Target
的 Class
对象中检索注释,并使用它的 value
方法。
Target target = LifeTime.class.getAnnotation(Target.class);
ElementType[] elementTypes = target.value();
假设我有这个注释:
@Target(METHOD)
@Retention(RUNTIME)
public @interface LifeTime {
long minutes();
long seconds();
}
有没有办法检索使用 Annotation
class 定义的 ElementType
枚举?
Annotation[] annotations = method.getDeclaredAnnotations();
for (Annotation annotation: annotations) {
if (annotation.WHAT? == ElementType.ANNOTATION_TYPE) {
}
}
谢谢。
用@Target
注解的是LifeTime
类型,不是某些method
.
从表示 Target
的 Class
对象中检索注释,并使用它的 value
方法。
Target target = LifeTime.class.getAnnotation(Target.class);
ElementType[] elementTypes = target.value();