Migrate to Java 11
org.openrewrite.java.migrate.Java8toJava11
This recipe will apply changes commonly needed when upgrading to Java 11. Specifically, for those applications that are built on Java 8, this recipe will update and add dependencies on J2EE libraries that are no longer directly bundled with the JDK. 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 11 as the target/source and plugins will be also be upgraded to versions that are compatible with Java 11.
Tags
Recipe source
GitHub, Issue Tracker, Maven Central
This recipe is composed of more than one recipe. If you want to customize the set of recipes this is composed of, you can find and copy the GitHub source for the recipe from the link above.
This recipe is available under the Moderne Source Available License.
Definition
- Recipe List
- Yaml Recipe List
- Migrate to Java 8
- Prefer
java.util.Base64instead ofsun.misc- useMimeCoder:
false
- useMimeCoder:
- Remove explicit casts on
Arrays.asList(..).toArray() - Add explicit JAXB API dependencies and runtime
- Add explicit JAX-WS dependencies
- Add explicit Inject dependencies
- Add explicit Common Annotations dependencies
BigDecimalrounding constants toRoundingModeenums- Use primitive wrapper
valueOfmethod - Use modernized
java.util.concurrentAPIs - Use modernized
java.langAPIs - Remove method invocations
- methodPattern:
java.lang.Runtime runFinalizersOnExit(boolean)
- methodPattern:
- Remove method invocations
- methodPattern:
java.lang.System runFinalizersOnExit(boolean)
- methodPattern:
- Use modernized
java.util.loggingAPIs - Migrate Lombok to a Java 11 compatible version
- Use modernized
java.netAPIs - Replace
Paths.getwithPath.of - Use modernized
java.sqlAPIs - Use modernized
javax.lang.model.utilAPIs - Use modernized
javax.management.monitorAPIs - Use modernized
javax.xml.streamAPIs - Remove Cobertura Maven plugin
- Upgrade build to Java 11
- Prefer
Optional.isEmpty() - Prefer
Optional.isPresent() Stream<Optional>idiom recipe- Use
com.sun.xml.bind.*instead ofcom.sun.xml.internal.bind.* - Replace deprecated methods in
SecurityManager - Upgrade plugins to Java 11 compatible versions
- Replace
javax.security.auth.Policywithjava.security.Policy - Replace
java.lang.ref.Reference.clone()with constructor call - Remove
Thread.destroy()andThread.stop(Throwable) - Replace AWT
getPeer()method- getPeerMethodPattern:
java.awt.* getPeer() - lightweightPeerFQCN:
java.awt.peer.LightweightPeer
- getPeerMethodPattern:
- Migrate to Scala 2.12.+
- Replace
com.sun.awt.AWTUtilitiesstatic method invocations- getAWTIsWindowsTranslucencyPattern:
com.sun.awt.AWTUtilities isTranslucencySupported(com.sun.awt.AWTUtilities.Translucency) - isWindowOpaquePattern:
com.sun.awt.AWTUtilities isWindowOpaque(java.awt.Window) - isTranslucencyCapablePattern:
com.sun.awt.AWTUtilities isTranslucencyCapable(java.awt.GraphicsConfiguration) - setWindowOpacityPattern:
com.sun.awt.AWTUtilities setWindowOpacity(java.awt.Window, float) - getWindowOpacityPattern:
com.sun.awt.AWTUtilities getWindowOpacity(java.awt.Window) - getWindowShapePattern:
com.sun.awt.AWTUtilities getWindowShape(java.awt.Window) - setComponentMixingCutoutShapePattern:
com.sun.awt.AWTUtilities setComponentMixingCutoutShape(java.awt.Component,java.awt.Shape)
- getAWTIsWindowsTranslucencyPattern:
- Replace
getLocalizedInputStreamandgetLocalizedOutputStreamwith direct assignment- localizedInputStreamMethodMatcher:
java.lang.Runtime getLocalizedInputStream(java.io.InputStream) - localizedOutputStreamMethodMatcher:
java.lang.Runtime getLocalizedOutputStream(java.io.OutputStream)
- localizedInputStreamMethodMatcher:
- Catch
TypeNotPresentExceptionthrown byClass.getAnnotation() - Replace
IllegalArgumentExceptionwithAlreadyConnectedExceptioninDatagramChannel.send()method - Return String
jkswhenKeyStore.getDefaultType()is called
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.Java8toJava11
displayName: Migrate to Java 11
description: |
This recipe will apply changes commonly needed when upgrading to Java 11. Specifically, for those applications that are built on Java 8, this recipe will update and add dependencies on J2EE libraries that are no longer directly bundled with the JDK. 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 11 as the target/source and plugins will be also be upgraded to versions that are compatible with Java 11.
tags:
- jaxb
- deprecated
- java11
- jaxws
- jakarta
recipeList:
- org.openrewrite.java.migrate.UpgradeToJava8
- org.openrewrite.java.migrate.UseJavaUtilBase64:
useMimeCoder: false
- org.openrewrite.java.migrate.CastArraysAsListToList
- org.openrewrite.java.migrate.javax.AddJaxbDependenciesWithRuntime
- org.openrewrite.java.migrate.javax.AddJaxwsDependencies
- org.openrewrite.java.migrate.javax.AddInjectDependencies
- org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies
- org.openrewrite.staticanalysis.BigDecimalRoundingConstantsToEnums
- org.openrewrite.staticanalysis.PrimitiveWrapperClassConstructorToValueOf
- org.openrewrite.java.migrate.concurrent.JavaConcurrentAPIs
- org.openrewrite.java.migrate.lang.JavaLangAPIs
- org.openrewrite.java.RemoveMethodInvocations:
methodPattern: java.lang.Runtime runFinalizersOnExit(boolean)
- org.openrewrite.java.RemoveMethodInvocations:
methodPattern: java.lang.System runFinalizersOnExit(boolean)
- org.openrewrite.java.migrate.logging.JavaLoggingAPIs
- org.openrewrite.java.migrate.lombok.UpdateLombokToJava11
- org.openrewrite.java.migrate.net.JavaNetAPIs
- org.openrewrite.java.migrate.nio.file.PathsGetToPathOf
- org.openrewrite.java.migrate.sql.JavaSqlAPIs
- org.openrewrite.java.migrate.javax.JavaxLangModelUtil
- org.openrewrite.java.migrate.javax.JavaxManagementMonitorAPIs
- org.openrewrite.java.migrate.javax.JavaxXmlStreamAPIs
- org.openrewrite.java.migrate.cobertura.RemoveCoberturaMavenPlugin
- org.openrewrite.java.migrate.UpgradeBuildToJava11
- org.openrewrite.java.migrate.util.OptionalNotPresentToIsEmpty
- org.openrewrite.java.migrate.util.OptionalNotEmptyToIsPresent
- org.openrewrite.java.migrate.util.OptionalStreamRecipe
- org.openrewrite.java.migrate.InternalBindPackages
- org.openrewrite.java.migrate.RemovedSecurityManagerMethods
- org.openrewrite.java.migrate.UpgradePluginsForJava11
- org.openrewrite.java.migrate.RemovedPolicy
- org.openrewrite.java.migrate.ReferenceCloneMethod
- org.openrewrite.java.migrate.ThreadStopDestroy
- org.openrewrite.java.migrate.ReplaceAWTGetPeerMethod:
getPeerMethodPattern: java.awt.* getPeer()
lightweightPeerFQCN: java.awt.peer.LightweightPeer
- org.openrewrite.scala.migrate.UpgradeScala_2_12
- org.openrewrite.java.migrate.ReplaceComSunAWTUtilitiesMethods:
getAWTIsWindowsTranslucencyPattern: com.sun.awt.AWTUtilities isTranslucencySupported(com.sun.awt.AWTUtilities.Translucency)
isWindowOpaquePattern: com.sun.awt.AWTUtilities isWindowOpaque(java.awt.Window)
isTranslucencyCapablePattern: com.sun.awt.AWTUtilities isTranslucencyCapable(java.awt.GraphicsConfiguration)
setWindowOpacityPattern: com.sun.awt.AWTUtilities setWindowOpacity(java.awt.Window, float)
getWindowOpacityPattern: com.sun.awt.AWTUtilities getWindowOpacity(java.awt.Window)
getWindowShapePattern: com.sun.awt.AWTUtilities getWindowShape(java.awt.Window)
setComponentMixingCutoutShapePattern: com.sun.awt.AWTUtilities setComponentMixingCutoutShape(java.awt.Component,java.awt.Shape)
- org.openrewrite.java.migrate.ReplaceLocalizedStreamMethods:
localizedInputStreamMethodMatcher: java.lang.Runtime getLocalizedInputStream(java.io.InputStream)
localizedOutputStreamMethodMatcher: java.lang.Runtime getLocalizedOutputStream(java.io.OutputStream)
- org.openrewrite.java.migrate.ArrayStoreExceptionToTypeNotPresentException
- org.openrewrite.java.migrate.IllegalArgumentExceptionToAlreadyConnectedException
- org.openrewrite.java.migrate.ChangeDefaultKeyStore
Used by
This recipe is used as part of the following composite recipes:
Examples
Example 1
PathsGetToPathOfTest#convertPathsGetToPathOf
- java
- Diff
Before
import java.nio.file.Path;
import java.nio.file.Paths;
import java.net.URI;
class A {
Path pathA = Paths.get("path");
Path pathB = Paths.get("path", "subpath");
Path pathC = Paths.get(URI.create("file:///path"));
}
After
import java.nio.file.Path;
import java.net.URI;
class A {
Path pathA = Path.of("path");
Path pathB = Path.of("path", "subpath");
Path pathC = Path.of(URI.create("file:///path"));
}
@@ -2,1 +2,0 @@
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.net.URI;
@@ -5,3 +4,3 @@
import java.net.URI;
class A {
- Path pathA = Paths.get("path");
- Path pathB = Paths.get("path", "subpath");
- Path pathC = Paths.get(URI.create("file:///path"));
+ Path pathA = Path.of("path");
+ Path pathB = Path.of("path", "subpath");
+ Path pathC = Path.of(URI.create("file:///path"));
}
Example 2
PathsGetToPathOfTest#convertPathsGetToPathOf
- java
- Diff
Before
import java.nio.file.Path;
import java.nio.file.Paths;
import java.net.URI;
class A {
Path pathA = Paths.get("path");
Path pathB = Paths.get("path", "subpath");
Path pathC = Paths.get(URI.create("file:///path"));
}
After
import java.nio.file.Path;
import java.net.URI;
class A {
Path pathA = Path.of("path");
Path pathB = Path.of("path", "subpath");
Path pathC = Path.of(URI.create("file:///path"));
}
@@ -2,1 +2,0 @@
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.net.URI;
@@ -5,3 +4,3 @@
import java.net.URI;
class A {
- Path pathA = Paths.get("path");
- Path pathB = Paths.get("path", "subpath");
- Path pathC = Paths.get(URI.create("file:///path"));
+ Path pathA = Path.of("path");
+ Path pathB = Path.of("path", "subpath");
+ Path pathC = Path.of(URI.create("file:///path"));
}
Usage
This recipe has no required configuration options. It can be activated by adding a dependency on org.openrewrite.recipe:rewrite-migrate-java in your build file or by running a shell command (in which case no build changes are needed):
- Gradle
- Gradle init script
- Maven POM
- Maven Command Line
- Moderne CLI
- Add the following to your
build.gradlefile:
plugins {
id("org.openrewrite.rewrite") version("latest.release")
}
rewrite {
activeRecipe("org.openrewrite.java.migrate.Java8toJava11")
setExportDatatables(true)
}
repositories {
mavenCentral()
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-migrate-java:3.20.0")
}
- Run
gradle rewriteRunto run the recipe.
- Create a file named
init.gradlein the root of your project.
initscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies { classpath("org.openrewrite:plugin:7.19.0") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("org.openrewrite.recipe:rewrite-migrate-java:3.20.0")
}
rewrite {
activeRecipe("org.openrewrite.java.migrate.Java8toJava11")
setExportDatatables(true)
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
}
- Run the recipe.
gradle --init-script init.gradle rewriteRun
- Add the following to your
pom.xmlfile:
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>6.22.1</version>
<configuration>
<exportDatatables>true</exportDatatables>
<activeRecipes>
<recipe>org.openrewrite.java.migrate.Java8toJava11</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-migrate-java</artifactId>
<version>3.20.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
- Run
mvn rewrite:runto run the recipe.
You will need to have Maven installed on your machine before you can run the following command.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-migrate-java:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.migrate.Java8toJava11 -Drewrite.exportDatatables=true
You will need to have configured the Moderne CLI on your machine before you can run the following command.
mod run . --recipe Java8toJava11
If the recipe is not available locally, then you can install it using:
mod config recipes jar install org.openrewrite.recipe:rewrite-migrate-java:3.20.0
See how this recipe works across multiple open-source repositories
Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.
Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.
Data Tables
- SourcesFileResults
- SourcesFileErrors
- RecipeRunStats
- MavenMetadataFailures
Source files that had results
org.openrewrite.table.SourcesFileResults
Source files that were modified by the recipe run.
| Column Name | Description |
|---|---|
| Source path before the run | The source path of the file before the run. null when a source file was created during the run. |
| Source path after the run | A recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run. |
| Parent of the recipe that made changes | In a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all. |
| Recipe that made changes | The specific recipe that made a change. |
| Estimated time saving | An estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds. |
| Cycle | The recipe cycle in which the change was made. |
Source files that errored on a recipe
org.openrewrite.table.SourcesFileErrors
The details of all errors produced by a recipe run.
| Column Name | Description |
|---|---|
| Source path | The file that failed to parse. |
| Recipe that made changes | The specific recipe that made a change. |
| Stack trace | The stack trace of the failure. |
Recipe performance
org.openrewrite.table.RecipeRunStats
Statistics used in analyzing the performance of recipes.
| Column Name | Description |
|---|---|
| The recipe | The recipe whose stats are being measured both individually and cumulatively. |
| Source file count | The number of source files the recipe ran over. |
| Source file changed count | The number of source files which were changed in the recipe run. Includes files created, deleted, and edited. |
| Cumulative scanning time (ns) | The total time spent across the scanning phase of this recipe. |
| Max scanning time (ns) | The max time scanning any one source file. |
| Cumulative edit time (ns) | The total time spent across the editing phase of this recipe. |
| Max edit time (ns) | The max time editing any one source file. |
Maven metadata failures
org.openrewrite.maven.table.MavenMetadataFailures
Attempts to resolve maven metadata that failed.
| Column Name | Description |
|---|---|
| Group id | The groupId of the artifact for which the metadata download failed. |
| Artifact id | The artifactId of the artifact for which the metadata download failed. |
| Version | The version of the artifact for which the metadata download failed. |
| Maven repository | The URL of the Maven repository that the metadata download failed on. |
| Snapshots | Does the repository support snapshots. |
| Releases | Does the repository support releases. |
| Failure | The reason the metadata download failed. |