Skip to content

(WIP) Keep JSR-305 annotations on javax.annotation in the Jakarta annotation migration - #1325

Draft
myhau wants to merge 1 commit into
mainfrom
fix/jakarta-annotation-keep-jsr305
Draft

myhau wants to merge 1 commit into
mainfrom
fix/jakarta-annotation-keep-jsr305

Conversation

@myhau

@myhau myhau commented Oct 20, 2026

Copy link
Copy Markdown

What's changed?

JavaxAnnotationMigrationToJakartaAnnotation renamed the whole javax.annotation package. That package is shared with JSR-305 (CheckReturnValue, CheckForNull, ParametersAreNonnullByDefault, Nonnegative, ...), whose annotations don't exist in jakarta.annotation, so migrated code stopped compiling.

Before

import javax.annotation.CheckReturnValue;
import javax.annotation.PostConstruct;

After (main): compilation failure

import jakarta.annotation.CheckReturnValue; // cannot find symbol: class CheckReturnValue, location: package jakarta.annotation
import jakarta.annotation.PostConstruct;

After (this PR)

import jakarta.annotation.PostConstruct;

import javax.annotation.CheckReturnValue;

The recipe now:

  • renames the jakarta.annotation-api types by name with ChangeType (Generated, ManagedBean, PostConstruct, PreDestroy, Priority, Resource, Resources, and Nonnull/Nullable as before);
  • then runs the package rename in a new ChangeUnresolvedJavaxAnnotationPackage, guarded by DoesNotUseType javax.annotation.*. ChangeType only matches resolved types, so this keeps migrating Java 8 models where these annotations came from the JDK (addApiForUnattributedJdkAnnotation), without touching files that still use a resolved JSR-305 annotation.

Tests

  • keepsJsr305Annotations, migratesJsr250AnnotationsNextToJsr305, splitsWildcardImportBetweenJakartaAndJsr305 (import javax.annotation.* used for both), migratesEveryJakartaAnnotationApiType.
  • ProvidedJakartaApiScopeTest now uses an import instead of a fully qualified field, since ChangeType shortens fully qualified references.

Known limitations

  • An unresolved JSR-250 type in a file that also uses a resolved JSR-305 type stays on javax, because the guard is per file.
  • An unresolved JSR-305 type is still renamed, as on main.
  • ChangeType can add an import that shadows a same-package class with the same simple name (@javax.annotation.Resource next to a class Resource in the same package). This is a ChangeType issue in rewrite core; a fix is in progress there.

Related

…n migration

JavaxAnnotationMigrationToJakartaAnnotation renamed the whole javax.annotation
package, which also moved JSR-305 annotations such as CheckReturnValue and
CheckForNull to jakarta.annotation, where they don't exist. Rename the
jakarta.annotation-api types by name instead, and only rename the package for
references the parser could not resolve, in files without a resolved
javax.annotation type.
Comment on lines +193 to +207
name: org.openrewrite.java.migrate.jakarta.ChangeUnresolvedJavaxAnnotationPackage
displayName: Migrate unresolved `javax.annotation` types to `jakarta.annotation`
description: >-
Rename `javax.annotation` references whose types could not be resolved, such as annotations that came from the JDK
in Java 8 models. Files that still use a resolved `javax.annotation` type keep their package, as those are JSR-305
annotations without a Jakarta equivalent.
preconditions:
- org.openrewrite.java.search.DoesNotUseType:
fullyQualifiedTypeName: javax.annotation.*
recipeList:
- org.openrewrite.java.ChangePackage:
oldPackageName: javax.annotation
newPackageName: jakarta.annotation
# Avoid changing javax.annotation.concurrent and javax.annotation.processing
recursive: false

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe I'm misunderstanding, but I don't see this do very much with the precondition making it such that the ChangePackage can't match the same. What's the thinking or use case here?

@myhau myhau changed the title Keep JSR-305 annotations on javax.annotation in the Jakarta annotation migration (WIP) Keep JSR-305 annotations on javax.annotation in the Jakarta annotation migration Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants