KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > gen > ClassMemberTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.api.java.source.gen;
20
21 import com.sun.source.tree.*;
22 import com.sun.source.tree.TypeParameterTree;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.EnumSet JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.lang.model.element.Modifier;
31 import javax.lang.model.type.TypeKind;
32 import org.netbeans.api.java.source.CancellableTask;
33 import org.netbeans.api.java.source.JavaSource;
34 import org.netbeans.api.java.source.JavaSource.Phase;
35 import org.netbeans.api.java.source.TestUtilities;
36 import org.netbeans.api.java.source.TreeMaker;
37 import org.netbeans.api.java.source.WorkingCopy;
38 import org.netbeans.junit.NbTestSuite;
39
40 /**
41  *
42  * @author Pavel Flaska
43  */

44 public class ClassMemberTest extends GeneratorTestMDRCompat {
45     
46     /** Creates a new instance of ClassMemberTest */
47     public ClassMemberTest(String JavaDoc testName) {
48         super(testName);
49     }
50     
51     public static NbTestSuite suite() {
52         NbTestSuite suite = new NbTestSuite();
53 // suite.addTestSuite(ClassMemberTest.class);
54
suite.addTest(new ClassMemberTest("testAddAtIndex0"));
55         suite.addTest(new ClassMemberTest("testAddAtIndex2"));
56         suite.addTest(new ClassMemberTest("testAddToEmpty"));
57         suite.addTest(new ClassMemberTest("testAddConstructor"));
58         suite.addTest(new ClassMemberTest("testInsertFieldToIndex0"));
59         suite.addTest(new ClassMemberTest("testModifyFieldName"));
60         suite.addTest(new ClassMemberTest("testModifyModifiers"));
61         suite.addTest(new ClassMemberTest("testAddToEmptyInterface"));
62         suite.addTest(new ClassMemberTest("testAddNewClassWithNewMembers"));
63         suite.addTest(new ClassMemberTest("testAddInnerInterface"));
64         suite.addTest(new ClassMemberTest("testAddInnerAnnotationType"));
65         suite.addTest(new ClassMemberTest("testAddInnerEnum"));
66         suite.addTest(new ClassMemberTest("testAddAfterEmptyInit1"));
67 // suite.addTest(new ClassMemberTest("testAddAfterEmptyInit2"));
68
return suite;
69     }
70
71     public void testAddAtIndex0() throws Exception JavaDoc {
72         testFile = new File JavaDoc(getWorkDir(), "Test.java");
73         TestUtilities.copyStringToFile(testFile,
74             "package hierbas.del.litoral;\n\n" +
75             "public class Test {\n" +
76             " \n" +
77             " public void taragui() {\n" +
78             " }\n" +
79             " \n" +
80             "}\n"
81             );
82         String JavaDoc golden =
83             "package hierbas.del.litoral;\n\n" +
84             "public class Test {\n" +
85             " \n" +
86             " \n" +
87             " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" +
88             " }\n" +
89             "\n" +
90             " public void taragui() {\n" +
91             " }\n" +
92             " \n" +
93             "}\n";
94
95         JavaSource src = getJavaSource(testFile);
96         CancellableTask task = new CancellableTask<WorkingCopy>() {
97
98             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
99                 workingCopy.toPhase(Phase.RESOLVED);
100                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
101                 TreeMaker make = workingCopy.getTreeMaker();
102
103                 for (Tree typeDecl : cut.getTypeDecls()) {
104                     // ensure that it is correct type declaration, i.e. class
105
if (Tree.Kind.CLASS == typeDecl.getKind()) {
106                         ClassTree classTree = (ClassTree) typeDecl;
107                         ClassTree copy = make.insertClassMember(classTree, 0, m(make));
108                         workingCopy.rewrite(classTree, copy);
109                     }
110                 }
111             }
112             
113             public void cancel() {
114             }
115         };
116         src.runModificationTask(task).commit();
117         String JavaDoc res = TestUtilities.copyFileToString(testFile);
118         System.err.println(res);
119         assertEquals(golden, res);
120     }
121     
122     public void testAddAtIndex2() throws Exception JavaDoc {
123         //member position 2 is actually after the taragui method, as position 0 is the syntetic constructor:
124
testFile = new File JavaDoc(getWorkDir(), "Test.java");
125         TestUtilities.copyStringToFile(testFile,
126             "package hierbas.del.litoral;\n\n" +
127             "public class Test {\n" +
128             " public void taragui() {\n" +
129             " }\n" +
130             " \n" +
131             "}\n"
132             );
133         String JavaDoc golden =
134             "package hierbas.del.litoral;\n\n" +
135             "public class Test {\n" +
136             " public void taragui() {\n" +
137             " }\n" +
138             " \n" +
139             " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" +
140             " }\n" +
141             "\n" +
142             "}\n";
143
144         JavaSource src = getJavaSource(testFile);
145         CancellableTask task = new CancellableTask<WorkingCopy>() {
146
147             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
148                 workingCopy.toPhase(Phase.RESOLVED);
149                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
150                 TreeMaker make = workingCopy.getTreeMaker();
151                 for (Tree typeDecl : cut.getTypeDecls()) {
152                     // ensure that it is correct type declaration, i.e. class
153
if (Tree.Kind.CLASS == typeDecl.getKind()) {
154                         ClassTree classTree = (ClassTree) typeDecl;
155                         ClassTree copy = make.insertClassMember(classTree, 2, m(make));
156                         workingCopy.rewrite(classTree, copy);
157                     }
158                 }
159             }
160             
161             public void cancel() {
162             }
163         };
164         src.runModificationTask(task).commit();
165         String JavaDoc res = TestUtilities.copyFileToString(testFile);
166         System.err.println(res);
167         assertEquals(golden, res);
168     }
169     
170     public void testAddToEmpty() throws Exception JavaDoc {
171         //member position 2 is actually after the taragui method, as position 0 is the syntetic constructor:
172
testFile = new File JavaDoc(getWorkDir(), "Test.java");
173         TestUtilities.copyStringToFile(testFile,
174             "package hierbas.del.litoral;\n\n" +
175             "public class Test {\n" +
176             "}\n"
177             );
178         String JavaDoc golden =
179             "package hierbas.del.litoral;\n\n" +
180             "public class Test {\n\n" +
181             " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" +
182             " }\n" +
183             "}\n";
184
185         JavaSource src = getJavaSource(testFile);
186         CancellableTask task = new CancellableTask<WorkingCopy>() {
187
188             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
189                 workingCopy.toPhase(Phase.RESOLVED);
190                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
191                 TreeMaker make = workingCopy.getTreeMaker();
192
193                 for (Tree typeDecl : cut.getTypeDecls()) {
194                     // ensure that it is correct type declaration, i.e. class
195
if (Tree.Kind.CLASS == typeDecl.getKind()) {
196                         ClassTree classTree = (ClassTree) typeDecl;
197                         ClassTree copy = make.addClassMember(classTree, m(make));
198                         workingCopy.rewrite(classTree, copy);
199                     }
200                 }
201             }
202             
203             public void cancel() {
204             }
205         };
206         src.runModificationTask(task).commit();
207         String JavaDoc res = TestUtilities.copyFileToString(testFile);
208         System.err.println(res);
209         assertEquals(golden, res);
210     }
211     
212     public void testAddConstructor() throws Exception JavaDoc {
213         testFile = new File JavaDoc(getWorkDir(), "Test.java");
214         TestUtilities.copyStringToFile(testFile,
215             "package hierbas.del.litoral;\n\n" +
216             "public class Test {\n" +
217             " \n" +
218             " String prefix;\n" +
219             " \n" +
220             " public void method() {\n" +
221             " }\n" +
222             " \n" +
223             "}\n"
224             );
225         String JavaDoc golden =
226             "package hierbas.del.litoral;\n\n" +
227             "public class Test {\n" +
228             " \n" +
229             " String prefix;\n" +
230             " \n" +
231             " public Test(boolean prefix) {\n" +
232             " }\n" +
233             "\n" +
234             " public void method() {\n" +
235             " }\n" +
236             " \n" +
237             "}\n";
238
239         JavaSource src = getJavaSource(testFile);
240         CancellableTask task = new CancellableTask<WorkingCopy>() {
241
242             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
243                 workingCopy.toPhase(Phase.RESOLVED);
244                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
245                 TreeMaker make = workingCopy.getTreeMaker();
246
247                 for (Tree typeDecl : cut.getTypeDecls()) {
248                     // ensure that it is correct type declaration, i.e. class
249
if (Tree.Kind.CLASS == typeDecl.getKind()) {
250                         ClassTree classTree = (ClassTree) typeDecl;
251                         ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC));
252                         List JavaDoc<VariableTree> arguments = new ArrayList JavaDoc<VariableTree>();
253                         arguments.add(make.Variable(
254                             make.Modifiers(EnumSet.noneOf(Modifier.class)),
255                             "prefix",
256                             make.PrimitiveType(TypeKind.BOOLEAN), null)
257                         );
258                         MethodTree constructor = make.Method(
259                             mods,
260                             "<init>",
261                             null,
262                             Collections.<TypeParameterTree> emptyList(),
263                             arguments,
264                             Collections.<ExpressionTree>emptyList(),
265                             make.Block(Collections.<StatementTree>emptyList(), false),
266                             null
267                         );
268                         ClassTree copy = make.insertClassMember(classTree, 2, constructor);
269                         workingCopy.rewrite(classTree, copy);
270                     }
271                 }
272             }
273             
274             public void cancel() {
275             }
276         };
277         src.runModificationTask(task).commit();
278         String JavaDoc res = TestUtilities.copyFileToString(testFile);
279         System.err.println(res);
280         assertEquals(golden, res);
281     }
282     
283     public void testInsertFieldToIndex0() throws Exception JavaDoc {
284         testFile = new File JavaDoc(getWorkDir(), "Test.java");
285         TestUtilities.copyStringToFile(testFile,
286             "package hierbas.del.litoral;\n\n" +
287             "public class Test {\n" +
288             " \n" +
289             " int i = 0;\n" +
290             " \n" +
291             " public Test() {\n" +
292             " }\n" +
293             " \n" +
294             "}\n"
295             );
296         String JavaDoc golden =
297             "package hierbas.del.litoral;\n\n" +
298             "public class Test {\n" +
299             " \n" +
300             " String prefix;\n" +
301             "\n" +
302             " int i = 0;\n" +
303             " \n" +
304             " public Test() {\n" +
305             " }\n" +
306             " \n" +
307             "}\n";
308
309         JavaSource src = getJavaSource(testFile);
310         
311         CancellableTask task = new CancellableTask<WorkingCopy>() {
312             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
313                 workingCopy.toPhase(Phase.RESOLVED);
314                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
315                 TreeMaker make = workingCopy.getTreeMaker();
316                 for (Tree typeDecl : cut.getTypeDecls()) {
317                     if (Tree.Kind.CLASS == typeDecl.getKind()) {
318                         ClassTree clazz = (ClassTree) typeDecl;
319                         VariableTree member = make.Variable(
320                                 make.Modifiers(Collections.<Modifier>emptySet()),
321                                 "prefix",
322                                 make.Identifier("String"),
323                                 null
324                             );
325                         ClassTree modifiedClazz = make.insertClassMember(clazz, 0, member);
326                         workingCopy.rewrite(clazz,modifiedClazz);
327                     }
328                 }
329             }
330             public void cancel() {}
331         };
332         src.runModificationTask(task).commit();
333         String JavaDoc res = TestUtilities.copyFileToString(testFile);
334         System.err.println(res);
335         assertEquals(golden, res);
336     }
337     
338     public void testModifyFieldName() throws Exception JavaDoc {
339         testFile = new File JavaDoc(getWorkDir(), "Test.java");
340         TestUtilities.copyStringToFile(testFile,
341             "package hierbas.del.litoral;\n\n" +
342             "public class Test {\n" +
343             " \n" +
344             " int i = 0;\n" +
345             " \n" +
346             " public Test() {\n" +
347             " }\n" +
348             " \n" +
349             "}\n"
350             );
351         String JavaDoc golden =
352             "package hierbas.del.litoral;\n\n" +
353             "public class Test {\n" +
354             " \n" +
355             " int newFieldName = 0;\n" +
356             " \n" +
357             " public Test() {\n" +
358             " }\n" +
359             " \n" +
360             "}\n";
361
362         JavaSource src = getJavaSource(testFile);
363         
364         CancellableTask task = new CancellableTask<WorkingCopy>() {
365             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
366                 workingCopy.toPhase(Phase.RESOLVED);
367                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
368                 TreeMaker make = workingCopy.getTreeMaker();
369                 for (Tree typeDecl : cut.getTypeDecls()) {
370                     if (Tree.Kind.CLASS == typeDecl.getKind()) {
371                         VariableTree variable = (VariableTree) ((ClassTree) typeDecl).getMembers().get(0);
372                         VariableTree copy = make.setLabel(variable, "newFieldName");
373                         workingCopy.rewrite(variable, copy);
374                     }
375                 }
376             }
377             public void cancel() {}
378         };
379         src.runModificationTask(task).commit();
380         String JavaDoc res = TestUtilities.copyFileToString(testFile);
381         System.err.println(res);
382         assertEquals(golden, res);
383     }
384     
385     public void testModifyModifiers() throws Exception JavaDoc {
386         testFile = new File JavaDoc(getWorkDir(), "Test.java");
387         TestUtilities.copyStringToFile(testFile,
388             "package hierbas.del.litoral;\n\n" +
389             "public class Test {\n" +
390             " \n" +
391             " private int i = 0;\n" +
392             " \n" +
393             " public Test() {\n" +
394             " }\n" +
395             " \n" +
396             "}\n"
397             );
398         String JavaDoc golden =
399             "package hierbas.del.litoral;\n\n" +
400             "public class Test {\n" +
401             " \n" +
402             " public int i = 0;\n" +
403             " \n" +
404             " public Test() {\n" +
405             " }\n" +
406             " \n" +
407             "}\n";
408
409         JavaSource src = getJavaSource(testFile);
410         
411         CancellableTask task = new CancellableTask<WorkingCopy>() {
412             public void run(WorkingCopy workingCopy) throws java.io.IOException JavaDoc {
413                 workingCopy.toPhase(Phase.RESOLVED);
414                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
415                 TreeMaker make = workingCopy.getTreeMaker();
416                 for (Tree typeDecl : cut.getTypeDecls()) {
417                     if (Tree.Kind.CLASS == typeDecl.getKind()) {
418                         VariableTree variable = (VariableTree) ((ClassTree) typeDecl).getMembers().get(0);
419                         ModifiersTree mods = variable.getModifiers();
420                         workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)));
421                     }
422                 }
423             }
424             
425             public void cancel() {
426             }
427         };
428         src.runModificationTask(task).commit();
429         String JavaDoc res = TestUtilities.copyFileToString(testFile);
430         System.err.println(res);
431         assertEquals(golden, res);
432     }
433     
434     public void testAddToEmptyInterface() throws Exception JavaDoc {
435         //member position 2 is actually after the taragui method, as position 0 is the syntetic constructor:
436
testFile = new File JavaDoc(getWorkDir(), "Test.java");
437         TestUtilities.copyStringToFile(testFile,
438             "package hierbas.del.litoral;\n\n" +
439             "public interface Test {\n" +
440             "}\n"
441             );
442         String JavaDoc golden =
443             "package hierbas.del.litoral;\n\n" +
444             "public interface Test {\n\n" +
445             " public void newlyCreatedMethod(int a, float b) throws java.io.IOException ;\n" +
446             "}\n";
447
448         JavaSource src = getJavaSource(testFile);
449         CancellableTask task = new CancellableTask<WorkingCopy>() {
450
451             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
452                 workingCopy.toPhase(Phase.RESOLVED);
453                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
454                 TreeMaker make = workingCopy.getTreeMaker();
455
456                 for (Tree typeDecl : cut.getTypeDecls()) {
457                     // ensure that it is correct type declaration, i.e. class
458
if (Tree.Kind.CLASS == typeDecl.getKind()) {
459                         ClassTree classTree = (ClassTree) typeDecl;
460                         MethodTree method = m(make);
461                         MethodTree methodC = make.Method(method.getModifiers(),
462                                 method.getName(),
463                                 method.getReturnType(),
464                                 (List JavaDoc<TypeParameterTree>) method.getTypeParameters(),
465                                 method.getParameters(),
466                                 method.getThrows(),
467                                 (BlockTree) null,
468                                 null
469                         );
470                         ClassTree copy = make.addClassMember(classTree, methodC);
471                         workingCopy.rewrite(classTree, copy);
472                     }
473                 }
474             }
475             
476             public void cancel() {
477             }
478         };
479         src.runModificationTask(task).commit();
480         String JavaDoc res = TestUtilities.copyFileToString(testFile);
481         System.err.println(res);
482         assertEquals(golden, res);
483     }
484     
485     public void testAddNewClassWithNewMembers() throws Exception JavaDoc {
486         testFile = new File JavaDoc(getWorkDir(), "Test.java");
487         TestUtilities.copyStringToFile(testFile,
488                 "package hierbas.del.litoral;\n\n" +
489                 "public class Test {\n" +
490                 "}\n"
491                 );
492         String JavaDoc golden =
493                 "package hierbas.del.litoral;\n\n" +
494                 "public class Test {\n" +
495                 " public class X {\n" +
496                 " private int i;\n\n" +
497                 " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" +
498                 " }\n" +
499                 " }\n" +
500                 "}\n";
501         
502         JavaSource src = getJavaSource(testFile);
503         CancellableTask task = new CancellableTask<WorkingCopy>() {
504             
505             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
506                 workingCopy.toPhase(Phase.RESOLVED);
507                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
508                 TreeMaker make = workingCopy.getTreeMaker();
509                 
510                 ClassTree ct = (ClassTree) cut.getTypeDecls().get(0);
511                 MethodTree method = m(make);
512                 MethodTree methodC = make.Method(method.getModifiers(),
513                         method.getName(),
514                         method.getReturnType(),
515                         (List JavaDoc<TypeParameterTree>) method.getTypeParameters(),
516                         method.getParameters(),
517                         method.getThrows(),
518                         "{}",
519                         null
520                         );
521                 VariableTree var = make.Variable(make.Modifiers(EnumSet.of(Modifier.PRIVATE)), "i", make.Type(workingCopy.getTypes().getPrimitiveType(TypeKind.INT)), null);
522                 ClassTree nueClass = make.Class(make.Modifiers(EnumSet.of(Modifier.PUBLIC)), "X", Collections.<TypeParameterTree>emptyList(), null, Collections.<ExpressionTree>emptyList(), Arrays.asList(var, methodC));
523                 ClassTree copy = make.addClassMember(ct, nueClass);
524                 workingCopy.rewrite(ct, copy);
525             }
526             
527             public void cancel() {
528             }
529         };
530         src.runModificationTask(task).commit();
531         String JavaDoc res = TestUtilities.copyFileToString(testFile);
532         System.err.println(res);
533         assertEquals(golden, res);
534     }
535     
536     private MethodTree m(TreeMaker make) {
537         // create method modifiers
538
ModifiersTree parMods = make.Modifiers(Collections.<Modifier>emptySet(), Collections.<AnnotationTree>emptyList());
539         // create parameters
540
VariableTree par1 = make.Variable(parMods, "a", make.PrimitiveType(TypeKind.INT), null);
541         VariableTree par2 = make.Variable(parMods, "b", make.PrimitiveType(TypeKind.FLOAT), null);
542         List JavaDoc<VariableTree> parList = new ArrayList JavaDoc<VariableTree>(2);
543         parList.add(par1);
544         parList.add(par2);
545         // create method
546
MethodTree newMethod = make.Method(
547             make.Modifiers(
548                 Collections.singleton(Modifier.PUBLIC), // modifiers
549
Collections.EMPTY_LIST // annotations
550
), // modifiers and annotations
551
"newlyCreatedMethod", // name
552
make.PrimitiveType(TypeKind.VOID), // return type
553
Collections.EMPTY_LIST, // type parameters for parameters
554
parList, // parameters
555
Collections.singletonList(make.Identifier("java.io.IOException")), // throws
556
make.Block(Collections.EMPTY_LIST, false), // empty statement block
557
null // default value - not applicable here, used by annotations
558
);
559         return newMethod;
560     }
561     
562     /**
563      * #92726, #92127: When semicolon is in class declaration, it is represented
564      * as an empty initilizer in the tree with position -1. This causes many
565      * problems during generating. See issues for details.
566      */

567     public void testAddAfterEmptyInit1() throws Exception JavaDoc {
568         testFile = new File JavaDoc(getWorkDir(), "Test.java");
569         TestUtilities.copyStringToFile(testFile,
570             "package hierbas.del.litoral;\n\n" +
571             "public class Test {\n" +
572             " static enum Enumerace {\n" +
573             " A, B\n" +
574             " };\n" +
575             "}\n"
576             );
577         String JavaDoc golden =
578             "package hierbas.del.litoral;\n\n" +
579             "public class Test {\n" +
580             " static enum Enumerace {\n" +
581             " A, B\n" +
582             " };\n\n" +
583             " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" +
584             " }\n" +
585             "}\n";
586
587         JavaSource src = getJavaSource(testFile);
588         CancellableTask task = new CancellableTask<WorkingCopy>() {
589
590             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
591                 workingCopy.toPhase(Phase.RESOLVED);
592                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
593                 TreeMaker make = workingCopy.getTreeMaker();
594
595                 for (Tree typeDecl : cut.getTypeDecls()) {
596                     // ensure that it is correct type declaration, i.e. class
597
if (Tree.Kind.CLASS == typeDecl.getKind()) {
598                         ClassTree classTree = (ClassTree) typeDecl;
599                         ClassTree copy = make.addClassMember(classTree, m(make));
600                         workingCopy.rewrite(classTree, copy);
601                     }
602                 }
603             }
604             
605             public void cancel() {
606             }
607         };
608         src.runModificationTask(task).commit();
609         String JavaDoc res = TestUtilities.copyFileToString(testFile);
610         System.err.println(res);
611         assertEquals(golden, res);
612     }
613     
614     /**
615      * #92726, #92127: When semicolon is in class declaration, it is represented
616      * as an empty initilizer in the tree with position -1. This causes many
617      * problems during generating. See issues for details.
618      */

