diff --git a/src/main/java/org/openrewrite/staticanalysis/UseStringReplace.java b/src/main/java/org/openrewrite/staticanalysis/UseStringReplace.java index 6caad84d..3801bdea 100644 --- a/src/main/java/org/openrewrite/staticanalysis/UseStringReplace.java +++ b/src/main/java/org/openrewrite/staticanalysis/UseStringReplace.java @@ -52,7 +52,7 @@ public String getDisplayName() { @Override public String getDescription() { return "When `String::replaceAll` is used, the first argument should be a real regular expression. " + - "If it’s not the case, `String::replace` does exactly the same thing as `String::replaceAll` without the performance drawback of the regex."; + "If it’s not the case, `String::replace` does exactly the same thing as `String::replaceAll` without the performance drawback of the regex."; } @Override @@ -81,19 +81,27 @@ private static class UseStringReplaceVisitor extends JavaVisitor ((J.Literal) arg) diff --git a/src/test/java/org/openrewrite/staticanalysis/UseStringReplaceTest.java b/src/test/java/org/openrewrite/staticanalysis/UseStringReplaceTest.java index 84caa957..1b5291ae 100644 --- a/src/test/java/org/openrewrite/staticanalysis/UseStringReplaceTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/UseStringReplaceTest.java @@ -146,4 +146,45 @@ 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 method1() { + return "abc".replaceAll("b", "$0"); + } + + public String method2() { + String s = "$0"; + return "abc".replaceAll("b", s); + } + } + """ + ) + ); + } }