Skip to content

Do not duplicate JSpecify annotations when several annotations map to one - #1200

Merged
timtebeek merged 3 commits into
mainfrom
tim/bern
Aug 14, 2026
Merged

Do not duplicate JSpecify annotations when several annotations map to one#1200
timtebeek merged 3 commits into
mainfrom
tim/bern

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 13, 2026

Copy link
Copy Markdown
Member

When an element carries two nullability annotations that both migrate to the same JSpecify annotation, each ChangeType step rewrites its own annotation and the element ends up annotated twice:

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

class Foo {
    public void bar(@Nullable @org.jetbrains.annotations.Nullable String baz) {
    }
}

migrated to @Nullable @Nullable String baz (fully qualified on the second one whenever the simple name could not be shortened, matching the output in the issue).

This adds a RemoveDuplicateAnnotations recipe that drops annotations repeated on the same element -- leading annotations on classes, methods and variables, annotations attached to modifiers, and type-use annotations on array types, nested types and type arguments -- keeping the first occurrence. It is run at the end of each MigrateFrom* recipe, so it also covers duplicates that only appear once two different MigrateFrom* recipes have both run.

Only annotations that are semantically equal to an earlier one are removed, so distinct annotations such as @Nullable @NonNull are left alone.

… one

When an element carries two nullability annotations that both migrate to the
same JSpecify annotation -- such as `javax.annotation.Nullable` together with
`javax.annotation.CheckForNull`, or annotations from two different frameworks --
the `ChangeType` steps each rewrite their own annotation, leaving the element
annotated twice.

Add a `RemoveDuplicateAnnotations` recipe that drops repeated annotations of the
same type, and run it at the end of each `MigrateFrom*` recipe.

Fixes #1199
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Aug 13, 2026
@timtebeek
timtebeek marked this pull request as draft August 13, 2026 17:42
The recipe is not specific to JSpecify: any many-to-one `ChangeType` mapping can
leave an element annotated twice. It now lives in rewrite-static-analysis, next
to `RemoveDuplicateConditions`, with an optional `annotationType` defaulting to
any annotation and a guard for `@Repeatable` annotations.

See openrewrite/rewrite-static-analysis#1000
@timtebeek

Copy link
Copy Markdown
Member Author

Moved the recipe out to rewrite-static-analysis: openrewrite/rewrite-static-analysis#1000. It is not JSpecify-specific — any many-to-one ChangeType mapping can leave an element annotated twice — so it now sits next to RemoveDuplicateConditions, with an optional annotationType defaulting to any annotation and a guard so @Repeatable annotations are never deduplicated.

Marked as draft: this PR is now just the six jspecify.yml references plus the end-to-end test, so it stays red until #1000 merges and a snapshot is published. Verified locally against a publishToMavenLocal build of #1000.

@timtebeek
timtebeek marked this pull request as ready for review August 14, 2026 10:52
@timtebeek
timtebeek merged commit 75f07f7 into main Aug 14, 2026
1 of 2 checks passed
@timtebeek
timtebeek deleted the tim/bern branch August 14, 2026 11:26
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Aug 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

JSpecifyBestPractices add @Nullable annotionen to params twice

1 participant