Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not rewrite String.replaceAll with special chars in replacement st…
Browse files Browse the repository at this point in the history
…ring

If the replacement string of String.replaceAll contains $ or \, we
should not rewrite it as these indicate special replacements:
https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/util/regex/Matcher.html#replaceAll(java.lang.String)

Fixes openrewrite#301
sambsnyd committed Jun 29, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 1826c68 commit 1815815
Showing 2 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/openrewrite/staticanalysis/UseStringReplace.java
Original file line number Diff line number Diff line change
@@ -83,6 +83,21 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)

//Checks if method invocation matches with String#replaceAll
if (REPLACE_ALL.matches(invocation)) {
Expression secondArgument = invocation.getArguments().get(1);

//Checks if the second argument is a string literal with $ or \ in it as this has special meaning and
// should not be rewritten:
//https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/util/regex/Matcher.html#replaceAll(java.lang.String)
if (isStringLiteral(secondArgument)) {
J.Literal literal = (J.Literal) secondArgument;
String value = (String) literal.getValue();

if (Objects.nonNull(value) && (value.contains("$") || value.contains("\\"))) {
// do not rewrite
return invocation;
}
}

Expression firstArgument = invocation.getArguments().get(0);

//Checks if the first argument is a String literal
Original file line number Diff line number Diff line change
@@ -146,4 +146,40 @@ public void method() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/301")
@DisplayName("String#replaceAll is not replaced by String#replace, because second argument has a backslash in it")
void replaceAllUnchangedIfBackslashInReplacementString() {
rewriteRun(
//language=java
java(
"""
class Test {
public String method() {
return "abc".replaceAll("b", "\\\\\\\\\\\\\\\\");
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/301")
@DisplayName("String#replaceAll is not replaced by String#replace, because second argument has a dollar sign in it")
void replaceAllUnchangedIfDollarInReplacementString() {
rewriteRun(
//language=java
java(
"""
class Test {
public String method() {
return "abc".replaceAll("b", "$0");
}
}
"""
)
);
}
}

0 comments on commit 1815815

Please sign in to comment.