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

ASTRewrite.rewriteAST fails with AssertionFailedException for records when it contains inner class #3002

Merged
merged 6 commits into from
Oct 16, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -824,4 +824,68 @@ public void test_RecordSemicolon() throws JavaModelException {


}

public void testRecord013() throws JavaModelException {
if (!isJRE16) {
System.err.println("Test " + getName() + " requires a JRE 16");
return;
}
String code = """
record Test(String name) {
public static Builder builder() {return null;}
public static final class Builder {}
}
""";
this.workingCopy = getWorkingCopy("/Converter_16/src/X.java", true/*resolve*/);
ASTNode node = buildAST(
code,
this.workingCopy);
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit compilationUnit = (CompilationUnit) node;
assertProblemsSize(compilationUnit, 0);
node = ((AbstractTypeDeclaration)compilationUnit.types().get(0));
assertEquals("Not a Record Declaration", ASTNode.RECORD_DECLARATION, node.getNodeType());
RecordDeclaration record = (RecordDeclaration)node;

List<SingleVariableDeclaration> RecordComponents = record.recordComponents();
assertEquals("Not a Single Variable Declaration Declaration", ASTNode.SINGLE_VARIABLE_DECLARATION, RecordComponents.get(0).getNodeType());

List<ASTNode> bodyDeclaration = record.bodyDeclarations();
MethodDeclaration md = (MethodDeclaration) bodyDeclaration.get(0);
TypeDeclaration td = (TypeDeclaration) bodyDeclaration.get(1);
assertEquals("Not a MethodDeclaration", ASTNode.METHOD_DECLARATION, md.getNodeType());
assertEquals("Not a TypeDeclaration", ASTNode.TYPE_DECLARATION, td.getNodeType());
}

public void testClass002() throws CoreException {
if (!isJRE16) {
System.err.println("Test "+getName()+" requires a JRE 16");
return;
}
String code = """
class Test {
public static Builder builder() {return null;}
public static final class Builder {}
}
""";
this.workingCopy = getWorkingCopy("/Converter_16/src/X.java", true/*resolve*/);
ASTNode node = buildAST(
code,
this.workingCopy);
assertEquals("Not a compilation unit", ASTNode.COMPILATION_UNIT, node.getNodeType());
CompilationUnit compilationUnit = (CompilationUnit) node;
assertProblemsSize(compilationUnit, 0);
List<AbstractTypeDeclaration> types = compilationUnit.types();
assertEquals("No. of Types is not 1", types.size(), 1);
AbstractTypeDeclaration type = types.get(0);
assertTrue("type not a type", type instanceof TypeDeclaration);
TypeDeclaration typeDecl = (TypeDeclaration)type;
assertTrue("type not a class", !typeDecl.isInterface());

List<ASTNode> bodyDeclaration = typeDecl.bodyDeclarations();
MethodDeclaration md = (MethodDeclaration) bodyDeclaration.get(0);
TypeDeclaration td = (TypeDeclaration) bodyDeclaration.get(1);
assertEquals("Not a MethodDeclaration", ASTNode.METHOD_DECLARATION, md.getNodeType());
assertEquals("Not a TypeDeclaration", ASTNode.TYPE_DECLARATION, td.getNodeType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1402,5 +1402,132 @@ public void testRecord_028() throws Exception {

}

public void testRecord_029_a() throws Exception {
if (checkAPILevel()) {
return;
}
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);

String code = """
package test1;
record Test(String name) {
public static Builder builder() {}
public static final class Builder {}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("Test.java", code, false, null);

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

assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
List <RecordDeclaration> methods= astRoot.types();
MethodDeclaration methodDecl= findMethodDeclaration(methods.get(0), "builder");
{
rewrite.remove(methodDecl, null);
}

String preview= evaluateRewrite(cu, rewrite);

String reWriteCode = """
package test1;
record Test(String name) {
public static final class Builder {}
}
""";

assertEqualString(preview, reWriteCode);
}

public void testRecord_029_b() throws Exception {
if (checkAPILevel()) {
return;
}
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);

String code = """
package test1;
public class Test {
public static Builder builder() {}
public static final class Builder {}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("Test.java", code, false, null);

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

assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
List <TypeDeclaration> methods= astRoot.types();
MethodDeclaration methodDecl= findMethodDeclaration(methods.get(0), "builder");
{
rewrite.remove(methodDecl, null);
}

String preview= evaluateRewrite(cu, rewrite);

String reWriteCode = """
package test1;
public class Test {
public static final class Builder {}
}
""";

assertEqualString(preview, reWriteCode);
}

public void testRecord_029_c() throws Exception {
if (checkAPILevel()) {
return;
}
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);

String code = """
package test1;
record Test(String name) {
public static Builder builder() {}
public static final class Builder {}
public static Builder builderNew() {}
public static final class builderNew {
builderNew(){
System.out.println("Test");
}
}
}
""";

ICompilationUnit cu= pack1.createCompilationUnit("Test.java", code, false, null);

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

assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
List <RecordDeclaration> methods= astRoot.types();
MethodDeclaration methodDecl= findMethodDeclaration(methods.get(0), "builder");
MethodDeclaration methodDeclNew= findMethodDeclaration(methods.get(0), "builderNew");
{
rewrite.remove(methodDecl, null);
rewrite.remove(methodDeclNew, null);
}

String preview= evaluateRewrite(cu, rewrite);

String reWriteCode = """
package test1;
record Test(String name) {
public static final class Builder {}
public static final class builderNew {
builderNew(){
System.out.println("Test");
}
}
}
""";

assertEqualString(preview, reWriteCode);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ protected void buildBodyDeclarations(

} else {
methodsIndex++;
continue;
}

}
Expand Down
Loading