619     public void testAddAfterEmptyInit2() throws Exception JavaDoc {
620         testFile = new File JavaDoc(getWorkDir(), "Test.java");
621         TestUtilities.copyStringToFile(testFile,
622             "package hierbas.del.litoral;\n\n" +
623             "public class Test {\n" +
624             " ;\n" +
625             "}\n"
626             );
627         String JavaDoc golden =
628             "package hierbas.del.litoral;\n\n" +
629             "public class Test {\n\n" +
630             " ;\n\n" +
631             " public void newlyCreatedMethod(int a, float b) throws java.io.IOException {\n" +
632             " }\n" +
633             "}\n";
634
635         JavaSource src = getJavaSource(testFile);
636         CancellableTask task = new CancellableTask<WorkingCopy>() {
637
638             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
639                 workingCopy.toPhase(Phase.RESOLVED);
640                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
641                 TreeMaker make = workingCopy.getTreeMaker();
642
643                 for (Tree typeDecl : cut.getTypeDecls()) {
644                     // ensure that it is correct type declaration, i.e. class
645
if (Tree.Kind.CLASS == typeDecl.getKind()) {
646                         ClassTree classTree = (ClassTree) typeDecl;
647                         ClassTree copy = make.addClassMember(classTree, m(make));
648                         workingCopy.rewrite(classTree, copy);
649                     }
650                 }
651             }
652             
653             public void cancel() {
654             }
655         };
656         src.runModificationTask(task).commit();
657         String JavaDoc res = TestUtilities.copyFileToString(testFile);
658         System.err.println(res);
659         assertEquals(golden, res);
660     }
661     
662     /**
663      * #96070
664      */

