KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 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 java.io.*;
23 import java.util.Collections JavaDoc;
24 import javax.lang.model.element.Modifier;
25 import javax.lang.model.type.TypeKind;
26 import org.netbeans.api.java.source.*;
27 import org.netbeans.api.java.source.JavaSource.*;
28 import org.netbeans.junit.NbTestSuite;
29
30 /**
31  *
32  * @author Pavel Flaska
33  */

34 public class AnonymousClassTest extends GeneratorTestMDRCompat {
35     
36     /** Creates a new instance of AnonymousClassTest
37      * @param name
38      */

39     public AnonymousClassTest(String JavaDoc name) {
40         super(name);
41     }
42     
43     /**
44      * @return
45      */

46     public static NbTestSuite suite() {
47         NbTestSuite suite = new NbTestSuite();
48         suite.addTestSuite(AnonymousClassTest.class);
49 // suite.addTest(new AnonymousClassTest("testAddMethodToInvocParam"));
50
// suite.addTest(new AnonymousClassTest("testAddInsideAnnWhenAnnInPar"));
51
return suite;
52     }
53     
54     /**
55      * #96364: When completing NewClassTree parameter in invocation,
56      * nothing is generated.
57      * Example:
58      *
59      * method(new Runnable| );
60      *
61      * should be completed to
62      *
63      * method(new Runnable {
64      * public void run() {
65      * }
66      * });
67      */

68     public void testAddMethodToInvocParam() throws Exception JavaDoc {
69         testFile = new File(getWorkDir(), "Test.java");
70         TestUtilities.copyStringToFile(testFile,
71             "package hierbas.del.litoral;\n\n" +
72             "class Test {\n" +
73             " void method(Runnable r) {\n" +
74             " method(new Runnable() {});\n" +
75             " }" +
76             "}\n");
77         String JavaDoc golden =
78             "package hierbas.del.litoral;\n\n" +
79             "class Test {\n" +
80             " void method(Runnable r) {\n" +
81             " method(new Runnable() {\n" +
82             " public void run() {\n" +
83             " }\n" +
84             "});\n" +
85             " }}\n";
86
87         JavaSource src = getJavaSource(testFile);
88         CancellableTask task = new CancellableTask<WorkingCopy>() {
89
90             public void run(WorkingCopy workingCopy) throws IOException {
91                 workingCopy.toPhase(Phase.RESOLVED);
92                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
93                 TreeMaker make = workingCopy.getTreeMaker();
94                 
95                 ClassTree testClass = (ClassTree) cut.getTypeDecls().get(0);
96                 MethodTree method = (MethodTree) testClass.getMembers().get(1);
97                 
98                 ExpressionStatementTree est = (ExpressionStatementTree) method.getBody().getStatements().get(0);
99                 MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
100                 NewClassTree nct = (NewClassTree) mit.getArguments().get(0);
101                 MethodTree m = make.Method(
102                     make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)),
103                     "run",
104                     make.PrimitiveType(TypeKind.VOID),
105                     Collections.<TypeParameterTree>emptyList(),
106                     Collections.<VariableTree>emptyList(),
107                     Collections.<ExpressionTree>emptyList(),
108                     make.Block(Collections.<StatementTree>emptyList(), false),
109                     null
110                 );
111                 workingCopy.rewrite(nct.getClassBody(), make.addClassMember(nct.getClassBody(), m));
112             }
113             
114             public void cancel() {
115             }
116         };
117         src.runModificationTask(task).commit();
118         String JavaDoc res = TestUtilities.copyFileToString(testFile);
119         System.err.println(res);
120         assertEquals(golden, res);
121     }
122     
123     /**
124      * Deva issue.
125      * Example:
126      *
127      * public class Main {
128      * void m(Runnable r) {
129      * }
130      *
131      * void method() {
132      * m(new Runnable() {
133      * public void run() {
134      * Object o = null;
135      * String s = o;
136      * }
137      * });
138      * }
139      * }
140      *
141      * When statement is changed, e.g. 'String s = o;' is changed
142      * to 'String s = (String) o;', it is not replaced in the source.
143      */

144     public void testAddInsideAnnWhenAnnInPar() throws Exception JavaDoc {
145         testFile = new File(getWorkDir(), "Test.java");
146         TestUtilities.copyStringToFile(testFile,
147             "package javaapplication1;\n" +
148             "\n" +
149             "public class Main {\n" +
150             " void m(Runnable r) {\n" +
151             " }\n" +
152             " \n" +
153             " void method() {\n" +
154             " m(new Runnable() {\n" +
155             " public void run() {\n" +
156             " Object o = null;\n" +
157             " String s = o;\n" +
158             " }\n" +
159             " });\n" +
160             " }\n" +
161             "}\n");
162         String JavaDoc golden =
163             "package javaapplication1;\n" +
164             "\n" +
165             "public class Main {\n" +
166             " void m(Runnable r) {\n" +
167             " }\n" +
168             " \n" +
169             " void method() {\n" +
170             " m(new Runnable() {\n" +
171             " public void run() {\n" +
172             " Object o = null;\n" +
173             " String s = (String) o;\n" +
174             " }\n" +
175             " });\n" +
176             " }\n" +
177             "}\n";
178
179         JavaSource src = getJavaSource(testFile);
180         CancellableTask task = new CancellableTask<WorkingCopy>() {
181
182             public void run(WorkingCopy workingCopy) throws IOException {
183                 workingCopy.toPhase(Phase.RESOLVED);
184                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
185                 TreeMaker make = workingCopy.getTreeMaker();
186                 
187                 ClassTree testClass = (ClassTree) cut.getTypeDecls().get(0);
188                 MethodTree method = (MethodTree) testClass.getMembers().get(2);
189                 BlockTree block = method.getBody();
190                 ExpressionStatementTree est = (ExpressionStatementTree) block.getStatements().get(0);
191                 MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
192                 NewClassTree nct = (NewClassTree) mit.getArguments().get(0);
193                 ClassTree clazzTree = nct.getClassBody();
194                 method = (MethodTree) clazzTree.getMembers().get(1);
195                 VariableTree vt = (VariableTree) method.getBody().getStatements().get(1);
196                 ExpressionTree init = vt.getInitializer();
197                 ExpressionTree cast = make.TypeCast(make.Identifier("String"), init);
198                 workingCopy.rewrite(init, cast);
199             }
200             
201             public void cancel() {
202             }
203         };
204         src.runModificationTask(task).commit();
205         String JavaDoc res = TestUtilities.copyFileToString(testFile);
206         System.err.println(res);
207         assertEquals(golden, res);
208     }
209     
210     String JavaDoc getGoldenPckg() {
211         return "";
212     }
213
214     String JavaDoc getSourcePckg() {
215         return "";
216     }
217 }
218
Popular Tags