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

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
mergify Bot added a commit to robfrank/linklift that referenced this pull request Sep 6, 2026
…42.0 to 3.42.1 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.42.0 to 3.42.1.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-migrate-java's releases](https://github.com/openrewrite/rewrite-migrate-java/releases).*

> v3.42.1
> -------
>
> What's Changed
> --------------
>
> * Do not duplicate JSpecify annotations when several annotations map to one by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1200](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1200)
> * Upgrade `maven-pmd-plugin` as part of the Java 17 migration by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1208](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1208)
> * Update inline Guava recipes for Guava 33.7.0 by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1207](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1207)
> * Extend `UseListOf` prose pattern to `LinkedHashSet` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1209](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1209)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.42.0...v3.42.1>


Commits

* [`c5ef164`](openrewrite/rewrite-migrate-java@c5ef164) Extend `UseListOf` prose pattern to `LinkedHashSet` ([#1181](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1181)) ([#1209](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1209))
* [`57c644c`](openrewrite/rewrite-migrate-java@57c644c) Update inline Guava recipes for Guava 33.7.0 ([#1207](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1207))
* [`4a91085`](openrewrite/rewrite-migrate-java@4a91085) Upgrade `maven-pmd-plugin` as part of the Java 17 migration ([#1208](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1208))
* [`1cb4f8d`](openrewrite/rewrite-migrate-java@1cb4f8d) [Auto] SDKMAN! Java candidates as of 2026-08-17T1025
* [`75f07f7`](openrewrite/rewrite-migrate-java@75f07f7) Do not duplicate JSpecify annotations when several annotations map to one ([#1](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1)...
* See full diff in [compare view](openrewrite/rewrite-migrate-java@v3.42.0...v3.42.1)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-migrate-java&package-manager=maven&previous-version=3.42.0&new-version=3.42.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

JSpecifyBestPractices add @Nullable annotionen to params twice

1 participant