Java best practices
MSALApplies 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. JavaBestPracticesmod run . --recipe= org. openrewrite. java. migrate. JavaBestPracticesInstall 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.
- Migrate to Java 25
- Migrate
public static void main(String[] args)to instancevoid main() - Extract complex
super(..)andthis(..)arguments into local variables - Replace unused variables with underscore
- Use text blocks
- Prefer
String.formatted(Object...) - Prefer the Java standard library instead of Guava
- Prefer the Java standard library instead of Joda-Time
- JSpecify best practices
- Use
varfor variables initialized with type casts - Use
varfor constructor call assignments - Apply
varto generic constructors - Apply
varto generic method invocations - Use
varfor primitive and String variables - Use modernized
java.utilAPIs - Equals avoids null
- Add missing
@Overrideto overriding and implementing methods - Week Year (YYYY) should not be used for date formatting
hashCode()should not be called on array instances- Remove
toString()calls on arrays - Replace
Object.notify()withObject.notifyAll() - Remove garbage collection invocations
- Remove
Object.finalize()invocations URL#equals()andURL#hashCode()- Simplify lambda blocks to expressions
- Use method references in lambda
- Use the diamond operator
- Remove unnecessary parentheses
- Remove redundant null checks before
instanceof - Replace
StringBuilder#appendwithString - Use
Collectioninterfaces - Enum values should be compared with
== - Combine semantically equal catch blocks
- Use
String::replace()when first parameter is not a real regular expression - Use
StandardCharsetsconstants - Use
System.lineSeparator() - Remove redundant casts
- Replace invocations of
Collections#sort(List, Comparator)withList#sort(Comparator) - Use
String.contentEquals(CharSequence)instead ofString.equals(CharSequence.toString()) - Convert Javadoc to Markdown documentation comments
- Remove unused imports