Skip to content

Commit

Permalink
Disable some ApacheCommonsStringUtils recipes after seeing results (#277
Browse files Browse the repository at this point in the history
)

* Disable some ApacheCommonsStringUtils recipes after seeing results

At scale we don't want to add a `Pattern.compile(",")` for split, or IntStream.

* Remove imports to match
  • Loading branch information
timtebeek authored Aug 22, 2023
1 parent 29a24e1 commit 8a1e088
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.IntStream;

@SuppressWarnings("ALL")
public class ApacheCommonsStringUtils {
Expand Down Expand Up @@ -142,17 +140,18 @@ String after(String s) {
}
}

private static class EndsWithIgnoreCase {
@BeforeTemplate
boolean before(String s, String suffix) {
return StringUtils.endsWithIgnoreCase(s, suffix);
}

@AfterTemplate
boolean after(String s, String suffix) {
return (s == null && suffix == null || s != null && suffix != null && s.regionMatches(true, s.length() - suffix.length(), suffix, 0, suffix.length()));
}
}
// NOTE: unlikely to go over well due to added complexity
//private static class EndsWithIgnoreCase {
// @BeforeTemplate
// boolean before(String s, String suffix) {
// return StringUtils.endsWithIgnoreCase(s, suffix);
// }
//
// @AfterTemplate
// boolean after(String s, String suffix) {
// return (s == null && suffix == null || s != null && suffix != null && s.regionMatches(true, s.length() - suffix.length(), suffix, 0, suffix.length()));
// }
//}

private static class EqualsIgnoreCase {
@BeforeTemplate
Expand All @@ -178,20 +177,23 @@ boolean after(String s, String other) {
}
}

private static class IndexOfAny {
@BeforeTemplate
int before(String s, String searchChars) {
return StringUtils.indexOfAny(s, searchChars);
}

@AfterTemplate
int after(String s, String searchChars) {
return IntStream.range(0, searchChars.length())
.filter(i -> s.indexOf(searchChars.charAt(i)) >= 0)
.min()
.orElse(-1);
}
}
// NOTE: unlikely to go over well due to added complexity
//private static class IndexOfAny {
// @BeforeTemplate
// int before(String s, String searchChars) {
// return StringUtils.indexOfAny(s, searchChars);
// }
//
// @AfterTemplate
// int after(String s, String searchChars) {
// return searchChars == null || searchChars.isEmpty() ? -1 :
// IntStream.range(0, searchChars.length())
// .map(searchChars::charAt)
// .map(s::indexOf)
// .min()
// .orElse(-1);
// }
//}

