Java best practices
Recipe ID
org.openrewrite.java.migrate.JavaBestPracticesArtifactorg.openrewrite.recipe:rewrite-migrate-javaApplies opinionated best practices for Java projects targeting Java 25. This recipe includes the full Java 25 upgrade chain plus additional improvements to code style, API usage, and third-party dependency reduction that go beyond what the version migration recipes apply.
Composite recipebest-practicesjava25MSAL
Add to your build
Get a download tokenThis recipe is Moderne licensed, so the token must belong to a Moderne customer. It replaces YOUR_DOWNLOAD_TOKEN below.
~/.gradle/gradle.properties
codeGenomeUsername=you@example.com
codeGenomeToken=YOUR_DOWNLOAD_TOKEN
build.gradle.kts
plugins {
id("org.openrewrite.rewrite") version("latest.release")
}
rewrite {
activeRecipe("org.openrewrite.java.migrate.JavaBestPractices")
}
repositories {
mavenCentral()
maven {
url = uri("https://artifacts.codegenomeproject.org/maven")
credentials {
username = providers.gradleProperty("codeGenomeUsername").get()
password = providers.gradleProperty("codeGenomeToken").get()
}
}
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-migrate-java:3.45.0")
}
Then run
./gradlew rewriteRunUsage
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.JavaBestPracticesIf the recipe isn’t available locally, install it with:
mod config recipes jar install org.openrewrite.recipe:rewrite-migrate-java:RELEASEDefinition
This recipe runs the following recipes in order.
- Migrate to Java 25
org.openrewrite.java.migrate.UpgradeToJava25 - Migrate
public static void main(String[] args)to instancevoid main()org.openrewrite.java.migrate.lang.MigrateMainMethodToInstanceMain - Extract complex
super(..)andthis(..)arguments into local variablesorg.openrewrite.java.migrate.lang.ExtractExplicitConstructorInvocationArguments - Replace unused variables with underscore
org.openrewrite.java.migrate.lang.ReplaceUnusedVariablesWithUnderscore - Use text blocks
org.openrewrite.java.migrate.lang.UseTextBlocks - Prefer
String.formatted(Object...)org.openrewrite.java.migrate.lang.StringFormatted - Prefer the Java standard library instead of Guava
org.openrewrite.java.migrate.guava.NoGuava - Prefer the Java standard library instead of Joda-Time
org.openrewrite.java.joda.time.NoJodaTime - JSpecify best practices
org.openrewrite.java.jspecify.JSpecifyBestPractices - Use
varfor variables initialized with type castsorg.openrewrite.java.migrate.lang.var.UseVarForTypeCast - Use
varfor constructor call assignmentsorg.openrewrite.java.migrate.lang.var.UseVarForConstructors - Apply
varto Generic Constructorsorg.openrewrite.java.migrate.lang.var.UseVarForGenericsConstructors - Apply
varto generic method invocationsorg.openrewrite.java.migrate.lang.var.UseVarForGenericMethodInvocations - Use
varfor primitive and String variablesorg.openrewrite.java.migrate.lang.var.UseVarForPrimitive - Use modernized
java.utilAPIsorg.openrewrite.java.migrate.util.JavaUtilAPIs - Equals avoids null
org.openrewrite.staticanalysis.EqualsAvoidsNull - Add missing
@Overrideto overriding and implementing methodsorg.openrewrite.staticanalysis.MissingOverrideAnnotation - Week Year (YYYY) should not be used for date formatting
org.openrewrite.staticanalysis.ReplaceWeekYearWithYear hashCode()should not be called on array instancesorg.openrewrite.staticanalysis.RemoveHashCodeCallsFromArrayInstances- Remove
toString()calls on arraysorg.openrewrite.staticanalysis.RemoveToStringCallsFromArrayInstances - Replaces
Object.notify()withObject.notifyAll()org.openrewrite.staticanalysis.UseObjectNotifyAll - Remove garbage collection invocations
org.openrewrite.staticanalysis.RemoveCallsToSystemGc - Remove
Object.finalize()invocationsorg.openrewrite.staticanalysis.RemoveCallsToObjectFinalize - URL Equals and Hash Code
org.openrewrite.staticanalysis.URLEqualsHashCodeRecipes - Simplify lambda blocks to expressions
org.openrewrite.staticanalysis.LambdaBlockToExpression - Use method references in lambda
org.openrewrite.staticanalysis.ReplaceLambdaWithMethodReference - Use the diamond operator
org.openrewrite.staticanalysis.UseDiamondOperator - Remove unnecessary parentheses
org.openrewrite.staticanalysis.UnnecessaryParentheses - Remove redundant null checks before instanceof
org.openrewrite.staticanalysis.RemoveRedundantNullCheckBeforeInstanceof - Replace
StringBuilder#appendwithStringorg.openrewrite.staticanalysis.ReplaceStringBuilderWithString - Use
Collectioninterfacesorg.openrewrite.staticanalysis.UseCollectionInterfaces - Enum values should be compared with "=="
org.openrewrite.staticanalysis.CompareEnumsWithEqualityOperator - Combine semantically equal catch blocks
org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks - Use
String::replace()when first parameter is not a real regular expressionorg.openrewrite.staticanalysis.UseStringReplace - Use
StandardCharsetconstantsorg.openrewrite.staticanalysis.UseStandardCharset - Use
System.lineSeparator()org.openrewrite.staticanalysis.UseSystemLineSeparator - Remove redundant casts
org.openrewrite.staticanalysis.RemoveRedundantTypeCast - Replace
java.util.Stackwithjava.util.Dequeorg.openrewrite.staticanalysis.ReplaceStackWithDeque - Replace invocations of
Collections#sort(List, Comparator)withList#sort(Comparator)org.openrewrite.staticanalysis.UseListSort - Use
String.contentEquals(CharSequence)instead ofString.equals(CharSequence.toString())org.openrewrite.staticanalysis.EqualsToContentEquals - Convert Javadoc to Markdown documentation comments
org.openrewrite.java.migrate.lang.JavadocToMarkdownDocComment - Remove unused imports
org.openrewrite.java.RemoveUnusedImports
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