665     public void testAddInnerInterface() throws Exception JavaDoc {
666         testFile = new File JavaDoc(getWorkDir(), "Test.java");
667         TestUtilities.copyStringToFile(testFile,
668             "package hierbas.del.litoral;\n\n" +
669             "public class Test {\n" +
670             "}\n"
671             );
672         String JavaDoc golden =
673             "package hierbas.del.litoral;\n\n" +
674             "public class Test {\n" +
675             " interface Honza {\n" +
676             " }\n" +
677             "}\n";
678
679         JavaSource src = getJavaSource(testFile);
680         CancellableTask task = new CancellableTask<WorkingCopy>() {
681
682             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
683                 workingCopy.toPhase(Phase.RESOLVED);
684                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
685                 TreeMaker make = workingCopy.getTreeMaker();
686                 ClassTree topLevel = (ClassTree) cut.getTypeDecls().get(0);
687                 ClassTree innerIntfc = make.Interface(make.Modifiers(
688                         Collections.<Modifier>emptySet()),
689                         "Honza",
690                         Collections.<TypeParameterTree>emptyList(),
691                         Collections.<ExpressionTree>emptyList(),
692                         Collections.<Tree>emptyList()
693                 );
694                 workingCopy.rewrite(topLevel, make.addClassMember(topLevel, innerIntfc));
695             }
696             
697             public void cancel() {
698             }
699         };
700         src.runModificationTask(task).commit();
701         String JavaDoc res = TestUtilities.copyFileToString(testFile);
702         System.err.println(res);
703         assertEquals(golden, res);
704     }
705     
706     /**
707      * #96070
708      */

