Improve AnnotateNullableParameters to avoid duplicate annotations and annotation placement issues#843
Conversation
fix annotation position
timtebeek
left a comment
There was a problem hiding this comment.
Hey thanks! Makes sense, mostly. Had a quick look: on our side we avoid the duplicate annotations by running migrate to JSpecify before adding annotations. Are you using something else that you saw this?
| private static final List<String> KNOWN_NULLABLE_ANNOTATIONS = Arrays.asList( | ||
| "android.support.annotation.Nullable", | ||
| "androidx.annotation.Nullable", | ||
| "edu.umd.cs.findbugs.annotations.Nullable", | ||
| "edu.umd.cs.findbugs.annotations.CheckForNull", | ||
| "javax.annotation.Nullable", | ||
| "jakarta.annotation.Nullable", | ||
| "javax.annotation.CheckForNull", | ||
| "org.checkerframework.checker.nullness.qual.Nullable", | ||
| "org.checkerframework.checker.nullness.compatqual.NullableDecl", | ||
| "org.eclipse.jdt.annotation.Nullable", | ||
| "org.jetbrains.annotations.Nullable", | ||
| "org.jspecify.annotations.Nullable", | ||
| "org.springframework.lang.Nullable" | ||
| ); |
There was a problem hiding this comment.
Would we even need to be exhaustive here? Or can we match any annotation that contains Null? Want to avoid that we have this explicit list that we need to maintain, or folks asking to add their own when this heuristic is unlikely to have mismatches.
There was a problem hiding this comment.
I agree. But I'd be a little more specific by checking against the simple names CheckForNull and Nullable. I've seen parameters annotated with NotNull while still being checked against null. Whether that's correct, wrong, or just too defensive, if we only match any annotation that contains "Null", those parameters may end up being annotated as @Nullable @NotNull param, which is confusing.
There was a problem hiding this comment.
Ah I would have thought if we see any annotation containing Null then we skip the whole thing, and assume the user knows what they're doing, or at least wouldn't appreciate a second annotation added.
There was a problem hiding this comment.
forget about it, my brain is not braining atm. So, if we check against contains("null"), parameters annotated with NotNull and similar annotations will be skipped, although they still check against null. Which is ok, as the code will be null-safe while letting static analysis tools like NullAway know that the param shall not be null.
There was a problem hiding this comment.
Ah I would have thought if we see any annotation containing
Nullthen we skip the whole thing, and assume the user knows what they're doing, or at least wouldn't appreciate a second annotation added.
100%. I could not see your comment while typing my answer
| // TYPE_USE annotations can be positioned on array brackets and before inner types | ||
| // of nested types; declaration-target annotations stay as leading annotations | ||
| if (TYPE_USE_NULLABLE_ANNOTATIONS.contains(fullyQualifiedName)) { | ||
| // For array types, move annotation from leading annotations to array brackets |
There was a problem hiding this comment.
This makes sense, thanks. Don't see an immediate good way to avoid the existing collection here, as the new fullyQualifiedName might not be on the recipe classpath.
Running "migrate to JSpecify" before AnnotateNullable* makes sense, but in a large codebase like ours we prefer using a more conservative approach: annotate first and then decide whether and if makes sense migrate to jspecify or other annotations based on needs and constraints. Plus, codebases that still rely on FindBugs with |
Instead of maintaining a hardcoded list of 13 nullable annotation FQNs, detect any annotation whose simple name contains "null" (case-insensitive). This automatically covers all frameworks and prevents duplication/conflicts.
timtebeek
left a comment
There was a problem hiding this comment.
Thanks for the help here! Good to avoid duplicates no matter how we'd arrive at those.
Problem
The
AnnotateNullableParametersrecipe has two issues when used with nullable annotations from different frameworks:1. Duplicate annotations when a different nullable annotation is already present
The recipe only checks for the target annotation before adding it. If a parameter already carries a different nullable annotation (e.g.
@CheckForNull), the recipe blindly adds the target on top, producing inconsistent code:This is problematic because users often want to run the recipe as a first step before a separate annotation-replacement recipe (e.g.
@CheckForNull→@Nullable). The duplication forces manual cleanup.2. Compilation error with non-TYPE_USE annotations on nested types
The recipe unconditionally calls
MoveFieldAnnotationToType, which repositions the annotation before the inner type of a nested type (Outer.@Nullable Inner). This is only valid for@Target(TYPE_USE)annotations. Declaration-target annotations like@CheckForNullcannot appear in that position:Solution
1. Skip parameters that already carry a known nullable annotation
Added a
KNOWN_NULLABLE_ANNOTATIONSlist covering well-known nullable annotations across frameworks.findCandidateParametersnow checks against all of them, not just the target:org.jspecify.annotations.Nullablejavax.annotation.Nullable/jakarta.annotation.Nullablejavax.annotation.CheckForNulledu.umd.cs.findbugs.annotations.Nullable/CheckForNullorg.checkerframework.checker.nullness.qual.Nullableorg.eclipse.jdt.annotation.Nullableorg.jetbrains.annotations.Nullableorg.springframework.lang.Nullableandroid.support.annotation.Nullable/androidx.annotation.Nullable2. Position annotation correctly based on
@TargettypeAdded a
TYPE_USE_NULLABLE_ANNOTATIONSset (jspecify, Jakarta, Checker Framework, Eclipse). Array bracket positioning (String @Nullable[]) and nested type positioning (Outer.@Nullable Inner) are now gated behind this check. Declaration-target annotations stay as leading annotations:Updated recipe description
Removed the
@Target(TYPE_USE)requirement from the option and recipe descriptions, since both TYPE_USE and declaration annotations are now properly supported.Testing
@CheckForNull(skipped), one without (annotated)B.@Nullable Cpositioning@CheckForNull B.CpositioningRequested reviewer
@timtebeek
Co-authored with claude.ai - Opus 4.6