-
Notifications
You must be signed in to change notification settings - Fork 78
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
Replace conversion to ObjectUtils.isEmpty
with !StringUtils.hasLength
#605
Conversation
import org.openrewrite.java.template.RecipeDescriptor; | ||
import org.springframework.util.StringUtils; | ||
|
||
@SuppressWarnings("ALL") |
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 a Recipes
class itself, which needs documentation.
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 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
ObjectUtils.isEmpty
with !StringUtils.hasLength
|
||
@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 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.
Just passing by, but the new implementation will only deal with I think it makes sense to be conservative indeed, and leave the deprecated call if it can’t be guaranteed to keep the original intention. However, the case of Since |
I think you are right, also I realized after your comment that probably my PR does not solve the issue, on the contrary it will cause compile errors for this specific user. But it is a weird case all around, an object was accepted for a String comparison, probably that is why they deprecated this method and the user was putting as input for an object a list! I replicated and a minimum example would be the following: So not sure if it actually makes sense to fix that case. |
Closed after the comment of @DidierLoiseau |
Yes, I would assume so too. If you check the
Do you think so? As this PR currently stands, it would only do a change when the parameter is actually a Maybe this PR should just remove the migration entirely instead, since it is unsafe? |
|
No but with this PR, it would only do the change when the compile-time type of the expression used as argument for
I don’t think OpenRewrite recipes should be expected to fix all deprecation issues. |
This is correct, and even if refaster doesn't cut it you could make the recipe "strongly typed" so to say |
What's changed?
StringUtils.isEmpty()
is deprecated currently openrewrite replaces it withObjectUtils.isEmpty()
which is stricterand led to this issue:
StringUtils#isEmpty
is not equivalent toObjectUtils#isEmpty
for collections #475According to the official suggestions: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/StringUtils.html#isEmpty(java.lang.Object)
I replaced it with
!StringUtils.hasLength
What's your motivation?
StringUtils#isEmpty
is not equivalent toObjectUtils#isEmpty
for collections #475Anything in particular you'd like reviewers to focus on?
Anyone you would like to review specifically?
@Laurens-W
Have you considered any alternatives or workarounds?
Any additional context
Checklist