Skip to content

Add Java best practices recipe#1021

Merged
timtebeek merged 12 commits intomainfrom
tim/java-best-practices
Mar 24, 2026
Merged

Add Java best practices recipe#1021
timtebeek merged 12 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
timtebeek and others added 3 commits March 23, 2026 19:43
Include UseMapOf alongside UseListOf, UseSetOf, and UseEnumSetOf
for consistent coverage of modern collection factory methods.
Copy link
Contributor

@MBoegers MBoegers left a comment

Choose a reason for hiding this comment

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

Added UseMapOf for completeness — the collection factories section now covers List, Set, Map, and EnumSet uniformly.

@timtebeek timtebeek merged commit fd3bfe3 into main Mar 24, 2026
1 check passed
@timtebeek timtebeek deleted the tim/java-best-practices branch March 24, 2026 21:02
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants