Java best practices

MSAL

Applies 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.

org.openrewrite.java.migrate.JavaBestPractices
mod run . --recipe=org.openrewrite.java.migrate.JavaBestPractices

Install it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.

Recipe list

It runs these recipes, in this order.

  1. Migrate to Java 25
  2. Migrate public static void main(String[] args) to instance void main()
  3. Extract complex super(..) and this(..) arguments into local variables
  4. Replace unused variables with underscore
  5. Use text blocks
  6. Prefer String.formatted(Object...)
  7. Prefer the Java standard library instead of Guava
  8. Prefer the Java standard library instead of Joda-Time
  9. JSpecify best practices
  10. Use var for variables initialized with type casts
  11. Use var for constructor call assignments
  12. Apply var to generic constructors
  13. Apply var to generic method invocations
  14. Use var for primitive and String variables
  15. Use modernized java.util APIs
  16. Equals avoids null
  17. Add missing @Override to overriding and implementing methods
  18. Week Year (YYYY) should not be used for date formatting
  19. hashCode() should not be called on array instances
  20. Remove toString() calls on arrays
  21. Replace Object.notify() with Object.notifyAll()
  22. Remove garbage collection invocations
  23. Remove Object.finalize() invocations
  24. URL#equals() and URL#hashCode()
  25. Simplify lambda blocks to expressions
  26. Use method references in lambda
  27. Use the diamond operator
  28. Remove unnecessary parentheses
  29. Remove redundant null checks before instanceof
  30. Replace StringBuilder#append with String
  31. Use Collection interfaces
  32. Enum values should be compared with ==
  33. Combine semantically equal catch blocks
  34. Use String::replace() when first parameter is not a real regular expression
  35. Use StandardCharsets constants
  36. Use System.lineSeparator()
  37. Remove redundant casts
  38. Replace invocations of Collections#sort(List, Comparator) with List#sort(Comparator)
  39. Use String.contentEquals(CharSequence) instead of String.equals(CharSequence.toString())
  40. Convert Javadoc to Markdown documentation comments
  41. Remove unused imports