gpt4 book ai didi

java - 获取在自定义注释中定义的 ElementType 枚举(认为@Target注释)

转载 作者:行者123 更新时间:2023-12-01 10:55:51 25 4
gpt4 key购买 nike

假设我有这个注释:

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

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

Annotation[] annotations = method.getDeclaredAnnotations();

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

}
}

谢谢。

最佳答案

这是用@Target注释的LifeTime类型,而不是某种方法

从表示 TargetClass 对象中检索注释,并使用其 value方法。

Target target = LifeTime.class.getAnnotation(Target.class);
ElementType[] elementTypes = target.value();

关于java - 获取在自定义注释中定义的 ElementType 枚举(认为@Target注释),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33619707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com