Search code examples
javaeclipsejooqnotnull

How to prevent Eclipse from adding @NotNull annotation when assigning jOOQ expression to local variable


Starting from jOOQ 3.14, when using jOOQ with code like this:

DSL.val(1);

I like to assign the above expression to a local variable, so I'm using the Eclipse IDE quick fix action "Assign statement to new local variable", which adds this import:

import org.jetbrains.annotations.NotNull;

And produces this code:

@NotNull
Param<Integer> val = DSL.val(1);

This doesn't happen with IntelliJ whose "Introduce local variable" quick fix produces the desired code:

Param<Integer> val = DSL.val(1);

How can the unwanted insertion of the @NotNull annotation be prevented?


Solution

  • There's a pending bug in Eclipse to fix this: https://bugs.eclipse.org/bugs/show_bug.cgi?id=565463. The rationale of the current behaviour (still present in 2020-12 (4.18.0)) can be seen in that issue.

    A workaround is to turn on annotation-based null analysis in Preferences > Java Compiler > Errors/Warnings > Enable annotation-based null analysis

    enter image description here

    ... and setting the jetbrains annotations as the primary and/or secondary annotations:

    enter image description here

    Eclipse may not find the annotations, in case of which a workaround for this may be to manually edit your project's .settings/org.eclipse.jdt.core.prefs file, adding these:

    org.eclipse.jdt.core.compiler.annotation.nonnull=org.jetbrains.annotations.NotNull
    org.eclipse.jdt.core.compiler.annotation.nullable=org.jetbrains.annotations.Nullable
    org.eclipse.jdt.core.compiler.annotation.nullanalysis=enabled
    

    The side effect of this workaround is of course that null analysis is now active on jOOQ code, which may or may not be what you want.