8000 Enum rewrite problem with ASTRewrite by subyssurendran666 · Pull Request #3979 · eclipse-jdt/eclipse.jdt.core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Enum rewrite problem with ASTRewrite #3979

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 agr 8000 ee to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2024 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -353,6 +353,10 @@ public static TypeDeclaration findTypeDeclaration(CompilationUnit astRoot, Strin
return (TypeDeclaration) findAbstractTypeDeclaration(astRoot, simpleTypeName);
}

public static EnumDeclaration findEnumDeclaration(CompilationUnit astRoot, String simpleTypeName) {
return (EnumDeclaration) findAbstractTypeDeclaration(astRoot, simpleTypeName);
}

public static AbstractType 8000 Declaration findAbstractTypeDeclaration(CompilationUnit astRoot, String simpleTypeName) {
List types= astRoot.types();
for (int i= 0; i < types.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
* Copyright (c) 2000, 2025 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -2022,4 +2022,61 @@ public void testSealedModifier_009() throws Exception {

}

public void testEnumRewriteProblem() throws Exception {
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
String baseCode = """
package x;
public enum X {
ENUM1 {
@Override
public String toString() {
int num = 1;
return "ENUM" + num;
}
}
}
""";
ICompilationUnit cu= pack1.createCompilationUnit("X.java", baseCode, false, null);
CompilationUnit astRoot= createAST(cu);
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
AST ast = AST.newAST(AST.getJLSLatest(), true);
assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
EnumDeclaration type = findEnumDeclaration(astRoot, "X");
List<EnumConstantDeclaration> enumDecl = type.enumConstants();
{
rewrite.replace(enumDecl.get(0).getName(), ast.newSimpleName("ENUM1_NEW"), null);

AnonymousClassDeclaration acd = enumDecl.get(0).getAnonymousClassDeclaration();
List<MethodDeclaration> mds = acd.bodyDeclarations();
rewrite.replace(mds.get(0).getName(), ast.newSimpleName("toStringNew"), null);

List<Modifier> modifiers = mds.get(0).modifiers();
rewrite.remove(modifiers.get(0), null);

Block block = mds.get(0).getBody();
List<?> statements = block.statements();
VariableDeclarationStatement vds = (VariableDeclarationStatement) statements.get(0);
List<VariableDeclarationFragment> vdfs = vds.fragments();
rewrite.replace(vdfs.get(0).getName(), ast.newSimpleName("num_new"), null);

ReturnStatement rtrnStemnt = (ReturnStatement) statements.get(1);
InfixExpression ie = (InfixExpression) rtrnStemnt.getExpression();

rewrite.replace(ie.getRightOperand(), ast.newSimpleName("num_new"), null);
}
String ASTConvertedCode = """
package x;
public enum X {
ENUM1_NEW {
public String toStringNew() {
int num_new = 1;
return "ENUM" + num_new;
}
}
}
""";

String preview= evaluateRewrite(cu, rewrite);
assertEqualString(preview, ASTConvertedCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4638,20 +4638,18 @@ public boolean visit(EnumConstantDeclaration node) {
pos= doVisit(node, EnumConstantDeclaration.ARGUMENTS_PROPERTY, pos);
}

if (isChanged(node, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY)) {
int kind= getChangeKind(node, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY);
if (kind == RewriteEvent.REMOVED) {
try {
// 'pos' can be before brace
pos= getScanner().getPreviousTokenEndOffset(TerminalToken.TokenNameLBRACE, pos);
} catch (CoreException e) {
handleException(e);
}
} else {
pos= node.getStartPosition() + node.getLength(); // insert pos
int kind= getChangeKind(node, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY);
if (kind == RewriteEvent.REMOVED) {
try {
// 'pos' can be before brace
pos= getScanner().getPreviousTokenEndOffset(TerminalToken.TokenNameLBRACE, pos);
} catch (CoreException e) {
handleException(e);
}
rewriteNode(node, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, pos, ASTRewriteFormatter.SPACE);
} else {
pos= node.getStartPosition() + node.getLength(); // insert pos
}
rewriteNode(node, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, pos, ASTRewriteFormatter.SPACE);
return false;
}

Expand Down
Loading
0