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

Disable some ApacheCommonsStringUtils recipes after seeing results #277

Merged
merged 2 commits into from
Aug 22, 2023
Merged
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
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