001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.api.java.source.gen;
042:
043: import com.sun.source.tree.BlockTree;
044: import com.sun.source.tree.ClassTree;
045: import com.sun.source.tree.CompilationUnitTree;
046: import com.sun.source.tree.ExpressionTree;
047: import com.sun.source.tree.ForLoopTree;
048: import com.sun.source.tree.MethodTree;
049: import com.sun.source.tree.VariableTree;
050: import java.io.File;
051: import java.io.IOException;
052: import org.netbeans.api.java.source.Task;
053: import org.netbeans.api.java.source.JavaSource;
054: import org.netbeans.api.java.source.JavaSource.Phase;
055: import org.netbeans.api.java.source.TestUtilities;
056: import org.netbeans.api.java.source.TreeMaker;
057: import org.netbeans.api.java.source.WorkingCopy;
058: import org.netbeans.junit.NbTestSuite;
059:
060: /**
061: * Tests correct adding cast to statement.
062: *
063: * @author Pavel Flaska
064: */
065: public class AddCastTest extends GeneratorTestMDRCompat {
066:
067: /** Creates a new instance of AddCastTest */
068: public AddCastTest(String name) {
069: super (name);
070: }
071:
072: public static NbTestSuite suite() {
073: NbTestSuite suite = new NbTestSuite();
074: suite.addTestSuite(AddCastTest.class);
075: return suite;
076: }
077:
078: public void testAddCastToDeclStmt() throws Exception {
079: testFile = new File(getWorkDir(), "Test.java");
080: TestUtilities
081: .copyStringToFile(
082: testFile,
083: "package hierbas.del.litoral;\n\n"
084: + "import java.util.*;\n\n"
085: + "public class Test<E> {\n"
086: + " public void taragui() {\n"
087: + " System.err.println(\"taragui() method\");\n"
088: + " String s = \"Oven.\";\n"
089: + "// line comment\n"
090: + " }\n" + "}\n");
091: String golden = "package hierbas.del.litoral;\n\n"
092: + "import java.util.*;\n\n"
093: + "public class Test<E> {\n"
094: + " public void taragui() {\n"
095: + " System.err.println(\"taragui() method\");\n"
096: + " String s = (String) \"Oven.\";\n"
097: + "// line comment\n" + " }\n" + "}\n";
098: JavaSource src = getJavaSource(testFile);
099:
100: Task<WorkingCopy> task = new Task<WorkingCopy>() {
101:
102: public void run(WorkingCopy workingCopy) throws IOException {
103: workingCopy.toPhase(Phase.RESOLVED);
104: CompilationUnitTree cut = workingCopy
105: .getCompilationUnit();
106: TreeMaker make = workingCopy.getTreeMaker();
107: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
108: MethodTree method = (MethodTree) clazz.getMembers()
109: .get(1);
110: VariableTree var = (VariableTree) method.getBody()
111: .getStatements().get(1);
112: ExpressionTree init = var.getInitializer();
113: ExpressionTree cast = make.TypeCast(make
114: .Identifier("String"), init);
115: workingCopy.rewrite(init, cast);
116: }
117:
118: };
119: src.runModificationTask(task).commit();
120: String res = TestUtilities.copyFileToString(testFile);
121: System.err.println(res);
122: assertEquals(golden, res);
123: }
124:
125: public void testAddCastInForWithoutSteps() throws Exception {
126: testFile = new File(getWorkDir(), "Test.java");
127: TestUtilities.copyStringToFile(testFile,
128: "package hierbas.del.litoral;\n\n"
129: + "import java.util.*;\n\n"
130: + "public class Test<E> {\n"
131: + " public void cast() {\n"
132: + " Object o = null;\n"
133: + " for (int i = 0; i < 5; ) {\n"
134: + " String s = o;\n" + " }\n"
135: + " }\n" + "}\n");
136: String golden = "package hierbas.del.litoral;\n\n"
137: + "import java.util.*;\n\n"
138: + "public class Test<E> {\n"
139: + " public void cast() {\n"
140: + " Object o = null;\n"
141: + " for (int i = 0; i < 5; ) {\n"
142: + " String s = (String) o;\n"
143: + " }\n" + " }\n" + "}\n";
144: JavaSource src = getJavaSource(testFile);
145:
146: Task<WorkingCopy> task = new Task<WorkingCopy>() {
147:
148: public void run(WorkingCopy workingCopy) throws IOException {
149: workingCopy.toPhase(Phase.RESOLVED);
150: CompilationUnitTree cut = workingCopy
151: .getCompilationUnit();
152: TreeMaker make = workingCopy.getTreeMaker();
153: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
154: MethodTree method = (MethodTree) clazz.getMembers()
155: .get(1);
156: ForLoopTree forLoop = (ForLoopTree) method.getBody()
157: .getStatements().get(1);
158: BlockTree block = (BlockTree) forLoop.getStatement();
159: VariableTree vt = (VariableTree) block.getStatements()
160: .get(0);
161: ExpressionTree init = vt.getInitializer();
162: ExpressionTree cast = make.TypeCast(make
163: .Identifier("String"), init);
164: workingCopy.rewrite(init, cast);
165: }
166:
167: };
168: src.runModificationTask(task).commit();
169: String res = TestUtilities.copyFileToString(testFile);
170: System.err.println(res);
171: assertEquals(golden, res);
172: }
173:
174: /*
175: * #94324
176: */
177: public void testAddCast94324() throws Exception {
178: testFile = new File(getWorkDir(), "Test.java");
179: TestUtilities
180: .copyStringToFile(
181: testFile,
182: "package javaapplication3;\n"
183: + "\n"
184: + "public class Klasa {\n"
185: + " \n"
186: + " static Object method() {\n"
187: + " return null;\n"
188: + " }\n"
189: + " \n"
190: + " public static void main(String[] args) {\n"
191: + " // TODO code application logic here\n"
192: + " String s = Klasa.method();\n"
193: + " }\n" + "\n" + "}\n");
194: String golden = "package javaapplication3;\n" + "\n"
195: + "public class Klasa {\n" + " \n"
196: + " static Object method() {\n"
197: + " return null;\n" + " }\n" + " \n"
198: + " public static void main(String[] args) {\n"
199: + " // TODO code application logic here\n"
200: + " String s = (String) Klasa.method();\n"
201: + " }\n" + "\n" + "}\n";
202: JavaSource src = getJavaSource(testFile);
203:
204: Task<WorkingCopy> task = new Task<WorkingCopy>() {
205:
206: public void run(WorkingCopy workingCopy) throws IOException {
207: workingCopy.toPhase(Phase.RESOLVED);
208: CompilationUnitTree cut = workingCopy
209: .getCompilationUnit();
210: TreeMaker make = workingCopy.getTreeMaker();
211: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
212: MethodTree method = (MethodTree) clazz.getMembers()
213: .get(2);
214: VariableTree var = (VariableTree) method.getBody()
215: .getStatements().get(0);
216: ExpressionTree init = var.getInitializer();
217: ExpressionTree cast = make.TypeCast(make
218: .Identifier("String"), init);
219: workingCopy.rewrite(init, cast);
220: }
221:
222: };
223: src.runModificationTask(task).commit();
224: String res = TestUtilities.copyFileToString(testFile);
225: System.err.println(res);
226: assertEquals(golden, res);
227: }
228:
229: /*
230: * #94324 - second test. If the moved expression is changed, it is again
231: * rewritten by VeryPretty - in this time, i do not see any solution for
232: * that.
233: *//*
234: public void testAddCast94324_2() throws Exception {
235: testFile = new File(getWorkDir(), "Test.java");
236: TestUtilities.copyStringToFile(testFile,
237: "package javaapplication3;\n" +
238: "\n" +
239: "public class Klasa {\n" +
240: " \n" +
241: " static Object method() {\n" +
242: " return null;\n" +
243: " }\n" +
244: " \n" +
245: " public static void main(String[] args) {\n" +
246: " // TODO code application logic here\n" +
247: " String s = Klasa.method();\n" +
248: " }\n" +
249: "\n" +
250: "}\n"
251: );
252: String golden =
253: "package javaapplication3;\n" +
254: "\n" +
255: "public class Klasa {\n" +
256: " \n" +
257: " static Object method() {\n" +
258: " return null;\n" +
259: " }\n" +
260: " \n" +
261: " public static void main(String[] args) {\n" +
262: " // TODO code application logic here\n" +
263: " String s = (String) javaapplication3.Klasa.metoda();\n" +
264: " }\n" +
265: "\n" +
266: "}\n";
267: JavaSource src = getJavaSource(testFile);
268:
269: CancellableTask<WorkingCopy> task = new CancellableTask<WorkingCopy>() {
270:
271: public void run(WorkingCopy workingCopy) throws IOException {
272: workingCopy.toPhase(Phase.RESOLVED);
273: CompilationUnitTree cut = workingCopy.getCompilationUnit();
274: TreeMaker make = workingCopy.getTreeMaker();
275: ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
276: MethodTree method = (MethodTree) clazz.getMembers().get(2);
277: VariableTree var = (VariableTree) method.getBody().getStatements().get(0);
278: MethodInvocationTree init = (MethodInvocationTree) var.getInitializer();
279: MethodInvocationTree initCopy = make.MethodInvocation(
280: (List<ExpressionTree>) init.getTypeArguments(),
281: make.setLabel((MemberSelectTree) init.getMethodSelect(), "metoda"),
282: init.getArguments()
283: );
284: workingCopy.rewrite(init, initCopy);
285: ExpressionTree cast = make.TypeCast(make.Identifier("String"), initCopy);
286: workingCopy.rewrite(init, cast);
287: }
288:
289: public void cancel() {
290: }
291: };
292: src.runModificationTask(task).commit();
293: String res = TestUtilities.copyFileToString(testFile);
294: System.err.println(res);
295: assertEquals(golden, res);
296: }*/
297:
298: String getGoldenPckg() {
299: return "";
300: }
301:
302: String getSourcePckg() {
303: return "";
304: }
305:
306: }
|