Java类自定义注解调用getAnnotation()返回null问题求助
Java类自定义注解调用getAnnotation()返回null问题求助
大家好,我现在在处理应用里几个类的自定义注解解析,遇到了个奇怪的问题:
当我调用myAnnotatedClass.getAnnotation(CustomAnnotation.class);的时候,返回的居然是null。
但我手动遍历这个类上的所有注解时,明明能看到我要找的那个注解就在里面!我写了这段代码来验证:
Annotation[] presentAnnotations = myAnnotatedClass.getAnnotations(); for(Annotation a : presentAnnotations) { System.out.println("annotation: " + a); System.out.println("type: " + a.annotationType()); }
运行后输出是这样的:
annotation: @com.mycompany.client.annotations.CustomAnnotation(someAttributue=[.....])
type: interface com.mycompany.client.annotations.CustomAnnotation
我实在搞不懂为什么直接调用getAnnotation()会返回null,有没有大佬能帮忙分析下原因啊?
备注:内容来源于stack exchange,提问作者user1154644
相关产品推荐
相关产品推荐

