Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle generics in PreferJavaUtilObjectsRequireNonNull #538

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ public class NoGuavaRefaster {
description = "Migrate from Guava `Preconditions.checkNotNull` to Java 8 `java.util.Objects.requireNonNull`."
)
public static class PreconditionsCheckNotNullToObjectsRequireNonNull {
@BeforeTemplate
Object before(Object object) {
return com.google.common.base.Preconditions.checkNotNull(object);
}

@AfterTemplate
Object after(Object object) {
return java.util.Objects.requireNonNull(object);
}
}

@RecipeDescriptor(
name = "`Preconditions.checkNotNull` with message to `Objects.requireNonNull`",
description = "Migrate from Guava `Preconditions.checkNotNull` to Java 8 `java.util.Objects.requireNonNull`."
)
public static class PreconditionsCheckNotNullWithMessageToObjectsRequireNonNull {
@BeforeTemplate
Object before(Object object, Object message) {
return com.google.common.base.Preconditions.checkNotNull(object, message);
Expand All @@ -47,6 +63,7 @@ Object after(Object object, Object message) {
)
public static class StringValueOfString {
@BeforeTemplate
@SuppressWarnings("UnnecessaryCallToStringValueOf")
String before(String string) {
return String.valueOf(string);
}
Expand Down
16 changes: 0 additions & 16 deletions src/main/resources/META-INF/rewrite/no-guava.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ recipeList:
- org.openrewrite.java.migrate.guava.PreferJavaUtilSupplier
- org.openrewrite.java.migrate.guava.PreferJavaUtilObjectsEquals
- org.openrewrite.java.migrate.guava.PreferJavaUtilObjectsHashCode
- org.openrewrite.java.migrate.guava.PreferJavaUtilObjectsRequireNonNull
- org.openrewrite.java.migrate.guava.PreferJavaUtilCollectionsUnmodifiableNavigableMap
- org.openrewrite.java.migrate.guava.PreferJavaUtilCollectionsSynchronizedNavigableMap
- org.openrewrite.java.migrate.guava.PreferCharCompare
Expand Down Expand Up @@ -215,21 +214,6 @@ recipeList:
methodPattern: com.google.common.base.Objects hash(..)
fullyQualifiedTargetTypeName: java.util.Objects

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.guava.PreferJavaUtilObjectsRequireNonNull
displayName: Prefer `java.util.Objects#requireNonNull`
description: Prefer `java.util.Objects#requireNonNull` instead of using `com.google.common.base.Preconditions#checkNotNull`.
tags:
- guava
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: com.google.common.base.Preconditions checkNotNull(Object)
newMethodName: requireNonNull
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: com.google.common.base.Preconditions requireNonNull(Object)
fullyQualifiedTargetTypeName: java.util.Objects

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.migrate.guava.PreferJavaUtilObjectsRequireNonNullElse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
class PreferJavaUtilObjectsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResource("/META-INF/rewrite/no-guava.yml", "org.openrewrite.java.migrate.guava.NoGuava")
spec.recipe(new NoGuavaRefasterRecipes())
.parser(JavaParser.fromJavaVersion().classpath("rewrite-java", "guava"));
}

Expand Down Expand Up @@ -58,6 +58,33 @@ Object foo(Object obj) {
);
}

@Test
void preconditionsCheckNotNullToObjectsRequireNonNullStringArgument() {
rewriteRun(
//language=java
java(
"""
import com.google.common.base.Preconditions;

class A {
String foo(String str) {
return Preconditions.checkNotNull(str);
}
}
""",
"""
import java.util.Objects;

class A {
String foo(String str) {
return Objects.requireNonNull(str);
}
}
"""
)
);
}

@Test
void preconditionsCheckNotNullToObjectsRequireNonNullTwoArguments() {
rewriteRun(
Expand Down Expand Up @@ -127,11 +154,30 @@ Object foo(Object obj) {
}
""",
"""
import static java.util.Objects.requireNonNull;
import java.util.Objects;

class A {
Object foo(Object obj) {
return Objects.requireNonNull(obj);
}
}
"""
)
);
}

@Test
void preconditionsCheckNotNullWithTemplateArgument() {
// There's no direct replacement for this three arg lenient format variant
rewriteRun(
//language=java
java(
"""
import com.google.common.base.Preconditions;

class A {
Object foo(Object obj) {
return requireNonNull(obj);
return Preconditions.checkNotNull(obj, "%s", "foo");
}
}
"""
Expand Down