diff --git a/src/main/resources/META-INF/rewrite/java-best-practices.yml b/src/main/resources/META-INF/rewrite/java-best-practices.yml new file mode 100644 index 0000000000..d450d241e3 --- /dev/null +++ b/src/main/resources/META-INF/rewrite/java-best-practices.yml @@ -0,0 +1,76 @@ +# +# Copyright 2026 the original author or authors. +#
+# Licensed under the Moderne Source Available License (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +#
+# https://docs.moderne.io/licensing/moderne-source-available-license +#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.java.migrate.JavaBestPractices
+displayName: Java best practices
+description: >-
+ 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.
+tags:
+ - java25
+ - best-practices
+preconditions:
+ - org.openrewrite.Singleton
+recipeList:
+ # Upgrade to the latest Java version
+ - org.openrewrite.java.migrate.UpgradeToJava25
+ # Text blocks for strings without newlines (upgrade chain only does convertStringsWithoutNewlines: false)
+ - org.openrewrite.java.migrate.lang.UseTextBlocks:
+ convertStringsWithoutNewlines: true
+ # Prefer Java standard library over Guava where possible
+ - org.openrewrite.java.migrate.guava.NoGuava
+ # JSpecify nullability annotations best practices
+ - org.openrewrite.java.jspecify.JSpecifyBestPractices
+ # Local variable type inference — targeted, non-controversial uses
+ # NOTE: UseVarForGenericsConstructors and UseVarForGenericMethodInvocations must run
+ # before UseDiamondOperator, as they need the explicit type arguments on constructors
+ # to safely infer the type for var. Diamond would erase that information.
+ - org.openrewrite.java.migrate.lang.var.UseVarForTypeCast
+ - org.openrewrite.java.migrate.lang.var.UseVarForConstructors
+ - org.openrewrite.java.migrate.lang.var.UseVarForGenericsConstructors
+ - org.openrewrite.java.migrate.lang.var.UseVarForGenericMethodInvocations
+ # Prefer modern Java collection factories and utilities
+ - org.openrewrite.java.migrate.util.UseEnumSetOf
+ - org.openrewrite.java.migrate.util.UseListOf
+ - org.openrewrite.java.migrate.util.UseSetOf
+ # Static analysis: bug prevention
+ - org.openrewrite.staticanalysis.ReplaceWeekYearWithYear
+ - org.openrewrite.staticanalysis.RemoveHashCodeCallsFromArrayInstances
+ - org.openrewrite.staticanalysis.RemoveToStringCallsFromArrayInstances
+ - org.openrewrite.staticanalysis.UseObjectNotifyAll
+ - org.openrewrite.staticanalysis.RemoveCallsToSystemGc
+ - org.openrewrite.staticanalysis.RemoveCallsToObjectFinalize
+ # Static analysis: modernization and cleanup
+ - org.openrewrite.staticanalysis.ReplaceLambdaWithMethodReference
+ - org.openrewrite.staticanalysis.UseDiamondOperator
+ - org.openrewrite.staticanalysis.UnnecessaryParentheses
+ - org.openrewrite.staticanalysis.ReplaceStringBuilderWithString
+ - org.openrewrite.staticanalysis.UseCollectionInterfaces
+ - org.openrewrite.staticanalysis.CompareEnumsWithEqualityOperator
+ - org.openrewrite.staticanalysis.CombineSemanticallyEqualCatchBlocks
+ - org.openrewrite.staticanalysis.UseStringReplace
+ - org.openrewrite.staticanalysis.UseStandardCharset
+ - org.openrewrite.staticanalysis.UseSystemLineSeparator
+ - org.openrewrite.staticanalysis.RemoveRedundantTypeCast
+ - org.openrewrite.staticanalysis.ReplaceStackWithDeque
+ - org.openrewrite.staticanalysis.UseListSort
+ - org.openrewrite.staticanalysis.EqualsToContentEquals
+ # Cleanup imports left behind by transformations
+ - org.openrewrite.java.RemoveUnusedImports
diff --git a/src/main/resources/META-INF/rewrite/recipes.csv b/src/main/resources/META-INF/rewrite/recipes.csv
index 6d4792219b..6c090e4c16 100644
--- a/src/main/resources/META-INF/rewrite/recipes.csv
+++ b/src/main/resources/META-INF/rewrite/recipes.csv
@@ -40,6 +40,7 @@ maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.R
maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.RemovedJaxBModuleProvided,Do not package `java.xml.bind` and `java.activation` modules in WebSphere Liberty applications,"The `java.xml.bind` and `java.activation` modules were removed in Java11. Websphere Liberty provides its own implementation of the modules, which can be used by specifying the `jaxb-2.2` feature in the server.xml file. This recipe updates the `javax.xml.bind` and `javax.activation` dependencies to use the `provided` scope to avoid class loading issues.",5,,,Modernize,Java,,,Modernize your code to best use the project's current JDK version. Take advantage of newly available APIs and reduce the dependency of your code on third party dependencies where there is equivalent functionality in the Java standard library.,Basic building blocks for transforming Java code.,,
maven,org.openrewrite.recipe:rewrite-migrate-java,org.openrewrite.java.migrate.WasDevMvnChangeParentArtifactId,Change `net.wasdev.maven.parent:java8-parent` to `:parent`,This recipe changes the artifactId of the `
+ * Licensed under the Moderne Source Available License (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://docs.moderne.io/licensing/moderne-source-available-license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openrewrite.java.migrate;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.maven.Assertions.pomXml;
+
+class JavaBestPracticesTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipeFromResources("org.openrewrite.java.migrate.JavaBestPractices");
+ }
+
+ @DocumentExample
+ @Test
+ void updateCompilerVersion() {
+ rewriteRun(
+ //language=xml
+ pomXml(
+ """
+