Skip to content

Commit

Permalink
Fix StringBuffer to text block clean-up when constructed with String (#…
Browse files Browse the repository at this point in the history
…1268)

- fix StringConcatToTextBlockFixCore.StringBufferFinder() to fail
  if a StringBuffer/StringBuilder constructor is used and isn't
  passed a StringLiteral or a NumberLiteral or an InfixExpression
  of StringLiterals concatenated
- add new tests to CleanUpTest15
  • Loading branch information
jjohnstn committed Mar 18, 2024
1 parent 448c988 commit 26487e2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
Expand Up @@ -56,6 +56,7 @@
import org.eclipse.jdt.core.dom.LineComment;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.NullLiteral;
import org.eclipse.jdt.core.dom.NumberLiteral;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.StringLiteral;
Expand Down Expand Up @@ -552,6 +553,16 @@ public boolean visit(ClassInstanceCreation node) {
if (args.size() == 1 && args.get(0) instanceof StringLiteral) {
fLiterals.add((StringLiteral)args.get(0));
nonNLS= hasNLSMarker(statement, icu);
} else if (args.size() > 0) {
if (args.get(0) instanceof InfixExpression infix) {
if (!processInfixExpression(statement, infix)) {
statementList.remove(statement);
return false;
}
} else if (!(args.get(0) instanceof NumberLiteral)) {
statementList.remove(statement);
return false;
}
}
Block block= (Block) statement.getParent();
List<Statement> stmtList= block.statements();
Expand Down Expand Up @@ -579,31 +590,8 @@ public boolean visit(ClassInstanceCreation node) {
fLiterals.add((StringLiteral)arg);
statementList.add(s);
} else if (arg instanceof InfixExpression infix) {
if (infix.getOperator() == Operator.PLUS) {
Expression left= infix.getLeftOperand();
Expression right= infix.getRightOperand();
List<Expression> extendedOps= infix.extendedOperands();
if (left instanceof StringLiteral && right instanceof StringLiteral) {
List<StringLiteral> extendedLiterals= new ArrayList<>();
for (Expression extendedOp : extendedOps) {
if (extendedOp instanceof StringLiteral) {
extendedLiterals.add((StringLiteral)extendedOp);
} else {
statementList.clear();
fLiterals.clear();
return false;
}

}
fLiterals.add((StringLiteral)left);
fLiterals.add((StringLiteral)right);
fLiterals.addAll(extendedLiterals);
statementList.add(s);
} else {
statementList.clear();
fLiterals.clear();
return false;
}
if (!processInfixExpression(s, infix)) {
return false;
}
} else {
statementList.clear();
Expand Down Expand Up @@ -670,6 +658,38 @@ public boolean visit(ClassInstanceCreation node) {
return true;
}

private boolean processInfixExpression(Statement s, InfixExpression infix) {
if (infix.getOperator() == Operator.PLUS) {
Expression left= infix.getLeftOperand();
Expression right= infix.getRightOperand();
List<Expression> extendedOps= infix.extendedOperands();
if (left instanceof StringLiteral && right instanceof StringLiteral) {
List<StringLiteral> extendedLiterals= new ArrayList<>();
for (Expression extendedOp : extendedOps) {
if (extendedOp instanceof StringLiteral) {
extendedLiterals.add((StringLiteral)extendedOp);
} else {
statementList.clear();
fLiterals.clear();
return false;
}

}
fLiterals.add((StringLiteral)left);
fLiterals.add((StringLiteral)right);
fLiterals.addAll(extendedLiterals);
if (!statementList.contains(s)) {
statementList.add(s);
}
} else {
statementList.clear();
fLiterals.clear();
return false;
}
}
return true;
}

@Override
public boolean visit(final VariableDeclarationStatement visited) {
Type type= visited.getType();
Expand Down
Expand Up @@ -340,6 +340,19 @@ public void testConcatToTextBlock2() throws Exception {
+ " buf5= new StringBuilder();\n" //
+ " buf5.append(7);\n" //
+ " String str3= \"abc\";\n" //
+ " String x2= \"\" +\n" //
+ " \"abc\\n\" +\n" //
+ " \"def\\n\" +\n" //
+ " \"ghi\\n\" +\n" //
+ " \"jki\\n\";\n" //
+ " StringBuilder buf6 = new StringBuilder(x2);\n" //
+ " System.out.println(buf6.toString());\n" //
+ " StringBuilder buf7 = new StringBuilder(\"\" +\n" //
+ " \"abc\\n\" +\n" //
+ " \"def\\n\" +\n" //
+ " \"ghi\\n\" +\n" //
+ " \"jki\\n\");\n" //
+ " System.out.println(buf7.toString());\n" //
+ " }\n" //
+ "}";

Expand Down Expand Up @@ -424,6 +437,21 @@ public void testConcatToTextBlock2() throws Exception {
+ " buf5= new StringBuilder();\n" //
+ " buf5.append(7);\n" //
+ " String str3= \"abc\";\n" //
+ " String x2= \"\"\"\n" //
+ " abc\n" //
+ " def\n" //
+ " ghi\n" //
+ " jki\n" //
+ " \"\"\";\n" //
+ " StringBuilder buf6 = new StringBuilder(x2);\n" //
+ " System.out.println(buf6.toString());\n" //
+ " String str5 = \"\"\"\n" //
+ " abc\n" //
+ " def\n" //
+ " ghi\n" //
+ " jki\n" //
+ " \"\"\";\n" //
+ " System.out.println(str5);\n" //
+ " }\n" //
+ "}";

Expand Down

0 comments on commit 26487e2

Please sign in to comment.