// NOTE: not sure if accurate replacement
//private static class IsAlphanumericSpace {
Expand Down Expand Up @@ -394,17 +396,18 @@ String after(String s, String end) {
// }
//}

private static class ReplaceOnce {
@BeforeTemplate
String before(String s, String search, String replacement) {
return StringUtils.replaceOnce(s, search, replacement);
}

@AfterTemplate
String after(String s, String search, String replacement) {
return (s == null || s.isEmpty() || search == null || search.isEmpty() || replacement == null ? s : s.replaceFirst(Pattern.quote(search), replacement));
}
}
// NOTE: requires dedicated recipe to clean up `Pattern.quote(",")`
//private static class ReplaceOnce {
// @BeforeTemplate
// String before(String s, String search, String replacement) {
// return StringUtils.replaceOnce(s, search, replacement);
// }
//
// @AfterTemplate
// String after(String s, String search, String replacement) {
// return (s == null || s.isEmpty() || search == null || search.isEmpty() || replacement == null ? s : s.replaceFirst(Pattern.quote(search), replacement));
// }
//}

private static class Replace {
@BeforeTemplate
Expand Down Expand Up @@ -455,17 +458,18 @@ String[] after(String s) {
}
}

private static class SplitSeparator {
@BeforeTemplate
String[] before(String s, String arg) {
return StringUtils.split(s, arg);
}

@AfterTemplate
String[] after(String s, String arg) {
return (s == null ? null : s.split(Pattern.quote(arg)));
}
}
// NOTE: requires dedicated recipe to clean up `Pattern.quote(",")`
//private static class SplitSeparator {
// @BeforeTemplate
// String[] before(String s, String arg) {
// return StringUtils.split(s, arg);
// }
//
// @AfterTemplate
// String[] after(String s, String arg) {
// return (s == null ? null : s.split(Pattern.quote(arg)));
// }
//}

// NOTE: different semantics in handling max=0 to discard trailing empty strings
//private static class SplitSeparatorMax {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

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

@SuppressWarnings({"UnnecessaryCallToStringValueOf", "ConstantValue", "UnusedAssignment", "DataFlowIssue", "StringOperationCanBeSimplified"})
@SuppressWarnings({"Deprecation", "UnusedAssignment", "DataFlowIssue", "StringOperationCanBeSimplified"})
class ApacheCommonsStringUtilsTest implements RewriteTest {

@Override
Expand Down Expand Up @@ -69,13 +69,13 @@ void bar(String in, CharSequence cs) {
string = StringUtils.defaultString(in, "nil");
string = StringUtils.deleteWhitespace(in);
bool = StringUtils.endsWithIgnoreCase(in, "suffix");
//bool = StringUtils.endsWithIgnoreCase(in, "suffix");
bool = StringUtils.equalsIgnoreCase(in, "other");
bool = StringUtils.equals(in, "other");
bool = StringUtils.equals(cs, "other");
bool = StringUtils.equals(cs, cs);
integer = StringUtils.indexOfAny(in, "search");
//integer = StringUtils.indexOfAny(in, "search");
bool = StringUtils.isAlphanumericSpace(in);
bool = StringUtils.isAlphanumeric(in);
Expand All @@ -98,15 +98,15 @@ void bar(String in, CharSequence cs) {
string = StringUtils.repeat(in, 4);
string = StringUtils.repeat(in, ",", 4);
string = StringUtils.replace(in, "search", "replacement");
string = StringUtils.replaceOnce(in, "search", "replacement");
//string = StringUtils.replaceOnce(in, "search", "replacement");
string = StringUtils.reverse(in);
string = StringUtils.right(in, 5);
string = StringUtils.rightPad(in, 5);
string = StringUtils.rightPad(in, 5, ' ');
string = StringUtils.rightPad(in, 5, " ");
array = StringUtils.split(in);
array = StringUtils.split(in, "*");
//array = StringUtils.split(in, "*");
bool = StringUtils.startsWith(in, "prefix");
bool = StringUtils.startsWithAny(in, "prefix");
bool = StringUtils.startsWithIgnoreCase(in, "prefix");
Expand All @@ -133,8 +133,6 @@ void bar(String in, CharSequence cs) {
import org.apache.commons.lang3.StringUtils;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
class Foo {
void bar(String in, CharSequence cs) {
Expand Down Expand Up @@ -162,13 +160,13 @@ void bar(String in, CharSequence cs) {
string = Objects.toString(in, "nil");
string = in == null ? null : in.replaceAll("\\s+", "");
bool = in != null && in.regionMatches(true, in.length() - "suffix".length(), "suffix", 0, "suffix".length());
//bool = StringUtils.endsWithIgnoreCase(in, "suffix");
bool = in != null && in.equalsIgnoreCase("other");
bool = Objects.equals(in, "other");
bool = StringUtils.equals(cs, "other");
bool = StringUtils.equals(cs, cs);
integer = IntStream.range(0, "search".length()).filter(i -> in.indexOf("search".charAt(i)) >= 0).min().orElse(-1);
//integer = StringUtils.indexOfAny(in, "search");
bool = StringUtils.isAlphanumericSpace(in);
bool = in != null && !in.isEmpty() && in.chars().allMatch(Character::isLetterOrDigit);
Expand All @@ -191,15 +189,15 @@ void bar(String in, CharSequence cs) {
string = StringUtils.repeat(in, 4);
string = StringUtils.repeat(in, ",", 4);
string = in == null || in.isEmpty() ? in : in.replace("search", "replacement");
string = in == null || in.isEmpty() ? in : in.replaceFirst(Pattern.quote("search"), "replacement");
//string = StringUtils.replaceOnce(in, "search", "replacement");
string = in == null ? null : new StringBuilder(in).reverse().toString();
string = StringUtils.right(in, 5);
string = StringUtils.rightPad(in, 5);
string = StringUtils.rightPad(in, 5, ' ');
string = StringUtils.rightPad(in, 5, " ");
array = in == null ? null : in.split("\\s+");
array = in == null ? null : in.split(Pattern.quote("*"));
//array = StringUtils.split(in, "*");
bool = StringUtils.startsWith(in, "prefix");
bool = StringUtils.startsWithAny(in, "prefix");
bool = StringUtils.startsWithIgnoreCase(in, "prefix");
Expand Down

0 comments on commit 8a1e088

Please sign in to comment.