Skip to content

Move type-use annotations to array brackets during JSpecify migration#1038

Merged
timtebeek merged 13 commits intomainfrom
tim/issue-934-investigation
Apr 2, 2026
Merged

Move type-use annotations to array brackets during JSpecify migration#1038
timtebeek merged 13 commits intomainfrom
tim/issue-934-investigation

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Mar 31, 2026

Summary

  • Fixes MigrateFromJavaxAnnotationApi does not modify annotations to the type level #934

  • Adds a new MoveAnnotationToArrayType recipe that moves type-use annotations (e.g. @Nullable) from declaration position to the array brackets (e.g. @Nullable byte[]byte @Nullable[] ), as required by the JSpecify spec.

  • Wires the recipe into all 6 JSpecify migration recipes in jspecify.yml, running after MoveFieldAnnotationToType.

  • Handles method return types, method parameters, and field declarations.

Test plan

  • Unit tests for MoveAnnotationToArrayType: return types, parameters, fields, non-array no-ops, multi-dimensional arrays
  • Integration test in JSpecifyBestPracticesTest for end-to-end javax → JSpecify migration with array types
  • All existing jspecify tests continue to pass

Move the recipe before ChangeType in each migration pipeline so it
matches on the old annotation type (e.g. javax.annotation.*). This
avoids incorrectly moving pre-existing JSpecify annotations where
@nullable String[] intentionally means "array of nullable Strings."
…ected

Verifies that a project with both javax annotations (to migrate) and
pre-existing JSpecify @nullable String[] (meaning array of nullable
elements) only migrates the javax annotations without touching the
already-correct JSpecify annotations.
Comment on lines +98 to +108
List<J.Annotation> leading = ListUtils.map(mv.getLeadingAnnotations(), a -> {
if (match[0] == null && matchesType(a)) {
match[0] = a;
return null;
}
return a;
});
if (leading == mv.getLeadingAnnotations()) {
return mv;
}
mv = mv.withLeadingAnnotations(leading);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I'm trying to figure out if we have recipes that are going to be fragile in light of leading annotations being dropped and whether any of our recipes need to be adapted to handle the change in position for the annotations (have not had a chance to dig into this further yet)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know what you'd need to be comfortable seeing this merged!

Comment on lines +208 to +231
void moveNullableToNestedClassArrayField() {
rewriteRun(
//language=java
java(
"""
import javax.annotation.Nullable;
import java.util.Map;

class Foo {
@Nullable
public Map.Entry<String, String>[] entries;
}
""",
"""
import javax.annotation.Nullable;
import java.util.Map;

class Foo {
public Map.Entry<String, String> @Nullable[] entries;
}
"""
)
);
}
Copy link
Copy Markdown
Contributor

@steve-aom-elliott steve-aom-elliott Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test may actually be incorrect. Based on this, these could be different meanings:

@Nullable X[] x

// I believe the nested equivalent would be
y.@Nullable X[] x

means individual elements can be null, but not the array itself

X @Nullable [] x

// I believe the nested equivalent would be
y.X @Nullable [] x

means individual elements cannot be null, but the array can

@Nullable X @Nullable [] x

// I believe the nested equivalent would be
y.@Nullable X @Nullable [] x

means both individual elements and the array itself can be null

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm; I thought it was different for the javax nullable annotation:

javax.annotation.Nullable is NOT a TYPE_USE annotation — it only targets METHOD, FIELD, PARAMETER, etc. When someone writes @javax.annotation.Nullable Map.Entry[], the annotation is in declaration position — it doesn’t have the element-vs-array distinction. It’s syntactically equivalent to “this whole thing might be null.”

The recipe deliberately targets pre-migration annotations (before ChangeType renames them to JSpecify). By the time the annotation becomes org.jspecify.annotations.Nullable (which IS TYPE_USE), it’s already in the correct array-bracket position with the intended meaning: “the array reference can be null.”

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The annotations targeted by MoveAnnotationToArrayType are:

  1. javax.annotation.*ull* — No @Target at all (JSR-305). Implicitly applies everywhere including TYPE_USE.
  2. jakarta.annotation.*ull*@Target({METHOD, PARAMETER, FIELD, TYPE_USE})yes, TYPE_USE
  3. org.jetbrains.annotations.*ull*@Target({METHOD, FIELD, PARAMETER, LOCAL_VARIABLE, TYPE_USE})yes, TYPE_USE
  4. io.micrometer.core.lang.*ull*@Target({METHOD, FIELD, PARAMETER, LOCAL_VARIABLE})no TYPE_USE
  5. org.springframework.lang.*ull*@Target({METHOD, PARAMETER, FIELD})no TYPE_USE
  6. io.micronaut.core.annotation.*ull*@Target({METHOD, PARAMETER, FIELD, TYPE_USE, ANNOTATION_TYPE})yes, TYPE_USE

So three of the six (jakarta, JetBrains, Micronaut) are explicitly TYPE_USE annotations, and javax is implicitly usable in TYPE_USE positions. Only Micrometer and Spring are purely declaration-position.

Your concern is valid for at least half the migration paths — for those annotations, @Nullable X[] on the element type genuinely has different semantics than X @Nullable[] on the array. The recipe is making a deliberate semantic choice to reposition the annotation to mean "array can be null" rather than "elements can be null."

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could be correct on that. I guess I'm just not as familiar with the intricacies of different targets for annotations and how they affect the interpretation of the statement. Given we're targetting specific sets of annotations, it's probably fine, if just possible to misuse given there aren't target guards in the recipe if you were for example to give it JSpecify annotation FQNs to start with. Would it make sense to just guard against asking to move JSpecify annotations to JSpecify annotations?

