2

In my Eclipse Neon workspace settings i checked [x] Enable annotation-based null analysis. Now when I'm doing Assign statement to new variable from quick assist, Eclipse sometimes adds @NonNull to the new variable assigment. So intead of line 1, I get line 2:

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

This raises two questions:

  1. How can I enable and disable this behaviour? In a second project within the same workspace Eclipse doesn't to this. So it doesn't only depend on the annotation-based null analysis which was configured in the workspace settings.

  2. Why does Eclipse conclude annotation is @NonNull while the return value can be clearly be null (by documentation):

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.

1 Answer 1

2

You are right, Eclipse computes the wrong null annotation: @NonNull instead of @Nullable. If you use external null annotation and annotate the method return type of java.lang.Class.getAnnotation(Class<A>) as @Nullable then the Assign statement to new variable quick fix creates the correct null annotation.

To disable creation of null annotations the [x] Use default annotations for null specification (below [x] Enable annotation-based null analysis) must be unchecked. Unfortunately, there seems to be another bug: clicking the check box opens the Annotations for Null Specifications dialog. As workaround for this bug, specify the primary as secondary annotation and n.a as primary annotation for each annotation.

Could you please report both bugs to Eclipse?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.