1515package org .eclipse .jdt .core .tests .rewrite .describing ;
1616
1717import java .util .List ;
18-
18+ import junit . framework . Test ;
1919import org .eclipse .jdt .core .ICompilationUnit ;
2020import org .eclipse .jdt .core .IPackageFragment ;
2121import org .eclipse .jdt .core .JavaCore ;
22- import org .eclipse .jdt .core .dom .AST ;
23- import org .eclipse .jdt .core .dom .ASTNode ;
24- import org .eclipse .jdt .core .dom .Block ;
25- import org .eclipse .jdt .core .dom .CompilationUnit ;
26- import org .eclipse .jdt .core .dom .ExpressionStatement ;
27- import org .eclipse .jdt .core .dom .ImplicitTypeDeclaration ;
28- import org .eclipse .jdt .core .dom .Javadoc ;
29- import org .eclipse .jdt .core .dom .MethodDeclaration ;
30- import org .eclipse .jdt .core .dom .MethodInvocation ;
31- import org .eclipse .jdt .core .dom .PrimitiveType ;
32- import org .eclipse .jdt .core .dom .QualifiedName ;
33- import org .eclipse .jdt .core .dom .StringLiteral ;
34- import org .eclipse .jdt .core .dom .TagElement ;
35- import org .eclipse .jdt .core .dom .TextElement ;
22+ import org .eclipse .jdt .core .dom .*;
3623import org .eclipse .jdt .core .dom .rewrite .ASTRewrite ;
3724import org .eclipse .jdt .core .dom .rewrite .ListRewrite ;
3825
39- import junit .framework .Test ;
40-
4126public class ASTRewritingImplicitTypeDeclarationTest extends ASTRewritingTest {
4227
4328 public ASTRewritingImplicitTypeDeclarationTest (String name , int apiLevel ) {
@@ -231,5 +216,73 @@ public void test003() throws Exception {
231216
232217 assertEqualString (preview , buf .toString ());
233218 }
219+ public void test004 () throws Exception {
220+ AST ast = AST .newAST (AST .JLS23 , true );
221+ IPackageFragment pack1 = this .sourceFolder .createPackageFragment ("test1" , false , null );
222+ StringBuilder buf = new StringBuilder ();
223+ buf = new StringBuilder ();
224+ buf .append ("/** \n " );
225+ buf .append (" * Hello\n " );
226+ buf .append (" */\n " );
227+ buf .append ("void main(){\n " );
228+ buf .append (" System.out.println(\" main\" );\n " );
229+ buf .append ("}\n " );
230+ buf .append ("void abc(){\n " );
231+ buf .append (" System.out.println(\" abc\" );\n " );
232+ buf .append ("}\n " );
233+
234+ ICompilationUnit cu = pack1 .createCompilationUnit ("X.java" , buf .toString (), false , null );
235+ CompilationUnit astRoot = createAST (cu );
236+ ASTRewrite rewrite = ASTRewrite .create (astRoot .getAST ());
237+
238+ assertTrue ("Parse errors" , (astRoot .getFlags () & ASTNode .MALFORMED ) == 0 );
239+ ImplicitTypeDeclaration implicitTypeDeclaration = findImplicitDeclaration (astRoot , "" );
240+ List <ASTNode > bodyDeclaration = implicitTypeDeclaration .bodyDeclarations ();
241+ System .out .println ("sasi" );
242+ {
243+
244+ rewrite .remove (bodyDeclaration .get (1 ), null );//remove one method
245+
246+ MethodInvocation methodInvocation = ast .newMethodInvocation ();
247+ methodInvocation .setName (ast .newSimpleName ("println" ));
248+
249+ StringLiteral literal = ast .newStringLiteral ();
250+ literal .setLiteralValue ("xyz" );
251+
252+ QualifiedName qualifiedName = ast .newQualifiedName (ast .newName ("System" ), ast .newSimpleName ("out" ));
253+
254+ methodInvocation .setExpression (qualifiedName );
255+ methodInvocation .arguments ().add (literal );
256+
257+ ExpressionStatement expressionStatement = ast .newExpressionStatement (methodInvocation );
258+
259+ Block block = ast .newBlock ();
260+ block .statements ().add (expressionStatement );
261+
262+ MethodDeclaration methodDeclaration = ast .newMethodDeclaration ();
263+ methodDeclaration .setBody (block );
264+ methodDeclaration .setName (ast .newSimpleName ("xyz" ));
265+ methodDeclaration .setReturnType2 (ast .newPrimitiveType (PrimitiveType .VOID ));
266+
267+ ListRewrite listRewrite = rewrite .getListRewrite (implicitTypeDeclaration , ImplicitTypeDeclaration .BODY_DECLARATIONS_PROPERTY );
268+ listRewrite .insertAt (methodDeclaration , 1 , null );
269+
270+ String preview = evaluateRewrite (cu , rewrite );
271+ buf = new StringBuilder ();
272+
273+ buf .append ("/** \n " );
274+ buf .append (" * Hello\n " );
275+ buf .append (" */\n " );
276+ buf .append ("void main(){\n " );
277+ buf .append (" System.out.println(\" main\" );\n " );
278+ buf .append ("}\n " );
279+ buf .append ("void xyz() {\n " );
280+ buf .append (" System.out.println(\" xyz\" );\n " );
281+ buf .append ("}\n " );
282+
283+ assertEqualString (preview , buf .toString ());
284+
285+ }
286+ }
234287
235288}
0 commit comments