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 string concat to text block handling of redundant constructors #1264

Merged
merged 1 commit into from
Mar 16, 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 @@ -442,12 +442,14 @@ public static final class StringBufferFinder extends ASTVisitor {
private final List<CompilationUnitRewriteOperation> fOperations;
private List<StringLiteral> fLiterals= new ArrayList<>();
private List<Statement> statementList= new ArrayList<>();
private Set<Statement> ignoredConstructorStatements= new HashSet<>();
private SimpleName originalVarName;
private Map<ExpressionStatement, ChangeStringBufferToTextBlock> conversions= new HashMap<>();
private static final String APPEND= "append"; //$NON-NLS-1$
private static final String TO_STRING= "toString"; //$NON-NLS-1$
private BodyDeclaration fLastBodyDecl;
private final Set<String> fExcludedNames;
private final Set<SimpleName> fRemovedDeclarations= new HashSet<>();

public StringBufferFinder(List<CompilationUnitRewriteOperation> operations, Set<String> excludedNames) {
super(true);
Expand Down Expand Up @@ -540,6 +542,10 @@ public boolean visit(ClassInstanceCreation node) {
!(statement instanceof ExpressionStatement)) {
return false;
}
if (ignoredConstructorStatements.contains(statement)) {
statementList.remove(statement);
return false;
}
boolean nonNLS= false;
CompilationUnit cu= (CompilationUnit) node.getRoot();
ICompilationUnit icu= (ICompilationUnit) cu.getJavaElement();
Expand All @@ -551,6 +557,7 @@ public boolean visit(ClassInstanceCreation node) {
List<Statement> stmtList= block.statements();
int startIndex= stmtList.indexOf(statement);
int i= startIndex;
Statement previousStatement= null;
while (++i < stmtList.size()) {
Statement s= stmtList.get(i);
if (s instanceof ExpressionStatement) {
Expand Down Expand Up @@ -598,21 +605,35 @@ public boolean visit(ClassInstanceCreation node) {
return false;
}
}
}
else {
} else {
statementList.clear();
fLiterals.clear();
return false;
}
} else {
break;
}
} else if (exp instanceof Assignment assignment && assignment.getLeftHandSide() instanceof SimpleName name
&& name.getFullyQualifiedName().equals(originalVarName.getFullyQualifiedName())
&& (i - startIndex == 1 || ignoredConstructorStatements.contains(previousStatement))) {
Expression rightSide= ASTNodes.getUnparenthesedExpression(assignment.getRightHandSide());
if (rightSide instanceof ClassInstanceCreation cic && isStringBufferType(cic.getType())) {
List<Expression> cicArgs= cic.arguments();
ignoredConstructorStatements.add(s);
statementList.add(s);
if (!cicArgs.isEmpty() || !fLiterals.isEmpty()) {
statementList.clear();
fLiterals.clear();
return false;
}
}
} else {
break;
}
} else {
break;
}
previousStatement= s;
}
Statement endStatement= stmtList.get(i - 1);
int lastStatementEnd= endStatement.getStartPosition() + endStatement.getLength();
Expand All @@ -636,7 +657,11 @@ public boolean visit(ClassInstanceCreation node) {
List<StringLiteral> literals= new ArrayList<>(fLiterals);
List<MethodInvocation> toStringList= new ArrayList<>(checkValidityVisitor.getToStringList());
BodyDeclaration bodyDecl= ASTNodes.getFirstAncestorOrNull(node, BodyDeclaration.class);
ChangeStringBufferToTextBlock operation= new ChangeStringBufferToTextBlock(toStringList, statements, literals, assignmentToConvert, fExcludedNames, fLastBodyDecl, nonNLS);
if (statements.get(0) instanceof VariableDeclarationStatement) {
fRemovedDeclarations.add(originalVarName);
}
ChangeStringBufferToTextBlock operation= new ChangeStringBufferToTextBlock(toStringList, statements, literals,
fRemovedDeclarations.contains(originalVarName) ? assignmentToConvert : null, fExcludedNames, fLastBodyDecl, nonNLS);
fLastBodyDecl= bodyDecl;
fOperations.add(operation);
conversions.put(assignmentToConvert, operation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ public void testConcatToTextBlock2() throws Exception {
+ " buf3.append(\"\\n\");\n" //
+ " // comment 6\n" //
+ " k = buf3.toString();\n" //
+ " buf3= new StringBuilder();\n" //
+ " buf3.append(4);\n" //
+ "\n" //
+ " String x = \"abc\\n\" +\n"
+ " \"def\\n\" +\n" //
Expand All @@ -325,6 +327,8 @@ public void testConcatToTextBlock2() throws Exception {
+ " buf4.append(\" */\");\n" //
+ " String expected= buf4.toString();\n" //
+ " StringBuilder buf5= new StringBuilder();\n" //
+ " buf5.append(3);\n" //
+ " buf5= new StringBuilder();\n" //
+ " buf5.append(\n" //
+ " \"package pack1;\\n\" +\n" //
+ " \"\\n\" +\n" //
Expand All @@ -333,6 +337,8 @@ public void testConcatToTextBlock2() throws Exception {
+ " \"public class C {\\n\" +\n" //
+ " \"}\");\n" //
+ " System.out.println(buf5.toString());\n" //
+ " buf5= new StringBuilder();\n" //
+ " buf5.append(7);\n" //
+ " String str3= \"abc\";\n" //
+ " }\n" //
+ "}";
Expand Down Expand Up @@ -382,6 +388,8 @@ public void testConcatToTextBlock2() throws Exception {
+ " }\n" //
+ " \n" //
+ " \"\"\";\n" //
+ " StringBuilder buf3 = new StringBuilder();\n" //
+ " buf3.append(4);\n" //
+ "\n" //
+ " String x = \"\"\"\n" //
+ " abc\n" //
Expand All @@ -403,6 +411,8 @@ public void testConcatToTextBlock2() throws Exception {
+ " * foo\n" //
+ " */\\\n" //
+ " \"\"\";\n" //
+ " StringBuilder buf5= new StringBuilder();\n" //
+ " buf5.append(3);\n" //
+ " String str4 = \"\"\"\n" //
+ " package pack1;\n" //
+ " \n" //
Expand All @@ -411,6 +421,8 @@ public void testConcatToTextBlock2() throws Exception {
+ " public class C {\n" //
+ " }\"\"\";\n" //
+ " System.out.println(str4);\n" //
+ " buf5= new StringBuilder();\n" //
+ " buf5.append(7);\n" //
+ " String str3= \"abc\";\n" //
+ " }\n" //
+ "}";
Expand Down
Loading