1+ /*******************************************************************************
2+ * Copyright (c) 2024 IBM Corporation and others.
3+ *
4+ * This program and the accompanying materials
5+ * are made available under the terms of the Eclipse Public License 2.0
6+ * which accompanies this distribution, and is available at
7+ * https://www.eclipse.org/legal/epl-2.0/
8+ *
9+ * SPDX-License-Identifier: EPL-2.0
10+ *
11+ * Contributors:
12+ * IBM Corporation - initial API and implementation
13+ *******************************************************************************/
14+
15+ package org .eclipse .jdt .core .tests .rewrite .describing ;
16+
17+ import java .util .List ;
18+
19+ import org .eclipse .jdt .core .ICompilationUnit ;
20+ import org .eclipse .jdt .core .IPackageFragment ;
21+ import 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 ;
36+ import org .eclipse .jdt .core .dom .rewrite .ASTRewrite ;
37+ import org .eclipse .jdt .core .dom .rewrite .ListRewrite ;
38+
39+ import junit .framework .Test ;
40+
41+ public class ASTRewritingImplicitTypeDeclarationTest extends ASTRewritingTest {
42+
43+ public ASTRewritingImplicitTypeDeclarationTest (String name , int apiLevel ) {
44+ super (name , apiLevel );
45+ }
46+
47+ public static Test suite () {
48+ return createSuite (ASTRewritingImplicitTypeDeclarationTest .class , 23 );
49+ }
50+
51+ @ Override
52+ protected void setUp () throws Exception {
53+ super .setUp ();
54+ if (this .apiLevel == AST .JLS23 ) {
55+ this .project1 .setOption (JavaCore .COMPILER_COMPLIANCE , JavaCore .VERSION_23 );
56+ this .project1 .setOption (JavaCore .COMPILER_SOURCE , JavaCore .VERSION_23 );
57+ this .project1 .setOption (JavaCore .COMPILER_CODEGEN_TARGET_PLATFORM , JavaCore .VERSION_23 );
58+ this .project1 .setOption (JavaCore .COMPILER_PB_ENABLE_PREVIEW_FEATURES , JavaCore .ENABLED );
59+ }
60+ }
61+
62+ public void test001 () throws Exception {
63+ AST ast = AST .newAST (AST .JLS23 , true );
64+ // Create CompilationUnit
65+ CompilationUnit compilationUnit = ast .newCompilationUnit ();
66+
67+ ImplicitTypeDeclaration implicitTypeDeclaration = ast .newImplicitTypeDeclaration ();
68+
69+ Javadoc javaDoc = ast .newJavadoc ();
70+ TextElement textElem = ast .newTextElement ();
71+ textElem .setText ("Hello" );
72+ TagElement tagElement = ast .newTagElement ();
73+ tagElement .fragments ().add (textElem );
74+ javaDoc .tags ().add (tagElement );
75+ implicitTypeDeclaration .setJavadoc (javaDoc );
76+
77+ QualifiedName qualifiedName = ast .newQualifiedName (ast .newName ("System" ), ast .newSimpleName ("out" ));
78+ MethodInvocation methodInvocation = ast .newMethodInvocation ();
79+ methodInvocation .setExpression (qualifiedName );
80+ methodInvocation .setName (ast .newSimpleName ("println" ));
81+
82+ StringLiteral literal = ast .newStringLiteral ();
83+ literal .setLiteralValue ("Eclipse" );
84+ methodInvocation .arguments ().add (literal );
85+ ExpressionStatement expressionStatement = ast .newExpressionStatement (methodInvocation );
86+
87+ Block block = ast .newBlock ();
88+ block .statements ().add (expressionStatement );
89+ MethodDeclaration methodDeclaration = ast .newMethodDeclaration ();
90+ methodDeclaration .setReturnType2 (ast .newPrimitiveType (PrimitiveType .VOID ));
91+ methodDeclaration .setName (ast .newSimpleName ("main" ));
92+ methodDeclaration .setBody (block );
93+ implicitTypeDeclaration .bodyDeclarations ().add (methodDeclaration );
94+ // Add Implicity Type class to compilation unit
95+ compilationUnit .types ().add (implicitTypeDeclaration );
96+
97+ StringBuilder buf = new StringBuilder ();
98+ buf .append ("/** \n " );
99+ buf .append (" * Hello\n " );
100+ buf .append (" */\n " );
101+ buf .append (" void main(){\n " );
102+ buf .append (" System.out.println(\" Eclipse\" );\n " );
103+ buf .append (" }\n " );
104+
105+ assertEqualString (compilationUnit .toString (), buf .toString ());
106+ }
107+
108+ //javaDoc
109+ public void test002 () throws Exception {
110+ AST ast = AST .newAST (AST .JLS23 , true );
111+ IPackageFragment pack1 = this .sourceFolder .createPackageFragment ("test1" , false , null );
112+ StringBuilder buf = new StringBuilder ();
113+ buf = new StringBuilder ();
114+ buf .append ("/** \n " );
115+ buf .append (" * Hello\n " );
116+ buf .append (" */\n " );
117+ buf .append ("void main(){\n " );
118+ buf .append (" System.out.println(\" Eclipse\" );\n " );
119+ buf .append ("}\n " );
120+
121+ ICompilationUnit cu = pack1 .createCompilationUnit ("X.java" , buf .toString (), false , null );
122+ CompilationUnit astRoot = createAST (cu );
123+ ASTRewrite rewrite = ASTRewrite .create (astRoot .getAST ());
124+
125+ assertTrue ("Parse errors" , (astRoot .getFlags () & ASTNode .MALFORMED ) == 0 );
126+
127+ ImplicitTypeDeclaration implicitTypeDeclaration = findImplicitDeclaration (astRoot , "" );
128+ List <MethodDeclaration > methodDeclarationsList = implicitTypeDeclaration .bodyDeclarations ();
129+ MethodDeclaration methodDeclaration = methodDeclarationsList .get (0 );
130+ {
131+
132+ Javadoc javaDoc = methodDeclaration .getJavadoc ();
133+
134+ Javadoc newJavaDoc = ast .newJavadoc ();
135+ TextElement textElem = ast .newTextElement ();
136+ textElem .setText ("Eclipse" );
137+ TagElement tagElement = ast .newTagElement ();
138+ tagElement .fragments ().add (textElem );
139+ newJavaDoc .tags ().add (tagElement );
140+
141+ rewrite .replace (javaDoc , newJavaDoc , null );
142+ }
143+
144+ String preview = evaluateRewrite (cu , rewrite );
145+ buf = new StringBuilder ();
146+
147+ buf .append ("/**\n " );
148+ buf .append (" * Eclipse\n " );
149+ buf .append (" */\n " );
150+ buf .append ("void main(){\n " );
151+ buf .append (" System.out.println(\" Eclipse\" );\n " );
152+ buf .append ("}\n " );
153+
154+ assertEqualString (preview , buf .toString ());
155+
156+ {
157+ Javadoc javaDoc = methodDeclaration .getJavadoc ();
158+ Javadoc newJavaDoc = null ;
159+
160+ rewrite .replace (javaDoc , newJavaDoc , null );
161+ }
162+
163+ preview = evaluateRewrite (cu , rewrite );
164+ buf = new StringBuilder ();
165+
166+ buf .append ("void main(){\n " );
167+ buf .append (" System.out.println(\" Eclipse\" );\n " );
168+ buf .append ("}\n " );
169+
170+ assertEqualString (preview , buf .toString ());
171+ }
172+
173+ //adding more MEthodDeclaration
174+ public void test003 () throws Exception {
175+ AST ast = AST .newAST (AST .JLS23 , true );
176+ IPackageFragment pack1 = this .sourceFolder .createPackageFragment ("test1" , false , null );
177+ StringBuilder buf = new StringBuilder ();
178+ buf = new StringBuilder ();
179+ buf .append ("/** \n " );
180+ buf .append (" * Hello\n " );
181+ buf .append (" */\n " );
182+ buf .append ("void main(){\n " );
183+ buf .append (" System.out.println(\" Eclipse\" );\n " );
184+ buf .append ("}\n " );
185+
186+ ICompilationUnit cu = pack1 .createCompilationUnit ("X.java" , buf .toString (), false , null );
187+ CompilationUnit astRoot = createAST (cu );
188+ ASTRewrite rewrite = ASTRewrite .create (astRoot .getAST ());
189+
190+ assertTrue ("Parse errors" , (astRoot .getFlags () & ASTNode .MALFORMED ) == 0 );
191+ ImplicitTypeDeclaration implicitTypeDeclaration = findImplicitDeclaration (astRoot , "" );
192+ {
193+ MethodInvocation methodInvocation = ast .newMethodInvocation ();
194+ methodInvocation .setName (ast .newSimpleName ("println" ));
195+
196+ StringLiteral literal = ast .newStringLiteral ();
197+ literal .setLiteralValue ("abc" );
198+
199+ QualifiedName qualifiedName = ast .newQualifiedName (ast .newName ("System" ), ast .newSimpleName ("out" ));
200+
201+ methodInvocation .setExpression (qualifiedName );
202+ methodInvocation .arguments ().add (literal );
203+
204+ ExpressionStatement expressionStatement = ast .newExpressionStatement (methodInvocation );
205+
206+ Block block = ast .newBlock ();
207+ block .statements ().add (expressionStatement );
208+
209+ MethodDeclaration methodDeclaration = ast .newMethodDeclaration ();
210+ methodDeclaration .setBody (block );
211+ methodDeclaration .setName (ast .newSimpleName ("abc" ));
212+ methodDeclaration .setReturnType2 (ast .newPrimitiveType (PrimitiveType .VOID ));
213+
214+ ListRewrite listRewrite = rewrite .getListRewrite (implicitTypeDeclaration , ImplicitTypeDeclaration .BODY_DECLARATIONS_PROPERTY );
215+ listRewrite .insertAt (methodDeclaration , 1 , null );
216+ }
217+
218+ String preview = evaluateRewrite (cu , rewrite );
219+ buf = new StringBuilder ();
220+
221+ buf .append ("/** \n " );
222+ buf .append (" * Hello\n " );
223+ buf .append (" */\n " );
224+ buf .append ("void main(){\n " );
225+ buf .append (" System.out.println(\" Eclipse\" );\n " );
226+ buf .append ("}\n " );
227+ buf .append ("\n " );
228+ buf .append ("void abc() {\n " );
229+ buf .append (" System.out.println(\" abc\" );\n " );
230+ buf .append ("}\n " );
231+
232+ assertEqualString (preview , buf .toString ());
233+ }
234+
235+ }
0 commit comments