java - Eclipse Neon 快速辅助自动添加@NonNull

标签 java eclipse annotations eclipse-neon

在我的 Eclipse Neon 工作区设置中,我选中了[x]启用基于注释的空分析。现在,当我从快速辅助中执行将语句分配给新变量时,Eclipse有时会将@NonNull添加到新变量分配中。因此,我得到的是第 2 行,而不是第 1 行:

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)

Returns this element's annotation for the specified type if such an annotation is present, else null.

最佳答案

你是对的,Eclipse 计算了错误的空注释:@NonNull 而不是 @Nullable。如果你use external null annotation并注释java.lang.Class.getAnnotation(Class<A>)的方法返回类型作为@Nullable,然后将语句分配给新变量快速修复创建正确的空注释。

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

请问report both bugs to Eclipse

关于java - Eclipse Neon 快速辅助自动添加@NonNull,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37412554/

相关文章:

java - 安卓工作室 : @Override "Annotations are not allowed here"

java - 将 python webapp 与一些 jar 文件集成

java - 初学者处理Java异常

java - 线程 "main"org.apache.axis2.AxisFault : Read timed out 中的异常

Java 对象和类 - 我的代码有问题吗?

java - 注解——用反射读取元素值

Java堆栈推送方法NullPointerException

java - 使用什么来识别连接到服务器的客户端? Java、安卓、套接字

Eclipse 共享项目选项?

java - 扩展一个注解接口(interface)是不是毫无意义而且令人气馁?