-
Notifications
You must be signed in to change notification settings - Fork 85
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
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).") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
static class ReplaceDeprecatedStringUtilsIsEmpty { | ||
|
||
|
||
@BeforeTemplate | ||
boolean before(String s) { | ||
return StringUtils.isEmpty(s); | ||
} | ||
|
||
@AfterTemplate | ||
boolean after(String s) { | ||
return !StringUtils.hasLength(s); | ||
} | ||
} | ||
} |
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also add a testcase where |
||
} | ||
} | ||
""", | ||
""" | ||
import org.springframework.util.StringUtils; | ||
class Test { | ||
void test(String s) { | ||
return !StringUtils.hasLength(s); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 aRecipes
class itself, which needs documentation.