From 134cc38e529e118a3667a45736e83461400a9474 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 7 Feb 2025 13:18:18 +0100 Subject: [PATCH] Replace `Validate.notNull(String)` as well (#458) Fixes https://github.com/openrewrite/rewrite-static-analysis/issues/457 --- .../META-INF/rewrite/java-best-practices.yml | 6 ++-- ...eNotNullWithObjectsRequireNonNullTest.java | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/java-best-practices.yml b/src/main/resources/META-INF/rewrite/java-best-practices.yml index 088184619..84da3e8b3 100644 --- a/src/main/resources/META-INF/rewrite/java-best-practices.yml +++ b/src/main/resources/META-INF/rewrite/java-best-practices.yml @@ -38,8 +38,8 @@ name: org.openrewrite.staticanalysis.ReplaceApacheCommonsLang3ValidateNotNullWit displayName: Replace `org.apache.commons.lang3.Validate#notNull` with `Objects#requireNonNull` description: 'Replace `org.apache.commons.lang3.Validate.notNull(..)` with `Objects.requireNonNull(..)`.' recipeList: - - org.openrewrite.staticanalysis.ReplaceValidateNotNullHavingSingleArgWithObjectsRequireNonNull - org.openrewrite.staticanalysis.ReplaceValidateNotNullHavingVarargsWithObjectsRequireNonNull + - org.openrewrite.staticanalysis.ReplaceValidateNotNullHavingSingleArgWithObjectsRequireNonNull --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.staticanalysis.ReplaceValidateNotNullHavingSingleArgWithObjectsRequireNonNull @@ -47,8 +47,8 @@ displayName: Replace `org.apache.commons.lang3.Validate#notNull` with `Objects#r description: 'Replace `org.apache.commons.lang3.Validate.notNull(Object)` with `Objects.requireNonNull(Object)`.' recipeList: - org.openrewrite.java.ChangeMethodName: - methodPattern: org.apache.commons.lang3.Validate notNull(Object) + methodPattern: org.apache.commons.lang3.Validate notNull(..) newMethodName: requireNonNull - org.openrewrite.java.ChangeMethodTargetToStatic: - methodPattern: org.apache.commons.lang3.Validate requireNonNull(Object) + methodPattern: org.apache.commons.lang3.Validate requireNonNull(..) fullyQualifiedTargetTypeName: java.util.Objects diff --git a/src/test/java/org/openrewrite/staticanalysis/ReplaceApacheCommonsLang3ValidateNotNullWithObjectsRequireNonNullTest.java b/src/test/java/org/openrewrite/staticanalysis/ReplaceApacheCommonsLang3ValidateNotNullWithObjectsRequireNonNullTest.java index 04b2fdbc1..cbd10062f 100644 --- a/src/test/java/org/openrewrite/staticanalysis/ReplaceApacheCommonsLang3ValidateNotNullWithObjectsRequireNonNullTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/ReplaceApacheCommonsLang3ValidateNotNullWithObjectsRequireNonNullTest.java @@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.Issue; import org.openrewrite.config.Environment; import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; @@ -82,6 +83,34 @@ void test(Object obj) { ); } + @Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/457") + @Test + void replaceWithOneArgumentAsString() { + rewriteRun( + //language=java + java( + """ + import org.apache.commons.lang3.Validate; + + class Test { + void test(String obj) { + Validate.notNull(obj); + } + } + """, + """ + import java.util.Objects; + + class Test { + void test(String obj) { + Objects.requireNonNull(obj); + } + } + """ + ) + ); + } + @Test void replaceMethodsWithTwoArg() { rewriteRun(