Skip to content

Commit

Permalink
Fix extraneous text block conversion from StringBuffer/StringBuilder (e…
Browse files Browse the repository at this point in the history
…clipse-jdt#1947)

- add check for number of string literals to new logic that converts
  the initializer of the StringBuilder/StringBuffer
- modify test in CleanUpTest15
- modify test in AssistQuickFixTest15
- fixes eclipse-jdt#1944
  • Loading branch information
jjohnstn authored Jan 17, 2025
1 parent 7ae9287 commit 3cff735
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,9 @@ public boolean visit(SimpleName simpleName) {
if (!checkValidityVisitor.isValid()) {
return failure();
} else if (checkValidityVisitor.isPassedAsArgument()) {
if (fLiterals.size() < 3) {
return failure();
}
List<Statement> statements= new ArrayList<>(statementList);
List<StringLiteral> literals= new ArrayList<>(fLiterals);
ModifyStringBufferToUseTextBlock operation= new ModifyStringBufferToUseTextBlock(node, statements,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,13 @@ public void foo() {
// comment 1
int index = buf3.indexOf("null");
bufFunc(buf3);
StringBuilder buf13 = new StringBuilder();
bufFunc(buf13);
StringBuilder buf14 = new StringBuilder("abcd\\n");
bufFunc(buf14);
StringBuilder buf15 = new StringBuilder("abcd\\n");
buf15.append("efg");
bufFunc(buf15);
\s
}
public void bufFunc(StringBuilder x) {
Expand All @@ -842,6 +849,13 @@ public void foo() {
// comment 1
int index = buf3.indexOf("null");
bufFunc(buf3);
StringBuilder buf13 = new StringBuilder();
bufFunc(buf13);
StringBuilder buf14 = new StringBuilder("abcd\\n");
bufFunc(buf14);
StringBuilder buf15 = new StringBuilder("abcd\\n");
buf15.append("efg");
bufFunc(buf15);
\s
}
public void bufFunc(StringBuilder x) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2024 Red Hat Inc. and others.
* Copyright (c) 2021, 2025 Red Hat Inc. and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -415,6 +415,13 @@ public void foo() {
buf12.append("ijkl\\n");
buf12.append("mnopq\\n");
bufFunc(buf12);
StringBuffer buf13 = new StringBuffer();
bufFunc(buf13);
StringBuffer buf14 = new StringBuffer("abcd\\n");
bufFunc(buf14);
StringBuffer buf15 = new StringBuffer("abcd\\n");
buf15.append("efg");
bufFunc(buf15);
}
private void write(CharSequence c) {
System.out.println(c);
Expand Down Expand Up @@ -554,6 +561,13 @@ public class C {
mnopq
\""");
bufFunc(buf12);
StringBuffer buf13 = new StringBuffer();
bufFunc(buf13);
StringBuffer buf14 = new StringBuffer("abcd\\n");
bufFunc(buf14);
StringBuffer buf15 = new StringBuffer("abcd\\n");
buf15.append("efg");
bufFunc(buf15);
}
private void write(CharSequence c) {
System.out.println(c);
Expand Down

0 comments on commit 3cff735

Please sign in to comment.