@timtebeek timtebeek marked this pull request as draft April 2, 2026 18:41
Jakarta, JetBrains, and Micronaut annotations target TYPE_USE, so
`@Nullable X[]` (element nullable) already has distinct semantics from
`X @nullable[]` (array nullable). Moving them would change meaning.
@timtebeek timtebeek marked this pull request as ready for review April 2, 2026 19:03
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Apr 2, 2026
@timtebeek timtebeek merged commit 9e138a7 into main Apr 2, 2026
1 check passed
@timtebeek timtebeek deleted the tim/issue-934-investigation branch April 2, 2026 19:18
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Apr 2, 2026
mergify Bot added a commit to robfrank/linklift that referenced this pull request May 3, 2026
…30.1 to 3.34.0 [skip ci]

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

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

> 3.34.0
> ------
>
> What's Changed
> --------------
>
> * Ensure `case` always has a space before the label in `IfElseIfConstructToSwitch` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1071](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1071)
> * Add `UsePredicateNot` to replace cast-and-negate with `Predicate.not(..)` by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1072](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1072)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.33.0...v3.34.0>
>
> 3.33.1
> ------
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.33.0...v3.33.1>
>
> 3.33.0
> ------
>
> What's Changed
> --------------
>
> * Add URLEqualsHashCodeRecipes to Java best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1058](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1058)
> * Add --add-opens JVM args to surefire/failsafe for Java 25 by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1055](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1055)
> * Collapse UpgradeDockerImageVersion cartesian via ChangeFrom capture by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-migrate-java#1060](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1060)
> * Add NoJodaTime to Java best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1061](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1061)
> * Support `copyOf` migration for Guava by [`@​greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-migrate-java#1062](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1062)
> * Rename `_` identifier to `__` for Java ≤ 8 source files by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-migrate-java#1063](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1063)
> * Fix `ClassCastException` on parenthesized initializers in `var` recipes by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1064](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1064)
> * Gate Kotlin-based Java version cap on actual Kotlin sources by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1066](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1066)
> * Fix `MigrateCollections*` template errors on complex expression contexts by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1067](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1067)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.32.1...v3.33.0>
>
> v3.32.1
> -------
>
> What's Changed
> --------------
>
> * Remove parentheses from single unnamed lambda parameters by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1054](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1054)
> * Skip Kotlin and Groovy files in ReplaceUnusedVariablesWithUnderscore by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1056](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1056)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.32.0...v3.32.1>
>
> 3.32.0
> ------
>
> What's Changed
> --------------
>
> * Cap Java version at 24 for Kotlin <2.3 in UpgradeToJava25 by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1035](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1035)
> * Inline JavaTemplate fields at their usage sites by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1036](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1036)
> * Use `JavaTemplate.apply()` static method by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1037](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1037)
> * Fix ClassCastException in UseVarForGenericMethodInvocations by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-migrate-java#1039](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1039)
> * Upgrade Mockito to 5.17.x in Java 25 migration recipe by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1041](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1041)
> * Migrate Mojarra and Glassfish JSF dependencies to Jakarta Faces by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1040](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1040)
> * Upgrade jakarta.annotation-api to 3.0.x in Jakarta EE 11 recipe by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1042](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1042)
> * Move type-use annotations to array brackets during JSpecify migration by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1038](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1038)
> * Add explicit casts for visitor visit() return type by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1049](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1049)
> * Add UseVarForTypeCast and UseVarForConstructors to UseVar recipe by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1050](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1050)
> * Replace Collections.emptyXXX with Immutable Static Factory Methods by [`@​mkarg`](https://github.com/mkarg) in [openrewrite/rewrite-migrate-java#1045](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1045)
> * Fix `AddMissingMethodImplementation` generating stubs for inherited methods by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1051](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1051)
> * Add EnableLombokAnnotationProcessor to LombokBestPractices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1052](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1052)

... (truncated)


Commits

* [`61c1777`](openrewrite/rewrite-migrate-java@61c1777) Extract documentation examples from tests
* [`95028a5`](openrewrite/rewrite-migrate-java@95028a5) Add `UsePredicateNot` to replace cast-and-negate with `Predicate.not(..)` ([#1](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1)...
* [`a58b8b2`](openrewrite/rewrite-migrate-java@a58b8b2) Ensure `case` always has a space before the label in `IfElseIfConstructToSwit...
* [`eac4344`](openrewrite/rewrite-migrate-java@eac4344) Fix `MigrateCollections*` template errors on complex expression contexts ([#1067](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1067))
* [`0ca5cc8`](openrewrite/rewrite-migrate-java@0ca5cc8) Extract documentation examples from tests
* [`968a22f`](openrewrite/rewrite-migrate-java@968a22f) Gate Kotlin-based Java version cap on actual Kotlin sources ([#1066](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1066))
* [`a471dd1`](openrewrite/rewrite-migrate-java@a471dd1) [Auto] SDKMAN! Java candidates as of 2026-04-20T1116
* [`3b7b950`](openrewrite/rewrite-migrate-java@3b7b950) Fix `ClassCastException` on parenthesized initializers in `var` recipes ([#1064](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1064))
* [`461609f`](openrewrite/rewrite-migrate-java@461609f) Rename `_` identifier to `__` for Java ≤ 8 source files ([#1063](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1063))
* [`634e7d9`](openrewrite/rewrite-migrate-java@634e7d9) Support `copyOf` migration for Guava ([#1062](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1062))
* Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.30.1...v3.34.0)
  
[![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.30.1&new-version=3.34.0)](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.

MigrateFromJavaxAnnotationApi does not modify annotations to the type level

2 participants