709     public void testAddInnerAnnotationType() throws Exception JavaDoc {
710         testFile = new File JavaDoc(getWorkDir(), "Test.java");
711         TestUtilities.copyStringToFile(testFile,
712             "package hierbas.del.litoral;\n\n" +
713             "public class Test {\n" +
714             "}\n"
715             );
716         String JavaDoc golden =
717             "package hierbas.del.litoral;\n\n" +
718             "public class Test {\n" +
719             " public @interface Honza {\n" +
720             " }\n" +
721             "}\n";
722
723         JavaSource src = getJavaSource(testFile);
724         CancellableTask task = new CancellableTask<WorkingCopy>() {
725
726             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
727                 workingCopy.toPhase(Phase.RESOLVED);
728                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
729                 TreeMaker make = workingCopy.getTreeMaker();
730                 ClassTree topLevel = (ClassTree) cut.getTypeDecls().get(0);
731                 ClassTree innerIntfc = make.AnnotationType(make.Modifiers(
732                         Collections.<Modifier>singleton(Modifier.PUBLIC)),
733                         "Honza",
734                         Collections.<Tree>emptyList()
735                 );
736                 workingCopy.rewrite(topLevel, make.addClassMember(topLevel, innerIntfc));
737             }
738             
739             public void cancel() {
740             }
741         };
742         src.runModificationTask(task).commit();
743         String JavaDoc res = TestUtilities.copyFileToString(testFile);
744         System.err.println(res);
745         assertEquals(golden, res);
746     }
747     
748     /**
749      * #96070
750      */

