KickJava   Java API By Example, From Geeks To Geeks.

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


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.BlockTree;
22 import com.sun.source.tree.ClassTree;
23 import com.sun.source.tree.ExpressionTree;
24 import com.sun.source.tree.MethodTree;
25 import com.sun.source.tree.Tree;
26 import com.sun.source.tree.TypeParameterTree;
27 import com.sun.source.tree.VariableTree;
28 import com.sun.source.util.SourcePositions;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.lang.model.element.Element;
35 import javax.lang.model.element.Modifier;
36 import javax.lang.model.element.TypeElement;
37 import org.netbeans.api.java.source.JavaSource;
38 import org.netbeans.api.java.source.SourceUtilsTestUtil2;
39 import org.netbeans.api.java.source.WorkingCopy;
40 import org.netbeans.api.java.source.transform.Transformer;
41 import org.netbeans.junit.NbTestSuite;
42 import junit.textui.TestRunner;
43 import org.netbeans.api.java.source.CancellableTask;
44 import org.openide.filesystems.FileObject;
45 import org.openide.filesystems.FileUtil;
46
47 /**
48  * Tests the method generator.
49  *
50  * @author Jan Becicka
51  */

52 public class MethodTest2 extends GeneratorTest {
53     
54     /** Need to be defined because of JUnit */
55     public MethodTest2(String JavaDoc name) {
56         super(name);
57     }
58     
59     public static NbTestSuite suite() {
60         NbTestSuite suite = new NbTestSuite();
61         suite.addTest(new MethodTest2("testMethodAdd"));
62         return suite;
63     }
64     
65     protected void setUp() throws Exception JavaDoc {
66         super.setUp();
67         testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest2.java");
68     }
69
70     /**
71      * Changes the modifiers on method. Removes public modifier, sets static
72      * and private modifier.
73      */

74     public void testMethodAdd() throws IOException JavaDoc {
75         FileObject fo = FileUtil.toFileObject(testFile);
76         JavaSource js = JavaSource.forFileObject(fo);
77         js.runModificationTask(new CancellableTask<WorkingCopy>() {
78             public void cancel() {
79             }
80             public void run(WorkingCopy wc) {
81                 
82                 CreateMethod create = new CreateMethod();
83                 SourceUtilsTestUtil2.run(wc, create);
84                 MethodTree mt = create.makeMethod();
85                 
86                 MethodImplGenerator add = new MethodImplGenerator(mt);
87                 SourceUtilsTestUtil2.run(wc, add);
88                 
89                 RenameImplGenerator rename = new RenameImplGenerator(add.method, "foo");
90                 SourceUtilsTestUtil2.run(wc, rename);
91                 
92                 SetTypeGenerator setType = new SetTypeGenerator(rename.method, wc.getElements().getTypeElement("java.lang.String"));
93                 SourceUtilsTestUtil2.run(wc, setType);
94                 
95                 SourcePositions pos[] = new SourcePositions[1];
96                 BlockTree btree = (BlockTree) wc.getTreeUtilities().parseStatement("{System.out.println();}", pos);
97                 
98                 SetBodyGenerator setBody = new SetBodyGenerator(setType.method, btree);
99                 SourceUtilsTestUtil2.run(wc, setBody);
100             }
101         }).commit();
102         
103         assertFiles("testMethodAdd.pass");
104     }
105
106     ////////////////////////////////////////////////////////////////////////////
107
/**
108      * @param args the command line arguments
109      */

110     public static void main(String JavaDoc[] args) {
111         TestRunner.run(suite());
112     }
113
114     String JavaDoc getSourcePckg() {
115         return "org/netbeans/test/codegen/";
116     }
117
118     String JavaDoc getGoldenPckg() {
119         return "org/netbeans/jmi/javamodel/codegen/MethodTest2/MethodTest2/";
120     }
121     
122     private class SetTypeGenerator extends Transformer<Void JavaDoc, Object JavaDoc> {
123         
124         public MethodTree method;
125         Element newType;
126         public SetTypeGenerator(MethodTree m, Element el) {
127             method = m;
128             this.newType = el;
129         }
130         
131         public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
132             super.visitMethod(node, p);
133             if (method.equals(node)) {
134                 MethodTree njuMethod = make.Method(
135                         node.getModifiers(),
136                         node.getName().toString(),
137                         make.Identifier(newType),
138                         node.getTypeParameters(),
139                         node.getParameters(),
140                         node.getThrows(),
141                         node.getBody(),
142                         (ExpressionTree) node.getDefaultValue()
143                         );
144                 changes.rewrite(node, njuMethod);
145                 method = njuMethod;
146             }
147             return null;
148         }
149     }
150     
151     private class SetBodyGenerator extends Transformer<Void JavaDoc, Object JavaDoc> {
152         
153         public MethodTree method;
154         BlockTree newType;
155         public SetBodyGenerator(MethodTree m, BlockTree tree) {
156             method = m;
157             this.newType = newType;
158             newType = tree;
159         }
160         
161         public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
162             super.visitMethod(node, p);
163             if (method.equals(node)) {
164                 MethodTree njuMethod = make.Method(
165                         node.getModifiers(),
166                         node.getName().toString(),
167                         (ExpressionTree) node.getReturnType(),
168                         node.getTypeParameters(),
169                         node.getParameters(),
170                         node.getThrows(),
171                         newType,
172                         (ExpressionTree) node.getDefaultValue()
173                         );
174                 method = njuMethod;
175                 changes.rewrite(node, njuMethod);
176             }
177             return null;
178         }
179     }
180     private class RenameImplGenerator extends Transformer<Void JavaDoc, Object JavaDoc> {
181         
182         public MethodTree method;
183         String JavaDoc newName;
184         public RenameImplGenerator(MethodTree m, String JavaDoc newName) {
185             method = m;
186             this.newName = newName;
187         }
188         
189         public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
190             super.visitMethod(node, p);
191             if (method.equals(node)) {
192                 MethodTree njuMethod = make.Method(
193                         node.getModifiers(),
194                         newName,
195                         (ExpressionTree) node.getReturnType(),
196                         node.getTypeParameters(),
197                         node.getParameters(),
198                         node.getThrows(),
199                         node.getBody(),
200                         (ExpressionTree) node.getDefaultValue()
201                         );
202                 changes.rewrite(node, njuMethod);
203                 method = njuMethod;
204             }
205             return null;
206         }
207     }
208     private class MethodImplGenerator extends Transformer<Void JavaDoc, Object JavaDoc> {
209         
210         public MethodTree method;
211         public MethodImplGenerator(MethodTree m) {
212             method = m;
213             
214         }
215         
216         public Void JavaDoc visitClass(ClassTree node, Object JavaDoc p) {
217             super.visitClass(node, p);
218             TypeElement te = (TypeElement)model.getElement(node);
219             if (te != null) {
220                 List JavaDoc<Tree> members = new ArrayList JavaDoc<Tree>();
221                 for(Tree m : node.getMembers())
222                     members.add(m);
223                 members.add(method);
224                 ClassTree decl = make.Class(node.getModifiers(), node.getSimpleName(), node.getTypeParameters(), node.getExtendsClause(), (List JavaDoc<ExpressionTree>)node.getImplementsClause(), members);
225                 model.setElement(decl, te);
226                 model.setType(decl, model.getType(node));
227                 changes.rewrite(node, decl);
228             }
229             return null;
230         }
231     }
232
233     private class CreateMethod extends Transformer<Void JavaDoc, Object JavaDoc> {
234         public MethodTree makeMethod() {
235             Set JavaDoc<Modifier> emptyModifs = Collections.emptySet();
236             List JavaDoc<TypeParameterTree> emptyTpt= Collections.emptyList();
237             List JavaDoc<VariableTree> emptyVt = Collections.emptyList();
238             List JavaDoc<ExpressionTree> emptyEt = Collections.emptyList();
239             return make.Method(
240                       make.Modifiers(emptyModifs),
241                       (CharSequence JavaDoc)"",
242                       (ExpressionTree) null,
243                       emptyTpt,
244                       emptyVt,
245                       emptyEt,
246                       (BlockTree) null,
247                       (ExpressionTree)null);
248         }
249     }
250     
251 }
252
Popular Tags