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-2006 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.ExpressionTree;
046: import com.sun.source.tree.MethodTree;
047: import com.sun.source.tree.Tree;
048: import com.sun.source.tree.TypeParameterTree;
049: import com.sun.source.tree.VariableTree;
050: import com.sun.source.util.SourcePositions;
051: import java.io.IOException;
052: import java.util.ArrayList;
053: import java.util.Collections;
054: import java.util.List;
055: import java.util.Set;
056: import javax.lang.model.element.Element;
057: import javax.lang.model.element.Modifier;
058: import javax.lang.model.element.TypeElement;
059: import org.netbeans.api.java.source.JavaSource;
060: import org.netbeans.api.java.source.SourceUtilsTestUtil2;
061: import org.netbeans.api.java.source.WorkingCopy;
062: import org.netbeans.modules.java.source.transform.Transformer;
063: import org.netbeans.junit.NbTestSuite;
064: import junit.textui.TestRunner;
065: import org.netbeans.api.java.source.Task;
066: import org.openide.filesystems.FileObject;
067: import org.openide.filesystems.FileUtil;
068:
069: /**
070: * Tests the method generator.
071: *
072: * @author Jan Becicka
073: */
074: public class MethodTest2 extends GeneratorTest {
075:
076: /** Need to be defined because of JUnit */
077: public MethodTest2(String name) {
078: super (name);
079: }
080:
081: public static NbTestSuite suite() {
082: NbTestSuite suite = new NbTestSuite();
083: suite.addTest(new MethodTest2("testMethodAdd"));
084: return suite;
085: }
086:
087: protected void setUp() throws Exception {
088: super .setUp();
089: testFile = getFile(getSourceDir(), getSourcePckg()
090: + "MethodTest2.java");
091: }
092:
093: /**
094: * Changes the modifiers on method. Removes public modifier, sets static
095: * and private modifier.
096: */
097: public void testMethodAdd() throws IOException {
098: FileObject fo = FileUtil.toFileObject(testFile);
099: JavaSource js = JavaSource.forFileObject(fo);
100: js.runModificationTask(new Task<WorkingCopy>() {
101:
102: public void run(WorkingCopy wc) {
103:
104: CreateMethod create = new CreateMethod();
105: SourceUtilsTestUtil2.run(wc, create);
106: MethodTree mt = create.makeMethod();
107:
108: MethodImplGenerator add = new MethodImplGenerator(mt);
109: SourceUtilsTestUtil2.run(wc, add);
110:
111: RenameImplGenerator rename = new RenameImplGenerator(
112: add.method, "foo");
113: SourceUtilsTestUtil2.run(wc, rename);
114:
115: SetTypeGenerator setType = new SetTypeGenerator(
116: rename.method, wc.getElements().getTypeElement(
117: "java.lang.String"));
118: SourceUtilsTestUtil2.run(wc, setType);
119:
120: SourcePositions pos[] = new SourcePositions[1];
121: BlockTree btree = (BlockTree) wc.getTreeUtilities()
122: .parseStatement("{System.out.println();}", pos);
123:
124: SetBodyGenerator setBody = new SetBodyGenerator(
125: setType.method, btree);
126: SourceUtilsTestUtil2.run(wc, setBody);
127: }
128: }).commit();
129:
130: assertFiles("testMethodAdd.pass");
131: }
132:
133: ////////////////////////////////////////////////////////////////////////////
134: /**
135: * @param args the command line arguments
136: */
137: public static void main(String[] args) {
138: TestRunner.run(suite());
139: }
140:
141: String getSourcePckg() {
142: return "org/netbeans/test/codegen/";
143: }
144:
145: String getGoldenPckg() {
146: return "org/netbeans/jmi/javamodel/codegen/MethodTest2/MethodTest2/";
147: }
148:
149: private class SetTypeGenerator extends Transformer<Void, Object> {
150:
151: public MethodTree method;
152: Element newType;
153:
154: public SetTypeGenerator(MethodTree m, Element el) {
155: method = m;
156: this .newType = el;
157: }
158:
159: public Void visitMethod(MethodTree node, Object p) {
160: super .visitMethod(node, p);
161: if (method.equals(node)) {
162: MethodTree njuMethod = make.Method(node.getModifiers(),
163: node.getName().toString(), make
164: .Identifier(newType), node
165: .getTypeParameters(), node
166: .getParameters(), node.getThrows(),
167: node.getBody(), (ExpressionTree) node
168: .getDefaultValue());
169: copy.rewrite(node, njuMethod);
170: method = njuMethod;
171: }
172: return null;
173: }
174: }
175:
176: private class SetBodyGenerator extends Transformer<Void, Object> {
177:
178: public MethodTree method;
179: BlockTree newType;
180:
181: public SetBodyGenerator(MethodTree m, BlockTree tree) {
182: method = m;
183: this .newType = newType;
184: newType = tree;
185: }
186:
187: public Void visitMethod(MethodTree node, Object p) {
188: super .visitMethod(node, p);
189: if (method.equals(node)) {
190: MethodTree njuMethod = make.Method(node.getModifiers(),
191: node.getName().toString(),
192: (ExpressionTree) node.getReturnType(), node
193: .getTypeParameters(), node
194: .getParameters(), node.getThrows(),
195: newType, (ExpressionTree) node
196: .getDefaultValue());
197: method = njuMethod;
198: copy.rewrite(node, njuMethod);
199: }
200: return null;
201: }
202: }
203:
204: private class RenameImplGenerator extends Transformer<Void, Object> {
205:
206: public MethodTree method;
207: String newName;
208:
209: public RenameImplGenerator(MethodTree m, String newName) {
210: method = m;
211: this .newName = newName;
212: }
213:
214: public Void visitMethod(MethodTree node, Object p) {
215: super .visitMethod(node, p);
216: if (method.equals(node)) {
217: MethodTree njuMethod = make.Method(node.getModifiers(),
218: newName, (ExpressionTree) node.getReturnType(),
219: node.getTypeParameters(), node.getParameters(),
220: node.getThrows(), node.getBody(),
221: (ExpressionTree) node.getDefaultValue());
222: copy.rewrite(node, njuMethod);
223: method = njuMethod;
224: }
225: return null;
226: }
227: }
228:
229: private class MethodImplGenerator extends Transformer<Void, Object> {
230:
231: public MethodTree method;
232:
233: public MethodImplGenerator(MethodTree m) {
234: method = m;
235:
236: }
237:
238: public Void visitClass(ClassTree node, Object p) {
239: super .visitClass(node, p);
240: TypeElement te = (TypeElement) model.getElement(node);
241: if (te != null) {
242: List<Tree> members = new ArrayList<Tree>();
243: for (Tree m : node.getMembers())
244: members.add(m);
245: members.add(method);
246: ClassTree decl = make.Class(node.getModifiers(), node
247: .getSimpleName(), node.getTypeParameters(),
248: node.getExtendsClause(),
249: (List<ExpressionTree>) node
250: .getImplementsClause(), members);
251: model.setElement(decl, te);
252: model.setType(decl, model.getType(node));
253: copy.rewrite(node, decl);
254: }
255: return null;
256: }
257: }
258:
259: private class CreateMethod extends Transformer<Void, Object> {
260: public MethodTree makeMethod() {
261: Set<Modifier> emptyModifs = Collections.emptySet();
262: List<TypeParameterTree> emptyTpt = Collections.emptyList();
263: List<VariableTree> emptyVt = Collections.emptyList();
264: List<ExpressionTree> emptyEt = Collections.emptyList();
265: return make.Method(make.Modifiers(emptyModifs),
266: (CharSequence) "", (ExpressionTree) null, emptyTpt,
267: emptyVt, emptyEt, (BlockTree) null,
268: (ExpressionTree) null);
269: }
270: }
271:
272: }
|