openrewrite / rewrite-apache

OpenRewrite recipes for Apache projects.
Apache License 2.0
2 stars 5 forks source link

Recipe for replacing StringUtils#isBlank(CharSequence) and StringUtils#isNotBlank(CharSequence) #7

Open DevDavido opened 3 months ago

DevDavido commented 3 months ago

What problem are you trying to solve?

Similar to the recipe which replaces the StringUtils#isEmpty(String) and StringUtils#isNotEmpty(String) [1], a recipe would be helpful which replaces StringUtils#isBlank(CharSequence) and StringUtils#isNotBlank(CharSequence) with its plain Java equivalent too.

What precondition(s) should be checked before applying this recipe?

At least Java 11 is required due to the availability of java.lang.String#isBlank().

Describe the situation before applying the recipe

class A {
    void foo(String bar) {
        boolean isBlankBar = StringUtils.isBlank(bar);
        boolean isNotBlankBar = StringUtils.isNotBlank(bar);
    }
}

Describe the situation after applying the recipe

class A {
    void foo(String bar) {
        boolean isBlankBar = (bar == null || bar.isBlank());
        boolean isNotBlankBar = (bar != null && !bar.isBlank());
    }
}
timtebeek commented 3 months ago

Good suggestion indeed @DevDavido ; The Java 11 requirement might be tricky to work around; we have the same issue in this PR

Might be worth following that one as well as any fix there would unlock this one as well.