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.*;
044: import java.io.*;
045: import java.util.Collections;
046: import javax.lang.model.element.Modifier;
047: import javax.lang.model.type.TypeKind;
048: import org.netbeans.api.java.source.*;
049: import org.netbeans.api.java.source.JavaSource.*;
050: import org.netbeans.junit.NbTestSuite;
051:
052: /**
053: *
054: * @author Pavel Flaska
055: */
056: public class AnonymousClassTest extends GeneratorTestMDRCompat {
057:
058: /** Creates a new instance of AnonymousClassTest
059: * @param name
060: */
061: public AnonymousClassTest(String name) {
062: super (name);
063: }
064:
065: /**
066: * @return
067: */
068: public static NbTestSuite suite() {
069: NbTestSuite suite = new NbTestSuite();
070: suite.addTestSuite(AnonymousClassTest.class);
071: // suite.addTest(new AnonymousClassTest("testAddMethodToInvocParam"));
072: // suite.addTest(new AnonymousClassTest("testAddInsideAnnWhenAnnInPar"));
073: return suite;
074: }
075:
076: /**
077: * #96364: When completing NewClassTree parameter in invocation,
078: * nothing is generated.
079: * Example:
080: *
081: * method(new Runnable| );
082: *
083: * should be completed to
084: *
085: * method(new Runnable {
086: * public void run() {
087: * }
088: * });
089: */
090: public void testAddMethodToInvocParam() throws Exception {
091: testFile = new File(getWorkDir(), "Test.java");
092: TestUtilities.copyStringToFile(testFile,
093: "package hierbas.del.litoral;\n" + "\n"
094: + "class Test {\n"
095: + " void method(Runnable r) {\n"
096: + " method(new Runnable() {});\n"
097: + " }" + "}\n");
098: String golden = "package hierbas.del.litoral;\n" + "\n"
099: + "class Test {\n" + " void method(Runnable r) {\n"
100: + " method(new Runnable() {\n" + "\n"
101: + " public void run() {\n"
102: + " }\n" + " });\n" + " }}\n";
103:
104: JavaSource src = getJavaSource(testFile);
105: Task task = new Task<WorkingCopy>() {
106:
107: public void run(WorkingCopy workingCopy) throws IOException {
108: workingCopy.toPhase(Phase.RESOLVED);
109: CompilationUnitTree cut = workingCopy
110: .getCompilationUnit();
111: TreeMaker make = workingCopy.getTreeMaker();
112:
113: ClassTree testClass = (ClassTree) cut.getTypeDecls()
114: .get(0);
115: MethodTree method = (MethodTree) testClass.getMembers()
116: .get(1);
117:
118: ExpressionStatementTree est = (ExpressionStatementTree) method
119: .getBody().getStatements().get(0);
120: MethodInvocationTree mit = (MethodInvocationTree) est
121: .getExpression();
122: NewClassTree nct = (NewClassTree) mit.getArguments()
123: .get(0);
124: MethodTree m = make.Method(make.Modifiers(Collections
125: .<Modifier> singleton(Modifier.PUBLIC)), "run",
126: make.PrimitiveType(TypeKind.VOID), Collections
127: .<TypeParameterTree> emptyList(),
128: Collections.<VariableTree> emptyList(),
129: Collections.<ExpressionTree> emptyList(), make
130: .Block(Collections
131: .<StatementTree> emptyList(),
132: false), null);
133: workingCopy.rewrite(nct.getClassBody(), make
134: .addClassMember(nct.getClassBody(), m));
135: }
136:
137: };
138: src.runModificationTask(task).commit();
139: String res = TestUtilities.copyFileToString(testFile);
140: System.err.println(res);
141: assertEquals(golden, res);
142: }
143:
144: /**
145: * Deva issue.
146: * Example:
147: *
148: * public class Main {
149: * void m(Runnable r) {
150: * }
151: *
152: * void method() {
153: * m(new Runnable() {
154: * public void run() {
155: * Object o = null;
156: * String s = o;
157: * }
158: * });
159: * }
160: * }
161: *
162: * When statement is changed, e.g. 'String s = o;' is changed
163: * to 'String s = (String) o;', it is not replaced in the source.
164: */
165: public void testAddInsideAnnWhenAnnInPar() throws Exception {
166: testFile = new File(getWorkDir(), "Test.java");
167: TestUtilities.copyStringToFile(testFile,
168: "package javaapplication1;\n" + "\n"
169: + "public class Main {\n"
170: + " void m(Runnable r) {\n" + " }\n"
171: + " \n" + " void method() {\n"
172: + " m(new Runnable() {\n"
173: + " public void run() {\n"
174: + " Object o = null;\n"
175: + " String s = o;\n"
176: + " }\n" + " });\n"
177: + " }\n" + "}\n");
178: String golden = "package javaapplication1;\n" + "\n"
179: + "public class Main {\n"
180: + " void m(Runnable r) {\n" + " }\n" + " \n"
181: + " void method() {\n"
182: + " m(new Runnable() {\n"
183: + " public void run() {\n"
184: + " Object o = null;\n"
185: + " String s = (String) o;\n"
186: + " }\n" + " });\n" + " }\n"
187: + "}\n";
188:
189: JavaSource src = getJavaSource(testFile);
190: Task task = new Task<WorkingCopy>() {
191:
192: public void run(WorkingCopy workingCopy) throws IOException {
193: workingCopy.toPhase(Phase.RESOLVED);
194: CompilationUnitTree cut = workingCopy
195: .getCompilationUnit();
196: TreeMaker make = workingCopy.getTreeMaker();
197:
198: ClassTree testClass = (ClassTree) cut.getTypeDecls()
199: .get(0);
200: MethodTree method = (MethodTree) testClass.getMembers()
201: .get(2);
202: BlockTree block = method.getBody();
203: ExpressionStatementTree est = (ExpressionStatementTree) block
204: .getStatements().get(0);
205: MethodInvocationTree mit = (MethodInvocationTree) est
206: .getExpression();
207: NewClassTree nct = (NewClassTree) mit.getArguments()
208: .get(0);
209: ClassTree clazzTree = nct.getClassBody();
210: method = (MethodTree) clazzTree.getMembers().get(1);
211: VariableTree vt = (VariableTree) method.getBody()
212: .getStatements().get(1);
213: ExpressionTree init = vt.getInitializer();
214: ExpressionTree cast = make.TypeCast(make
215: .Identifier("String"), init);
216: workingCopy.rewrite(init, cast);
217: }
218:
219: };
220: src.runModificationTask(task).commit();
221: String res = TestUtilities.copyFileToString(testFile);
222: System.err.println(res);
223: assertEquals(golden, res);
224: }
225:
226: String getGoldenPckg() {
227: return "";
228: }
229:
230: String getSourcePckg() {
231: return "";
232: }
233: }
|