gpt4 book ai didi

Java 类型使用注释 - 如何阅读它们

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:50 24 4
gpt4 key购买 nike

我正在使用自定义类型使用注释。我无法像任何其他常规注释一样从对象中读取它们:

public class TestingAnnotations {



public static void main(final String[] args) {
final @CustomAnnotation TypeAnnotated b = new @CustomAnnotation TypeAnnotated();
System.out.println(b.getClass().getAnnotation(CustomAnnotation.class)); //<-- prints null :(
}
}

@Target({ElementType.TYPE_USE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {

}

class TypeAnnotated {

}

那么,如何检查 b 实例是否已注释?

谢谢

最佳答案

您实际上并没有注释该类....当类看起来像这样时,它就被注释了:

@CustomAnnotation
class TypeAnnotated {

}

之后你将得到注释:

TypeAnnotated b = new TypeAnnotated();
System.out.println(b.getClass().getAnnotation(CustomAnnotation.class));

关于Java 类型使用注释 - 如何阅读它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44724443/

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