751     public void testAddInnerEnum() throws Exception JavaDoc {
752         testFile = new File JavaDoc(getWorkDir(), "Test.java");
753         TestUtilities.copyStringToFile(testFile,
754             "package hierbas.del.litoral;\n\n" +
755             "public class Test {\n" +
756             "}\n"
757             );
758         String JavaDoc golden =
759             "package hierbas.del.litoral;\n\n" +
760             "public class Test {\n" +
761             " protected enum Honza {\n" +
762             " }\n" +
763             "}\n";
764
765         JavaSource src = getJavaSource(testFile);
766         CancellableTask task = new CancellableTask<WorkingCopy>() {
767
768             public void run(WorkingCopy workingCopy) throws IOException JavaDoc {
769                 workingCopy.toPhase(Phase.RESOLVED);
770                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
771                 TreeMaker make = workingCopy.getTreeMaker();
772                 ClassTree topLevel = (ClassTree) cut.getTypeDecls().get(0);
773                 ClassTree innerIntfc = make.Enum(make.Modifiers(
774                         Collections.<Modifier>singleton(Modifier.PROTECTED)),
775                         "Honza",
776                         Collections.<ExpressionTree>emptyList(),
777                         Collections.<Tree>emptyList()
778                 );
779                 workingCopy.rewrite(topLevel, make.addClassMember(topLevel, innerIntfc));
780             }
781             
782             public void cancel() {
783             }
784         };
785         src.runModificationTask(task).commit();
786         String JavaDoc res = TestUtilities.copyFileToString(testFile);
787         System.err.println(res);
788         assertEquals(golden, res);
789     }
790     
791     String JavaDoc getGoldenPckg() {
792         return "";
793     }
794
795     String JavaDoc getSourcePckg() {
796         return "";
797     }
798     
799 }
800
Popular Tags