From ef4e46be373f7fff4bc175e10ffb86a9e6ad3f93 Mon Sep 17 00:00:00 2001 From: Anshuman Mishra Date: Thu, 10 Oct 2024 15:39:25 -0700 Subject: [PATCH 1/4] Delete String.valueOf(string) to string conversion --- .../java/migrate/guava/NoGuavaRefaster.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java index 37db830d96..ea510ad0f7 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java @@ -56,21 +56,4 @@ Object after(Object object, Object message) { return java.util.Objects.requireNonNull(object, String.valueOf(message)); } } - - @RecipeDescriptor( - name = "`String.valueof(String)` to `String`", - description = "Migrate from `String.valueof(String)` to `String`, mainly as a cleanup after other recipes." - ) - public static class StringValueOfString { - @BeforeTemplate - @SuppressWarnings("UnnecessaryCallToStringValueOf") - String before(String string) { - return String.valueOf(string); - } - - @AfterTemplate - String after(String string) { - return (string); - } - } } From 7b47eb93329c5e7984c9e5b6fd57d16e994a491d Mon Sep 17 00:00:00 2001 From: Anshuman Mishra Date: Thu, 10 Oct 2024 16:16:53 -0700 Subject: [PATCH 2/4] fix test --- .../java/migrate/guava/NoGuavaRefaster.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java index ea510ad0f7..e648294e18 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java @@ -19,6 +19,8 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate; import org.openrewrite.java.template.RecipeDescriptor; +import java.util.Objects; + @RecipeDescriptor( name = "Refaster style Guava to Java migration recipes", description = "Recipes that migrate from Guava to Java, using Refaster style templates for cases beyond what declarative recipes can cover." @@ -41,11 +43,28 @@ Object after(Object object) { } } + // Note: Order of the below two recipes are important. @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, String message) { + return com.google.common.base.Preconditions.checkNotNull(object, message); + } + + @AfterTemplate + Object after(Object object, String message) { + return java.util.Objects.requireNonNull(object, message); + } + } + + @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 PreconditionsCheckNotNullWithMessageToObjectsRequireNonNullMessageTypeObject { @BeforeTemplate Object before(Object object, Object message) { return com.google.common.base.Preconditions.checkNotNull(object, message); From 848caaa5e3eabc92ed1dd38b6338f548be5d45f6 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 11 Oct 2024 10:36:26 +0200 Subject: [PATCH 3/4] Slightly tweak recipe names to show difference --- .../org/openrewrite/java/migrate/guava/NoGuavaRefaster.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java index e648294e18..577a4a9303 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java @@ -45,7 +45,7 @@ Object after(Object object) { // Note: Order of the below two recipes are important. @RecipeDescriptor( - name = "`Preconditions.checkNotNull` with message to `Objects.requireNonNull`", + name = "`Preconditions.checkNotNull` with `String` message to `Objects.requireNonNull`", description = "Migrate from Guava `Preconditions.checkNotNull` to Java 8 `java.util.Objects.requireNonNull`." ) public static class PreconditionsCheckNotNullWithMessageToObjectsRequireNonNull { @@ -61,7 +61,7 @@ Object after(Object object, String message) { } @RecipeDescriptor( - name = "`Preconditions.checkNotNull` with message to `Objects.requireNonNull`", + name = "`Preconditions.checkNotNull` with `Object` message to `Objects.requireNonNull` with `String.valueOf`", description = "Migrate from Guava `Preconditions.checkNotNull` to Java 8 `java.util.Objects.requireNonNull`." ) public static class PreconditionsCheckNotNullWithMessageToObjectsRequireNonNullMessageTypeObject { From 0a885917ec04ddeba166573dae37d217c10246ea Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 11 Oct 2024 10:38:14 +0200 Subject: [PATCH 4/4] Remove unused import --- .../org/openrewrite/java/migrate/guava/NoGuavaRefaster.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java index 577a4a9303..562f63ad8f 100644 --- a/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java +++ b/src/main/java/org/openrewrite/java/migrate/guava/NoGuavaRefaster.java @@ -19,8 +19,6 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate; import org.openrewrite.java.template.RecipeDescriptor; -import java.util.Objects; - @RecipeDescriptor( name = "Refaster style Guava to Java migration recipes", description = "Recipes that migrate from Guava to Java, using Refaster style templates for cases beyond what declarative recipes can cover."