Migrate to Java 21
Recipe ID
org.openrewrite.java.migrate.UpgradeToJava21Artifact
org.openrewrite.recipe:rewrite-migrate-javaThis recipe will apply changes commonly needed when migrating to Java 21. This recipe will also replace deprecated API with equivalents when there is a clear migration strategy. Build files will also be updated to use Java 21 as the target/source and plugins will be also be upgraded to versions that are compatible with Java 21.
Usage
This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.
mod run . --recipe org.openrewrite.java.migrate.UpgradeToJava21If the recipe isn’t available locally, install it with:
mod config recipes jar install org.openrewrite.recipe:rewrite-migrate-java:3.41.0Definition
This recipe runs the following recipes in order.
- Migrate to Java 17
org.openrewrite.java.migrate.UpgradeToJava17 - Upgrade build to Java 21
org.openrewrite.java.migrate.UpgradeBuildToJava21 - Remove extra semicolons
org.openrewrite.staticanalysis.RemoveExtraSemicolons - Replace
Thread.resume(),Thread.stop(), andThread.suspend()withthrow new UnsupportedOperationException()org.openrewrite.java.migrate.lang.ThreadStopUnsupported - Convert
new URL(String)toURI.create(String).toURL()org.openrewrite.java.migrate.net.URLConstructorToURICreate - Adopt
SequencedCollectionorg.openrewrite.java.migrate.util.SequencedCollection - Prefer
Locale.of(..)overnew Locale(..)org.openrewrite.java.migrate.util.UseLocaleOf - Replace deprecated
Runtime#exec()methodsorg.openrewrite.staticanalysis.ReplaceDeprecatedRuntimeExecMethods - Upgrade
actions/setup-javajava-versionorg.openrewrite.github.SetupJavaUpgradeJavaVersion - Upgrade plugins to Java 21 compatible versions
org.openrewrite.java.migrate.UpgradePluginsForJava21 - Avoid using the deprecated empty
finalize()method injava.desktoporg.openrewrite.java.migrate.DeleteDeprecatedFinalize - Adopt
javax.security.auth.Subject.current()andjavax.security.auth.Subject.callAs()methods`org.openrewrite.java.migrate.RemovedSubjectMethods - If-else-if-else to switch
org.openrewrite.java.migrate.lang.IfElseIfConstructToSwitch - Add null check to existing switch cases
org.openrewrite.java.migrate.lang.NullCheckAsSwitchCase - Convert assigning Switch statements to Switch expressions
org.openrewrite.java.migrate.lang.SwitchCaseAssignmentsToSwitchExpression - Convert switch cases where every case returns into a returned switch expression
org.openrewrite.java.migrate.lang.SwitchCaseReturnsToSwitchExpression - Convert switch expression yield to arrow
org.openrewrite.java.migrate.lang.SwitchExpressionYieldToArrow - Adopt switch pattern matching (JEP 441)
org.openrewrite.java.migrate.SwitchPatternMatching - Remove illegal semicolons
org.openrewrite.java.migrate.RemoveIllegalSemicolons
Data tables
Structured output this recipe can produce.
- Maven metadata failuresAttempts to resolve maven metadata that failed.
org.openrewrite.maven.table.MavenMetadataFailures - Source files that had resultsSource files that were modified by the recipe run.
org.openrewrite.table.SourcesFileResults - Source files that had search resultsSearch results that were found during the recipe run.
org.openrewrite.table.SearchResults - Source files that errored on a recipeThe details of all errors produced by a recipe run.
org.openrewrite.table.SourcesFileErrors - Recipe performanceStatistics used in analyzing the performance of recipes.
org.openrewrite.table.RecipeRunStats