You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 07:54:30