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 3, 2024
1 parent f99d6bb commit 0054e3c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,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 = (List<ASTNode>) implicitTypeDeclaration.bodyDeclarations();

Check warning on line 255 in org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingImplicitTypeDeclarationTest.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler and API Tools

Unnecessary Code

NORMAL: Unnecessary cast from List to List
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 @@ -1858,7 +1858,7 @@ public boolean visit(ImplicitTypeDeclaration node) {
int pos= rewriteJavadoc(node, ImplicitTypeDeclaration.JAVADOC_PROPERTY);

Check warning on line 1858 in org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java

View check run for this annotation

Jenkins - Eclipse JDT / Compiler and API Tools

Unnecessary Code

NORMAL: The value of the local variable pos is not used

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 0054e3c

Please sign in to comment.