Skip to content

Add Java best practices recipe#1021

Open
timtebeek wants to merge 9 commits intomainfrom
tim/java-best-practices
Open

Add Java best practices recipe#1021
timtebeek wants to merge 9 commits intomainfrom
tim/java-best-practices

Conversation

@timtebeek
Copy link
Member

Summary

  • Adds a new org.openrewrite.java.migrate.JavaBestPractices recipe that builds on the full Java 25 upgrade chain with opinionated best practices
  • Includes NoGuava (prefer stdlib over Guava), JSpecify nullability best practices, targeted var usage (UseVarForTypeCast, UseVarForGenericsConstructors, UseVarForGenericMethodInvocations), and UseTextBlocks with convertStringsWithoutNewlines: true
  • Adds 9 broadly accepted static analysis recipes: method references, diamond operator, unnecessary parentheses, StringBuilder replacement, collection interfaces, enum equality, wrapper valueOf, BigDecimal rounding enums, multi-catch, and UseStringReplace
  • Includes RemoveUnusedImports as a cleanup pass

Test plan

  • ./gradlew test --tests JavaBestPracticesTest passes
  • Run recipe against a sample project to verify transformations

Opinionated recipe that builds on UpgradeToJava25 with additional
best practices: NoGuava, JSpecify, targeted UseVar, and broadly
accepted static analysis improvements.
PrimitiveWrapperClassConstructorToValueOf and
BigDecimalRoundingConstantsToEnums are already included
transitively via Java8toJava11.
Add stdlib preference recipes (UseEnumSetOf, UseListOf, UseSetOf,
UseStringIsEmpty), bug prevention (ReplaceWeekYearWithYear,
RemoveHashCodeCallsFromArrayInstances, RemoveToStringCallsFromArrayInstances,
UseObjectNotifyAll, RemoveCallsToSystemGc, RemoveCallsToObjectFinalize),
and modernization (UseStandardCharset, UseSystemLineSeparator,
RemoveRedundantTypeCast, ReplaceStackWithDeque, UseListSort,
EqualsToContentEquals).
@timtebeek timtebeek marked this pull request as ready for review March 21, 2026 11:01
Removed UseStringIsEmptyRecipe from Java best practices.
UseVarForGenericsConstructors/UseVarForGenericMethodInvocations
must run before UseDiamondOperator to preserve the explicit type
arguments needed for var inference.
@timtebeek timtebeek requested a review from MBoegers March 21, 2026 11:56
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Mar 21, 2026
@timtebeek timtebeek added recipe Recipe requested java java 25+ labels Mar 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Ready to Review

Development

Successfully merging this pull request may close these issues.

1 participant