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

Fix StringBuffer to text block clean-up when constructed with String #1268

Merged
merged 1 commit into from
Mar 18, 2024
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 @@ -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
Original file line number Diff line number Diff line change
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
Loading