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:
042: package org.netbeans.modules.refactoring.java;
043:
044: import com.sun.source.tree.BlockTree;
045: import com.sun.source.tree.MethodTree;
046: import com.sun.source.tree.StatementTree;
047: import java.util.List;
048: import org.netbeans.api.java.source.CompilationController;
049: import org.netbeans.api.java.source.JavaSource;
050: import org.netbeans.modules.refactoring.api.RenameRefactoring;
051: import org.openide.filesystems.FileObject;
052: import org.openide.util.lookup.Lookups;
053: import com.sun.source.tree.Tree;
054: import com.sun.source.tree.VariableTree;
055: import com.sun.source.util.TreePath;
056: import javax.lang.model.element.Element;
057: import javax.lang.model.element.ElementKind;
058: import org.netbeans.api.java.source.TreePathHandle;
059:
060: /**
061: *
062: * @author Jiri Prox Jiri.Prox@Sun.COM
063: */
064: public class RenameClassTest extends RefactoringTestCase {
065:
066: /** Creates a new instance of RenameClassTest */
067: public RenameClassTest(String name) {
068: super (name);
069:
070: }
071:
072: public void testRenameClass() throws Exception {
073: FileObject test = getFileInProject("default",
074: "src/defaultpkg/Foo.java");
075: final RenameRefactoring renameRefactoring = new RenameRefactoring(
076: Lookups.singleton(test));
077: perform(renameRefactoring, new ParameterSetter() {
078: public void setParameters() {
079: renameRefactoring.setNewName("newName");
080: }
081: });
082: }
083:
084: public void testRenameEnum() throws Exception {
085: FileObject test = getFileInProject("default",
086: "src/defaultpkg/RenameEnum.java");
087: final RenameRefactoring renameRefactoring = new RenameRefactoring(
088: Lookups.singleton(test));
089: perform(renameRefactoring, new ParameterSetter() {
090: public void setParameters() {
091: renameRefactoring.setNewName("NewEnumName");
092: }
093: });
094: }
095:
096: public void testRenameAnnotation() throws Exception {
097: FileObject test = getFileInProject("default",
098: "src/defaultpkg/RenameAnnot.java");
099: final RenameRefactoring renameRefactoring = new RenameRefactoring(
100: Lookups.singleton(test));
101: perform(renameRefactoring, new ParameterSetter() {
102: public void setParameters() {
103: renameRefactoring.setNewName("NewAnnotName");
104: }
105: });
106: }
107:
108: public void testRenamePackage() throws Exception {
109: FileObject test = getFileInProject("default", "src/renamepkg");
110: final RenameRefactoring renameRefactoring = new RenameRefactoring(
111: Lookups.singleton(test));
112: perform(renameRefactoring, new ParameterSetter() {
113: public void setParameters() {
114: renameRefactoring.setNewName("newpkgname");
115: }
116: });
117: }
118:
119: public void testRenamePackage2() throws Exception {
120: FileObject test = getFileInProject("default", "src/renamepkg2");
121: final RenameRefactoring renameRefactoring = new RenameRefactoring(
122: Lookups.singleton(test));
123: perform(renameRefactoring, new ParameterSetter() {
124: public void setParameters() {
125: renameRefactoring.setNewName("newpkgname2");
126: }
127: });
128:
129: }
130:
131: private class ParamSelector implements
132: TreePathResolver.TreePathHandleSelector {
133:
134: public TreePathHandle select(
135: CompilationController compilationController) {
136: TreePath cuPath = new TreePath(compilationController
137: .getCompilationUnit());
138: List<? extends Tree> typeDecls = compilationController
139: .getCompilationUnit().getTypeDecls();
140: for (Tree t : typeDecls) {
141: TreePath p = new TreePath(cuPath, t);
142: Element e = compilationController.getTrees()
143: .getElement(p);
144: List<? extends Element> elems = e.getEnclosedElements();
145: for (Element element : elems) {
146: if (element.getKind() == ElementKind.METHOD) {
147: Tree tt = compilationController.getTrees()
148: .getTree(element);
149: MethodTree mt = (MethodTree) tt;
150: List<? extends VariableTree> vars = mt
151: .getParameters();
152: TreePath path = TreePath.getPath(cuPath, vars
153: .get(0));
154: return TreePathHandle.create(path,
155: compilationController);
156: }
157: }
158: }
159: return null;
160: }
161: }
162:
163: public void testRenameParam() throws Exception {
164: FileObject test = getFileInProject("default",
165: "src/defaultpkg/RenameParam.java");
166: JavaSource js = JavaSource.forFileObject(test);
167: TreePathResolver res = new TreePathResolver(new ParamSelector());
168: js.runUserActionTask(res, true);
169: final RenameRefactoring renameRefactoring = new RenameRefactoring(
170: Lookups.singleton(res.tph));
171: renameRefactoring.getContext().add(res.info);
172: perform(renameRefactoring, new ParameterSetter() {
173: public void setParameters() {
174: renameRefactoring.setNewName("newParamName");
175: }
176: });
177: }
178:
179: private class FieldSelector implements
180: TreePathResolver.TreePathHandleSelector {
181:
182: public TreePathHandle select(
183: CompilationController compilationController) {
184: TreePath cuPath = new TreePath(compilationController
185: .getCompilationUnit());
186: List<? extends Tree> typeDecls = compilationController
187: .getCompilationUnit().getTypeDecls();
188: for (Tree t : typeDecls) {
189: TreePath p = new TreePath(cuPath, t);
190: Element e = compilationController.getTrees()
191: .getElement(p);
192: List<? extends Element> elems = e.getEnclosedElements();
193: for (Element element : elems) {
194: if (element.getKind() == ElementKind.FIELD) {
195: Tree tt = compilationController.getTrees()
196: .getTree(element);
197: TreePath path = TreePath.getPath(cuPath, tt);
198: return TreePathHandle.create(path,
199: compilationController);
200: }
201: }
202: }
203: return null;
204: }
205:
206: }
207:
208: public void testRenameField() throws Exception {
209: FileObject test = getFileInProject("default",
210: "src/defaultpkg/RenameField.java");
211: JavaSource js = JavaSource.forFileObject(test);
212: TreePathResolver res = new TreePathResolver(new FieldSelector());
213: js.runUserActionTask(res, true);
214: final RenameRefactoring renameRefactoring = new RenameRefactoring(
215: Lookups.singleton(res.tph));
216: renameRefactoring.getContext().add(res.info);
217: perform(renameRefactoring, new ParameterSetter() {
218: public void setParameters() {
219: renameRefactoring.setNewName("newFieldName");
220: }
221: });
222: }
223:
224: private class VariableSelector implements
225: TreePathResolver.TreePathHandleSelector {
226:
227: public TreePathHandle select(
228: CompilationController compilationController) {
229: TreePath cuPath = new TreePath(compilationController
230: .getCompilationUnit());
231: List<? extends Tree> typeDecls = compilationController
232: .getCompilationUnit().getTypeDecls();
233: Tree t = typeDecls.get(0);
234: TreePath p = new TreePath(cuPath, t);
235: Element e = compilationController.getTrees().getElement(p);
236: List<? extends Element> elems = e.getEnclosedElements();
237:
238: for (Element element : elems) {
239: if (element.getKind() == ElementKind.METHOD) {
240: Tree tt = compilationController.getTrees().getTree(
241: element);
242: MethodTree mt = (MethodTree) tt;
243: BlockTree bt = mt.getBody();
244: List<? extends StatementTree> sts = bt
245: .getStatements();
246: StatementTree st = sts.get(0);
247: TreePath path = TreePath.getPath(cuPath, st);
248: return TreePathHandle.create(path,
249: compilationController);
250: }
251: }
252: return null;
253: }
254:
255: }
256:
257: public void testRenameLocalVar() throws Exception {
258: FileObject test = getFileInProject("default",
259: "src/defaultpkg/RenameLocal.java");
260: JavaSource js = JavaSource.forFileObject(test);
261: TreePathResolver res = new TreePathResolver(
262: new VariableSelector());
263: js.runUserActionTask(res, true);
264: final RenameRefactoring renameRefactoring = new RenameRefactoring(
265: Lookups.singleton(res.tph));
266: renameRefactoring.getContext().add(res.info);
267: perform(renameRefactoring, new ParameterSetter() {
268: public void setParameters() {
269: renameRefactoring.setNewName("newVarName");
270: }
271: });
272: }
273:
274: }
|