2

在我的 Eclipse Neon 工作区设置中,我检查了[x] Enable annotation-based null analysis。现在,当我Assign statement to new variable从快速协助中进行操作时,Eclipse 有时会添加@NonNull到新的变量分配中。所以在第 1 行,我得到第 2 行:

Deprecated annotation = Main.class.getAnnotation(Deprecated.class); // line 1
@NonNull Deprecated annotation = Main.class.getAnnotation(Deprecated.class); // line 2

这提出了两个问题:

  1. 如何启用和禁用此行为?在同一个工作区中的第二个项目中,Eclipse 不会这样做。因此,它不仅仅依赖于在工作区设置中配置的基于注释的空值分析。

  2. 为什么 Eclipse 得出结论annotation@NonNull返回值可以清楚地是null(通过文档):

java.lang.annotation.Annotation java.lang.Class.getAnnotation(java.lang.Class annotationClass)

如果存在这样的注释,则返回此元素的指定类型的注释,否则返回 null。

4

1 回答 1

2

你是对的,Eclipse 计算了错误的空注释:@NonNull而不是@Nullable。如果您使用外部空注释并将方法返回类型注释java.lang.Class.getAnnotation(Class<A>)@Nullable,则分配语句到新变量快速修复会创建正确的空注释。

要禁用空注释的创建,必须取消选中[x] Use default annotations for null specification(下方)。[x] Enable annotation-based null analysis不幸的是,似乎还有另一个错误:单击复选框会打开“空规范注释”对话框。作为此错误的解决方法,将主要注释指定为辅助注释并指定n.a为每个注释的主要注释。

您能否向 Eclipse 报告这两个错误

于 2016-06-25T10:36:57.680 回答