KickJava   Java API By Example, From Geeks To Geeks.

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


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.ExpressionTree;
22 import com.sun.source.tree.IdentifierTree;
23 import com.sun.source.tree.MethodTree;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import javax.lang.model.element.Element;
28 import org.netbeans.api.java.source.JavaSource;
29 import org.netbeans.api.java.source.SourceUtilsTestUtil2;
30 import org.netbeans.api.java.source.WorkingCopy;
31 import org.netbeans.api.java.source.transform.Transformer;
32 import org.netbeans.junit.NbTestSuite;
33 import junit.textui.TestRunner;
34 import org.netbeans.api.java.source.CancellableTask;
35 import org.netbeans.api.java.source.ClasspathInfo;
36 import org.netbeans.api.java.source.CompilationInfo;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39
40 /**
41  * Tests the method generator.
42  *
43  * @author Jan Becicka
44  */

45 public class MethodTest3 extends GeneratorTest {
46     
47     /** Need to be defined because of JUnit */
48     public MethodTest3(String JavaDoc name) {
49         super(name);
50     }
51     
52     public static NbTestSuite suite() {
53         NbTestSuite suite = new NbTestSuite();
54         suite.addTest(new MethodTest3("testMethodThrows"));
55         return suite;
56     }
57     
58     protected void setUp() throws Exception JavaDoc {
59         super.setUp();
60         testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest3.java");
61         i = ClasspathInfo.create(testFile);
62     }
63     
64     ClasspathInfo i;
65
66     /**
67      * Changes the modifiers on method. Removes public modifier, sets static
68      * and private modifier.
69      */

70     public void testMethodThrows() throws IOException JavaDoc {
71         FileObject fo = FileUtil.toFileObject(testFile);
72         JavaSource js = JavaSource.forFileObject(fo);
73         js.runModificationTask(new CancellableTask<WorkingCopy>() {
74             public void cancel() {
75             }
76             public void run(WorkingCopy wc) {
77                 RemoveException remove = new RemoveException(0);
78                 SourceUtilsTestUtil2.run(wc, remove);
79                 
80                 AddException add = new AddException(wc, "java.lang.IllegalMonitorStateException");
81                 SourceUtilsTestUtil2.run(wc, add);
82             }
83         }).commit();
84         
85         assertFiles("testMethodThrows.pass");
86     }
87
88     ////////////////////////////////////////////////////////////////////////////
89
/**
90      * @param args the command line arguments
91      */

92     public static void main(String JavaDoc[] args) {
93         TestRunner.run(suite());
94     }
95
96     String JavaDoc getSourcePckg() {
97         return "org/netbeans/test/codegen/";
98     }
99
100     String JavaDoc getGoldenPckg() {
101         return "org/netbeans/jmi/javamodel/codegen/MethodTest3/MethodTest3/";
102     }
103     
104     private class RemoveException extends Transformer<Void JavaDoc, Object JavaDoc> {
105         
106         int index;
107         public RemoveException(int index) {
108             this.index = index;
109         }
110         
111         public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
112             super.visitMethod(node, p);
113             Element al = model.getElement(node);
114             if ("foo()".equals(al.toString())) {
115                 List JavaDoc<ExpressionTree> l = new ArrayList JavaDoc<ExpressionTree>();
116                 int i=0;
117                 for (ExpressionTree n: node.getThrows()) {
118                     if (i!=index) {
119                         l.add(n);
120                     }
121                 }
122                 MethodTree njuMethod = make.Method(
123                         node.getModifiers(),
124                         node.getName(),
125                         (ExpressionTree) node.getReturnType(),
126                         node.getTypeParameters(),
127                         node.getParameters(),
128                         l,
129                         node.getBody(),
130                         (ExpressionTree) node.getDefaultValue()
131                         );
132                 model.setElement(njuMethod, al);
133                 changes.rewrite(node, njuMethod);
134             }
135             return null;
136         }
137     }
138     private class AddException extends Transformer<Void JavaDoc, Object JavaDoc> {
139
140         private CompilationInfo info;
141         ExpressionTree tr;
142         String JavaDoc ex;
143         public AddException(CompilationInfo info, String JavaDoc ex) {
144             this.info = info;
145             this.ex = ex;
146         }
147         
148         public Void JavaDoc visitMethod(MethodTree node, Object JavaDoc p) {
149             super.visitMethod(node, p);
150             Element al = model.getElement(node);
151             if ("foo()".equals(al.toString())) {
152                 tr = make.Identifier(ex);
153                 List JavaDoc<ExpressionTree> l = new ArrayList JavaDoc<ExpressionTree>();
154                 l.addAll(node.getThrows());
155                 l.add(tr);
156                 MethodTree njuMethod = make.Method(
157                         node.getModifiers(),
158                         node.getName(),
159                         (ExpressionTree) node.getReturnType(),
160                         node.getTypeParameters(),
161                         node.getParameters(),
162                         l,
163                         node.getBody(),
164                         (ExpressionTree) node.getDefaultValue()
165                         );
166                 Element el = info.getElements().getTypeElement(((IdentifierTree) tr).getName().toString());
167                 
168                 model.setElement(tr, el);
169                 model.setType(tr, el.asType());
170                 changes.rewrite(node, njuMethod);
171             }
172             return null;
173         }
174     }
175     
176 }
177
Popular Tags