Skip to content

Commit

Permalink
Incorporated the code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
subyssurendran666 committed Oct 15, 2024
1 parent bd8059c commit a801a1e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<ASTNode> 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());

}
}

}
4 changes: 0 additions & 4 deletions org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/AST.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* To create an interface, use this method and then call
* <code>ImplicitTypeDeclaration</code>.
* </p>
*
* @return a new unparented type declaration node
* @since 3.40
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a801a1e

Please sign in to comment.