Skip to content

Commit

Permalink
Replace Validate.notNull(String) as well (#458)
Browse files Browse the repository at this point in the history
Fixes #457
  • Loading branch information
timtebeek authored Feb 7, 2025
1 parent 09ea6c0 commit 134cc38
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/resources/META-INF/rewrite/java-best-practices.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ 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
displayName: Replace `org.apache.commons.lang3.Validate#notNull` with `Objects#requireNonNull`
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 134cc38

Please sign in to comment.