首页 > 解决方案 > eclipse GetAnnotation 返回@NonNull

问题描述

我正在尝试使用 Eclipseorg.eclipse.jdt.annotation.NonNull注释,但我不明白这一点

方法签名:

@NonNull MyAnnotation java.lang.reflect.Field.getAnnotation(Class<@NonNull MyAnnotation> annotationClass)
for loop:
if(field.getAnnotation(MyAnnotation.class)!=null)
    continue;

Eclipse 声明如下代码 死代码

try{

为什么 eclipse 认为getAnnotation该类返回@NonNull?文档清楚地说明它可以返回null

我已经通过调试器验证了代码没有死,踩低谷。

编辑: 我不在任何包上使用 @NonNullByDefault

我试过了:

标签: javaeclipse

解决方案


通过添加第一行来消除警告:

Class<MyAnnotation> c = MyAnnotation.class;

if(field.getAnnotation(c)!=null)
    continue;

之后我没有收到死代码警告。

不知何故,Eclipse 将 @NonNull 注释自动添加到 MyAnnotation。


推荐阅读