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

Replace conversion to ObjectUtils.isEmpty with !StringUtils.hasLength #605

Closed
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ dependencies {
exclude("com.google.auto.service", "auto-service-annotations")
}
implementation("org.mongodb:mongo-java-driver:3.12.+")
implementation("org.springframework:spring-core:5.3.+")
implementation("org.springframework.data:spring-data-mongodb:2.2.+"){
because("We only require the classes (for refaster style recipes), not the dependencies")
exclude(group = "org.springframework")
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/openrewrite/java/spring/StringOptimization.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openrewrite.java.spring;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.openrewrite.java.template.RecipeDescriptor;
import org.springframework.util.StringUtils;

@SuppressWarnings("ALL")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a bit broad

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also be sure to add a @RecipeDescriptor to the outer class, as that'll get turned into a Recipes class itself, which needs documentation.

public class StringOptimization {

@RecipeDescriptor(
name = "Use `!StringUtils.hasLength`",
description = "Replace usage of deprecated `StringUtils.isEmpty` with `!StringUtils.hasLength` https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/StringUtils.html#isEmpty(java.lang.Object).")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whenever you link to the Spring docs, please us a versioned doc link, so containing say /3.3.0 as opposed to /current, as those links can go bad when a next version comes out.

static class ReplaceDeprecatedStringUtilsIsEmpty {


@BeforeTemplate
boolean before(String s) {
return StringUtils.isEmpty(s);
}

@AfterTemplate
boolean after(String s) {
return !StringUtils.hasLength(s);
}
}
}
11 changes: 2 additions & 9 deletions src/main/resources/META-INF/rewrite/spring-framework-53.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ recipeList:
artifactId: "*"
newVersion: 5.3.x
overrideManagedVersion: false
- org.openrewrite.java.spring.framework.UseObjectUtilsIsEmpty
- org.openrewrite.java.spring.StringOptimizationRecipes.ReplaceDeprecatedStringUtilsIsEmptyRecipe
- org.openrewrite.java.spring.framework.MigrateHandlerInterceptor
- org.openrewrite.java.spring.framework.MigrateInstantiationAwareBeanPostProcessorAdapter
- org.openrewrite.java.spring.framework.JdbcTemplateObjectArrayArgToVarArgs
Expand All @@ -51,11 +51,4 @@ recipeList:
groupId: cglib
artifactId: cglib
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.spring.framework.UseObjectUtilsIsEmpty
displayName: Use `ObjectUtils#isEmpty(Object)`
description: '`StringUtils#isEmpty(Object)` was deprecated in 5.3.'
recipeList:
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: org.springframework.util.StringUtils isEmpty(Object)
fullyQualifiedTargetTypeName: org.springframework.util.ObjectUtils

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.openrewrite.java.spring;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class ReplaceDeprecatedStringUtilsIsEmptyTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec){
spec.recipe(new StringOptimizationRecipes()).parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(),"spring-core-5.+"));
}

@DocumentExample
@Test
@SuppressWarnings("deprecation")
void replaceStringUtilsIsEmpty() {
rewriteRun(
//language=java
java(
"""
import org.springframework.util.StringUtils;
class Test {
void test(String s) {
return StringUtils.isEmpty(s);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add a testcase where !StringUtils.isEmpty(s); is used

}
}
""",
"""
import org.springframework.util.StringUtils;
class Test {
void test(String s) {
return !StringUtils.hasLength(s);
}
}
"""
)
);
}
}