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.modules.refactoring.java;
042:
043: import com.sun.source.tree.ClassTree;
044: import com.sun.source.tree.Tree;
045: import com.sun.source.util.TreePath;
046: import java.io.IOException;
047: import java.util.List;
048: import javax.lang.model.element.Element;
049: import javax.lang.model.element.ElementKind;
050: import org.netbeans.api.java.source.CompilationController;
051: import org.netbeans.api.java.source.ElementHandle;
052: import org.netbeans.api.java.source.JavaSource;
053: import org.netbeans.api.java.source.TreePathHandle;
054: import org.netbeans.modules.refactoring.java.api.MemberInfo;
055: import org.netbeans.modules.refactoring.java.api.PullUpRefactoring;
056: import org.openide.filesystems.FileObject;
057:
058: /**
059: *
060: * @author Jiri Prox
061: */
062: public class PullUpTest extends RefactoringTestCase {
063:
064: public PullUpTest(String name) {
065: super (name);
066: }
067:
068: public void testPullUpField() throws Exception {
069: FileObject srcFO = getFileInProject("default",
070: "src/pulluppkg/PullUpField.java");
071: JavaSource jsSrc = JavaSource.forFileObject(srcFO);
072: FileObject dest = getFileInProject("default",
073: "src/pulluppkg/PullUpFieldSuper.java");
074: JavaSource jsDest = JavaSource.forFileObject(dest);
075: final TreePathResolver fieldSelector = new TreePathResolver(
076: new MemberSelector(ElementKind.FIELD, 1));
077: final TreePathResolver srcClassSelector = new TreePathResolver(
078: new TopClassSelector(0));
079: final TreePathResolver destClassSelector = new TreePathResolver(
080: new TopClassSelector(0));
081: jsSrc.runUserActionTask(fieldSelector, true);
082: jsSrc.runUserActionTask(srcClassSelector, true);
083: jsDest.runUserActionTask(destClassSelector, true);
084: final PullUpRefactoring pullUp = new PullUpRefactoring(
085: srcClassSelector.tph);
086: perform(pullUp, new ParameterSetter() {
087:
088: public void setParameters() {
089: MemberInfo[] mi = new MemberInfo[] { MemberInfo.create(
090: fieldSelector.tph
091: .resolveElement(fieldSelector.info),
092: fieldSelector.info) };
093: pullUp.setMembers(mi);
094: ElementHandle el = ElementHandle
095: .create(destClassSelector.tph
096: .resolveElement(destClassSelector.info));
097: pullUp.setTargetType(el);
098: }
099: });
100: }
101:
102: public void testPullUpMethod() throws Exception {
103: FileObject test = getFileInProject("default",
104: "src/pulluppkg/PullUpBaseClass.java");
105: JavaSource js = JavaSource.forFileObject(test);
106: FileObject dest = getFileInProject("default",
107: "src/pulluppkg/PullUpSuperClass.java");
108: JavaSource jsDest = JavaSource.forFileObject(dest);
109: final TreePathResolver pulledElementSelector = new TreePathResolver(
110: new MemberSelector(ElementKind.METHOD, 1));
111: final TreePathResolver srcClassSelector = new TreePathResolver(
112: new TopClassSelector(0));
113: final TreePathResolver destClassSelector = new TreePathResolver(
114: new TopClassSelector(0));
115: performPullUp(test, srcClassSelector, jsDest,
116: destClassSelector, new boolean[] { false },
117: pulledElementSelector);
118: }
119:
120: public void testPullUpClass() throws Exception {
121: FileObject test = getFileInProject("default",
122: "src/pulluppkg/PullUpInnerClass.java");
123: JavaSource js = JavaSource.forFileObject(test);
124: FileObject dest = getFileInProject("default",
125: "src/pulluppkg/PullUpInnerClassSuper.java");
126: JavaSource jsDest = JavaSource.forFileObject(dest);
127: final TreePathResolver pulledElementSelector = new TreePathResolver(
128: new MemberSelector(ElementKind.CLASS, 1));
129: final TreePathResolver srcClassSelector = new TreePathResolver(
130: new TopClassSelector(0));
131: final TreePathResolver destClassSelector = new TreePathResolver(
132: new TopClassSelector(0));
133: performPullUp(test, srcClassSelector, jsDest,
134: destClassSelector, new boolean[] { false },
135: pulledElementSelector);
136: }
137:
138: public void testPullUp2Iface() throws Exception {
139: FileObject test = getFileInProject("default",
140: "src/pulluppkg/PullUp2Iface.java");
141: JavaSource js = JavaSource.forFileObject(test);
142: FileObject dest = getFileInProject("default",
143: "src/pulluppkg/PullUp2IfaceSuper.java");
144: JavaSource jsDest = JavaSource.forFileObject(dest);
145: final TreePathResolver pulledElementSelector = new TreePathResolver(
146: new MemberSelector(ElementKind.METHOD, 1));
147: final TreePathResolver srcClassSelector = new TreePathResolver(
148: new TopClassSelector(0));
149: final TreePathResolver destClassSelector = new TreePathResolver(
150: new TopClassSelector(0));
151: performPullUp(test, srcClassSelector, jsDest,
152: destClassSelector, new boolean[] { true },
153: pulledElementSelector);
154: }
155:
156: public void testPullUpMakeAbs() throws Exception {
157: FileObject test = getFileInProject("default",
158: "src/pulluppkg/PullUpAbs.java");
159: JavaSource js = JavaSource.forFileObject(test);
160: FileObject dest = getFileInProject("default",
161: "src/pulluppkg/PullUpAbsSuper.java");
162: JavaSource jsDest = JavaSource.forFileObject(dest);
163: final TreePathResolver pulledElementSelector = new TreePathResolver(
164: new MemberSelector(ElementKind.METHOD, 1));
165: final TreePathResolver srcClassSelector = new TreePathResolver(
166: new TopClassSelector(0));
167: final TreePathResolver destClassSelector = new TreePathResolver(
168: new TopClassSelector(0));
169: performPullUp(test, srcClassSelector, jsDest,
170: destClassSelector, new boolean[] { true },
171: pulledElementSelector);
172: }
173:
174: public void testPullUpAbsMethod() throws Exception {
175: FileObject test = getFileInProject("default",
176: "src/pulluppkg/PullUpAbsMethod.java");
177: JavaSource js = JavaSource.forFileObject(test);
178: FileObject dest = getFileInProject("default",
179: "src/pulluppkg/PullUpAbsMethodSuper.java");
180: JavaSource jsDest = JavaSource.forFileObject(dest);
181: final TreePathResolver pulledElementSelector = new TreePathResolver(
182: new MemberSelector(ElementKind.METHOD, 1));
183: final TreePathResolver srcClassSelector = new TreePathResolver(
184: new TopClassSelector(0));
185: final TreePathResolver destClassSelector = new TreePathResolver(
186: new TopClassSelector(0));
187: performPullUp(test, srcClassSelector, jsDest,
188: destClassSelector, new boolean[] { false },
189: pulledElementSelector);
190: }
191:
192: public void testPullUpAbsMethod2Iface() throws Exception {
193: FileObject test = getFileInProject("default",
194: "src/pulluppkg/PullUpAbsMethod2Iface.java");
195: JavaSource js = JavaSource.forFileObject(test);
196: FileObject dest = getFileInProject("default",
197: "src/pulluppkg/PullUpAbsMethod2IfaceSuper.java");
198: JavaSource jsDest = JavaSource.forFileObject(dest);
199: final TreePathResolver pulledElementSelector = new TreePathResolver(
200: new MemberSelector(ElementKind.METHOD, 1));
201: final TreePathResolver srcClassSelector = new TreePathResolver(
202: new TopClassSelector(0));
203: final TreePathResolver destClassSelector = new TreePathResolver(
204: new TopClassSelector(0));
205: performPullUp(test, srcClassSelector, jsDest,
206: destClassSelector, new boolean[] { false },
207: pulledElementSelector);
208: }
209:
210: // public void testPullUpInterface() throws Exception {
211: // FileObject test = getFileInProject("default", "src/pulluppkg/PullUpIface.java");
212: // JavaSource js = JavaSource.forFileObject(test);
213: // FileObject dest = getFileInProject("default", "src/pulluppkg/PullUpIfaceSuper.java");
214: // JavaSource jsDest = JavaSource.forFileObject(dest);
215: // final TreePathResolver pulledElementSelector = new TreePathResolver(new ImplementsSelector(0, 0));
216: // final TreePathResolver srcClassSelector = new TreePathResolver(new TopClassSelector(0));
217: // final TreePathResolver destClassSelector = new TreePathResolver(new TopClassSelector(0));
218: // performPullUp(test, srcClassSelector, jsDest, destClassSelector, new boolean[]{false}, MemberInfo.Group.IMPLEMENTS,pulledElementSelector);
219: // }
220:
221: public void testPullUpTwoClassesUp() throws Exception {
222: FileObject test = getFileInProject("default",
223: "src/pulluppkg/PullUpTwoClasses.java");
224: JavaSource js = JavaSource.forFileObject(test);
225: FileObject dest = getFileInProject("default",
226: "src/pulluppkg/PullUpTwoClassesSuperSuper.java");
227: JavaSource jsDest = JavaSource.forFileObject(dest);
228: final TreePathResolver pulledElementSelector = new TreePathResolver(
229: new MemberSelector(ElementKind.METHOD, 1));
230: final TreePathResolver pulledElementSelector2 = new TreePathResolver(
231: new MemberSelector(ElementKind.FIELD, 1));
232: final TreePathResolver srcClassSelector = new TreePathResolver(
233: new TopClassSelector(0));
234: final TreePathResolver destClassSelector = new TreePathResolver(
235: new TopClassSelector(0));
236: performPullUp(test, srcClassSelector, jsDest,
237: destClassSelector, new boolean[] { false, false },
238: pulledElementSelector, pulledElementSelector2);
239: }
240:
241: public void testPullUpExisting() throws Exception {
242: FileObject test = getFileInProject("default",
243: "src/pulluppkg/PullUpExisting.java");
244: JavaSource js = JavaSource.forFileObject(test);
245: FileObject dest = getFileInProject("default",
246: "src/pulluppkg/PullUpExistingSuper.java");
247: JavaSource jsDest = JavaSource.forFileObject(dest);
248: final TreePathResolver pulledElementSelector = new TreePathResolver(
249: new MemberSelector(ElementKind.METHOD, 1));
250: final TreePathResolver srcClassSelector = new TreePathResolver(
251: new TopClassSelector(0));
252: final TreePathResolver destClassSelector = new TreePathResolver(
253: new TopClassSelector(0));
254: performPullUp(test, srcClassSelector, jsDest,
255: destClassSelector, new boolean[] { false },
256: pulledElementSelector);
257: }
258:
259: public void testPullUpLocalyReferenced() throws Exception {
260: FileObject test = getFileInProject("default",
261: "src/pulluppkg/PullUpReferenced.java");
262: JavaSource js = JavaSource.forFileObject(test);
263: FileObject dest = getFileInProject("default",
264: "src/pulluppkg/PullUpReferencedSuper.java");
265: JavaSource jsDest = JavaSource.forFileObject(dest);
266: final TreePathResolver pulledElementSelector = new TreePathResolver(
267: new MemberSelector(ElementKind.METHOD, 1));
268: final TreePathResolver srcClassSelector = new TreePathResolver(
269: new TopClassSelector(0));
270: final TreePathResolver destClassSelector = new TreePathResolver(
271: new TopClassSelector(0));
272: performPullUp(test, srcClassSelector, jsDest,
273: destClassSelector, new boolean[] { false },
274: pulledElementSelector);
275: }
276:
277: private void performPullUp(FileObject sourceFO,
278: final TreePathResolver srcClassSelector, JavaSource dest,
279: final TreePathResolver destClassSelector,
280: final boolean[] makeAbstract,
281: final TreePathResolver... pulledElementSelectors)
282: throws IOException, IllegalArgumentException {
283: JavaSource source = JavaSource.forFileObject(sourceFO);
284: for (int i = 0; i < pulledElementSelectors.length; i++) {
285: source.runUserActionTask(pulledElementSelectors[i], true);
286: }
287: source.runUserActionTask(srcClassSelector, true);
288: dest.runUserActionTask(destClassSelector, true);
289: final PullUpRefactoring pullUp = new PullUpRefactoring(
290: srcClassSelector.tph);
291: perform(pullUp, new ParameterSetter() {
292:
293: @SuppressWarnings("unchecked")
294: public void setParameters() {
295: MemberInfo[] mi = new MemberInfo[pulledElementSelectors.length];
296:
297: for (int i = 0; i < pulledElementSelectors.length; i++) {
298: mi[i] = MemberInfo
299: .create(
300: pulledElementSelectors[i].tph
301: .resolveElement(pulledElementSelectors[i].info),
302: pulledElementSelectors[i].info);
303: mi[i].setMakeAbstract(makeAbstract[i]);
304:
305: }
306: pullUp.setMembers(mi);
307: ElementHandle el = ElementHandle
308: .create(destClassSelector.tph
309: .resolveElement(destClassSelector.info));
310: pullUp.setTargetType(el);
311: }
312: });
313: }
314:
315: class MemberSelector implements
316: TreePathResolver.TreePathHandleSelector {
317:
318: private ElementKind kind;
319: private int number;
320:
321: public MemberSelector(ElementKind kind, int number) {
322: this .kind = kind;
323: this .number = number;
324: }
325:
326: public MemberSelector(int number) {
327: this (null, number);
328: this .number = number;
329: }
330:
331: public TreePathHandle select(
332: CompilationController compilationController) {
333: TreePath cuPath = new TreePath(compilationController
334: .getCompilationUnit());
335: List<? extends Tree> typeDecls = compilationController
336: .getCompilationUnit().getTypeDecls();
337: Tree t = typeDecls.get(0);
338: TreePath p = new TreePath(cuPath, t);
339: Element e = compilationController.getTrees().getElement(p);
340: List<? extends Element> elems = e.getEnclosedElements();
341: for (Element element : elems) {
342: int i = 0;
343: if (element.getKind() == kind || kind == null) {
344: i++;
345: if (i == number) {
346: return TreePathHandle.create(element,
347: compilationController);
348: }
349: }
350: }
351: return null;
352: }
353: }
354:
355: class ImplementsSelector implements
356: TreePathResolver.TreePathHandleSelector {
357:
358: private int classNum;
359: private int ifaceNum;
360:
361: public ImplementsSelector(int classNum, int ifaceNum) {
362: this .classNum = classNum;
363: this .ifaceNum = ifaceNum;
364: }
365:
366: public TreePathHandle select(
367: CompilationController compilationController) {
368: TreePath cuPath = new TreePath(compilationController
369: .getCompilationUnit());
370: List<? extends Tree> typeDecls = compilationController
371: .getCompilationUnit().getTypeDecls();
372: ClassTree cTree = (ClassTree) typeDecls.get(classNum);
373: Tree interfaceTree = cTree.getImplementsClause().get(
374: ifaceNum);
375: TreePathHandle pathHandle = TreePathHandle.create(TreePath
376: .getPath(cuPath, interfaceTree),
377: compilationController);
378: return pathHandle;
379: }
380: }
381: }
|