From 0e75faeba1f60733888575f08967f48eb39a8820 Mon Sep 17 00:00:00 2001 From: Suby S Surendran Date: Fri, 27 Sep 2024 15:04:04 +0530 Subject: [PATCH] Incorporated the code review changes --- ...TRewritingImplicitTypeDeclarationTest.java | 87 +++++++++++++++---- .../dom/org/eclipse/jdt/core/dom/AST.java | 4 - .../core/dom/rewrite/ASTRewriteAnalyzer.java | 4 +- 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingImplicitTypeDeclarationTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingImplicitTypeDeclarationTest.java index cf7c39a6a24..7c8a6170115 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingImplicitTypeDeclarationTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingImplicitTypeDeclarationTest.java @@ -15,29 +15,14 @@ package org.eclipse.jdt.core.tests.rewrite.describing; import java.util.List; - +import junit.framework.Test; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.core.dom.AST; -import org.eclipse.jdt.core.dom.ASTNode; -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.ImplicitTypeDeclaration; -import org.eclipse.jdt.core.dom.Javadoc; -import org.eclipse.jdt.core.dom.MethodDeclaration; -import org.eclipse.jdt.core.dom.MethodInvocation; -import org.eclipse.jdt.core.dom.PrimitiveType; -import org.eclipse.jdt.core.dom.QualifiedName; -import org.eclipse.jdt.core.dom.StringLiteral; -import org.eclipse.jdt.core.dom.TagElement; -import org.eclipse.jdt.core.dom.TextElement; +import org.eclipse.jdt.core.dom.*; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ListRewrite; -import junit.framework.Test; - public class ASTRewritingImplicitTypeDeclarationTest extends ASTRewritingTest{ public ASTRewritingImplicitTypeDeclarationTest(String name, int apiLevel) { @@ -231,5 +216,73 @@ public void test003() throws Exception { assertEqualString(preview, buf.toString()); } + public void test004() throws Exception { + AST ast = AST.newAST(AST.JLS23, true); + IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null); + StringBuilder buf = new StringBuilder(); + buf= new StringBuilder(); + buf.append("/** \n"); + buf.append(" * Hello\n"); + buf.append(" */\n"); + buf.append("void main(){\n"); + buf.append(" System.out.println(\"main\");\n"); + buf.append("}\n"); + buf.append("void abc(){\n"); + buf.append(" System.out.println(\"abc\");\n"); + buf.append("}\n"); + + ICompilationUnit cu= pack1.createCompilationUnit("X.java", buf.toString(), false, null); + CompilationUnit astRoot= createAST(cu); + ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); + + assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0); + ImplicitTypeDeclaration implicitTypeDeclaration= findImplicitDeclaration(astRoot, ""); + List bodyDeclaration = implicitTypeDeclaration.bodyDeclarations(); + System.out.println("sasi"); + { + + rewrite.remove(bodyDeclaration.get(1), null);//remove one method + + MethodInvocation methodInvocation = ast.newMethodInvocation(); + methodInvocation.setName(ast.newSimpleName("println")); + + StringLiteral literal = ast.newStringLiteral(); + literal.setLiteralValue("xyz"); + + QualifiedName qualifiedName = ast.newQualifiedName(ast.newName("System"), ast.newSimpleName("out")); + + methodInvocation.setExpression(qualifiedName); + methodInvocation.arguments().add(literal); + + ExpressionStatement expressionStatement = ast.newExpressionStatement(methodInvocation); + + Block block = ast.newBlock(); + block.statements().add(expressionStatement); + + MethodDeclaration methodDeclaration = ast.newMethodDeclaration(); + methodDeclaration.setBody(block); + methodDeclaration.setName(ast.newSimpleName("xyz")); + methodDeclaration.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); + + ListRewrite listRewrite= rewrite.getListRewrite(implicitTypeDeclaration, ImplicitTypeDeclaration.BODY_DECLARATIONS_PROPERTY); + listRewrite.insertAt(methodDeclaration, 1, null); + + String preview = evaluateRewrite(cu, rewrite); + buf= new StringBuilder(); + + buf.append("/** \n"); + buf.append(" * Hello\n"); + buf.append(" */\n"); + buf.append("void main(){\n"); + buf.append(" System.out.println(\"main\");\n"); + buf.append("}\n"); + buf.append("void xyz() {\n"); + buf.append(" System.out.println(\"xyz\");\n"); + buf.append("}\n"); + + assertEqualString(preview, buf.toString()); + + } + } } \ No newline at end of file diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java index db2f9675a7b..fddc84e74f8 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java @@ -3194,10 +3194,6 @@ public TryStatement newTryStatement() { * The name of the class is an unspecified, but legal, name; * no modifiers; no doc comment; no superclass or superinterfaces; * and an empty class body. - *

- * To create an interface, use this method and then call - * ImplicitTypeDeclaration. - *

* * @return a new unparented type declaration node * @since 3.40 diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java index 52496e94803..94d00d72b08 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java @@ -1855,10 +1855,10 @@ public boolean visit(ImplicitTypeDeclaration node) { return doVisitUnchangedChildren(node); } //javaDoc - int pos= rewriteJavadoc(node, ImplicitTypeDeclaration.JAVADOC_PROPERTY); + rewriteJavadoc(node, ImplicitTypeDeclaration.JAVADOC_PROPERTY); int startIndent= getIndent(node.getStartPosition()) + 1; - int startPos= getPosAfterLeftBrace(pos); + int startPos= node.getStartPosition(); rewriteParagraphList(node, ImplicitTypeDeclaration.BODY_DECLARATIONS_PROPERTY, startPos, startIndent, -1, 2); return false; }