Skip to content

Commit

Permalink
ASTRewriteFlattener inserts unnecessary semicolon
Browse files Browse the repository at this point in the history
  • Loading branch information
Shee43 authored and mpalat committed Jul 14, 2023
1 parent fbb2a77 commit 9b70716
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
Expand Down Expand Up @@ -69,7 +70,6 @@ public void setUpSuite() throws Exception {
public ASTConverter_16Test(String name) {
super(name);
}

public static Test suite() {
return buildModelTestSuite(ASTConverter_16Test.class);
}
Expand Down Expand Up @@ -815,4 +815,36 @@ public boolean visit(VariableDeclarationFragment node) {
}
});
}
@SuppressWarnings("rawtypes")
public void test_RecordSemicolon() throws JavaModelException {
if (!isJRE16) {
System.err.println("Test "+getName()+" requires a JRE 16");
return;
}
String contents =
"record Point(int x, int y) {\n" +
" private static final int staticField = 16;\n"+
"}";
this.workingCopy = getWorkingCopy("/Converter_16/src/ASTtree.java", true/*resolve*/);
ASTNode node = buildAST(
contents,
this.workingCopy);
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit unit = (CompilationUnit) node;
assertProblemsSize(unit, 0);
node = getASTNode(unit, 0);
assertEquals("Not a record declaration", ASTNode.RECORD_DECLARATION, node.getNodeType());
RecordDeclaration recordDeclaration = (RecordDeclaration) node;
final List bodyDeclarations = recordDeclaration.bodyDeclarations();
assertEquals("Wrong size", 1, bodyDeclarations.size());
FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclarations.get(0);
final List fragments = fieldDeclaration.fragments();
assertEquals("Wrong size", 1, fragments.size());
VariableDeclarationFragment fragment = (VariableDeclarationFragment)fragments.get(0);
final Expression initializer = fragment.getInitializer();
assertEquals("Not a number literal", ASTNode.NUMBER_LITERAL, initializer.getNodeType());
checkSourceRange(initializer, "16", contents);


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ExpressionStatement;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.Javadoc;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.MethodDeclaration;
Expand All @@ -40,6 +41,7 @@
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.TypeParameter;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

Expand Down Expand Up @@ -1372,5 +1374,59 @@ public void testRecord_027() throws Exception {
}


public void testRecord_028() throws Exception {
if (checkAPILevel()) {
return;
}
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf= new StringBuilder();
buf.append("package test1;\n");
buf.append("public class Y {\n");
buf.append("\n");
buf.append("}\n");
ICompilationUnit cu= pack1.createCompilationUnit("Y.java", buf.toString(), false, null);

CompilationUnit astRoot= createAST(cu);
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());

AST ast= astRoot.getAST();

assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
TypeDeclaration type= findTypeDeclaration(astRoot, "Y");
ListRewrite declarations = rewrite.getListRewrite(type, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
{
RecordDeclaration record = ast.newRecordDeclaration();
record.setName(ast.newSimpleName("X"));

VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
variableDeclarationFragment.setName(ast.newSimpleName("staticField"));

FieldDeclaration fieldDeclaration = ast.newFieldDeclaration(variableDeclarationFragment);
fieldDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PRIVATE_KEYWORD));
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.STATIC_KEYWORD));
fieldDeclaration.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));

record.bodyDeclarations().add(fieldDeclaration);

declarations.insertFirst(record, null);
}

String preview= evaluateRewrite(cu, rewrite);

buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class Y {\n");
buf.append("\n");
buf.append(" record X() {\n");
buf.append(" private static final int staticField;\n");
buf.append(" }\n");
buf.append("\n");
buf.append("}\n");

assertEqualString(preview, buf.toString());

}

}

Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ public boolean visit(RecordDeclaration node) {
visitList(node, RecordDeclaration.SUPER_INTERFACE_TYPES_PROPERTY, String.valueOf(','), "implements ", Util.EMPTY_STRING); //$NON-NLS-1$

this.result.append('{');
visitList(node, RecordDeclaration.BODY_DECLARATIONS_PROPERTY, Util.EMPTY_STRING, String.valueOf(';'), Util.EMPTY_STRING);
visitList(node, RecordDeclaration.BODY_DECLARATIONS_PROPERTY, Util.EMPTY_STRING);
this.result.append('}');
return false;
}
Expand Down

0 comments on commit 9b70716

Please sign in to comment.