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-2007 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.plugins;
042:
043: import org.netbeans.modules.refactoring.java.spi.JavaRefactoringPlugin;
044: import java.io.IOException;
045: import java.util.EnumSet;
046: import java.util.HashSet;
047: import java.util.Set;
048: import javax.lang.model.element.Element;
049: import javax.lang.model.element.ElementKind;
050: import javax.lang.model.element.TypeElement;
051: import org.netbeans.api.java.source.*;
052: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
053: import org.netbeans.modules.refactoring.api.Problem;
054: import org.netbeans.modules.refactoring.api.ProgressEvent;
055: import org.netbeans.modules.refactoring.java.RetoucheUtils;
056: import org.netbeans.modules.refactoring.java.api.PushDownRefactoring;
057: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
058: import org.openide.filesystems.FileObject;
059: import org.openide.util.NbBundle;
060:
061: /**
062: * Plugin that implements the core functionality of Push Down refactoring.
063: *
064: * @author Pavel Flaska
065: * @author Jan Becicka
066: */
067: public final class PushDownRefactoringPlugin extends
068: JavaRefactoringPlugin {
069:
070: /** Reference to the parent refactoring instance */
071: private final PushDownRefactoring refactoring;
072: private TreePathHandle treePathHandle;
073:
074: /** Creates a new instance of PushDownRefactoringPlugin */
075: public PushDownRefactoringPlugin(PushDownRefactoring refactoring) {
076: this .refactoring = refactoring;
077: treePathHandle = refactoring.getSourceType();
078: }
079:
080: protected JavaSource getJavaSource(Phase p) {
081: //TODO: wrong classpath
082: switch (p) {
083: default:
084: return JavaSource.forFileObject(treePathHandle
085: .getFileObject());
086: }
087: }
088:
089: @Override
090: protected Problem preCheck(CompilationController cc)
091: throws IOException {
092: fireProgressListenerStart(AbstractRefactoring.PRE_CHECK, 4);
093: try {
094: cc.toPhase(JavaSource.Phase.RESOLVED);
095: Problem precheckProblem = isElementAvail(treePathHandle, cc);
096: if (precheckProblem != null) {
097: // fatal error -> don't continue with further checks
098: return precheckProblem;
099: }
100: if (!RetoucheUtils.isElementInOpenProject(treePathHandle
101: .getFileObject())) {
102: return new Problem(true, NbBundle.getMessage(
103: PushDownRefactoringPlugin.class,
104: "ERR_ProjectNotOpened"));
105: }
106:
107: // increase progress (step 1)
108: fireProgressListenerStep();
109: ElementHandle<TypeElement> eh = ElementHandle
110: .create((TypeElement) treePathHandle
111: .resolveElement(cc));
112: Set<FileObject> resources = cc
113: .getClasspathInfo()
114: .getClassIndex()
115: .getResources(
116: eh,
117: EnumSet
118: .of(ClassIndex.SearchKind.IMPLEMENTORS),
119: EnumSet.of(ClassIndex.SearchScope.SOURCE));
120: if (resources.isEmpty()) {
121: return new Problem(true, NbBundle.getMessage(
122: PushDownRefactoringPlugin.class,
123: "ERR_PushDOwn_NoSubtype")); // NOI18N
124: }
125: // increase progress (step 2)
126: fireProgressListenerStep();
127: // #2 - check if there are any members to pull up
128: Element el = treePathHandle.resolveElement(cc);
129: for (Element element : el.getEnclosedElements()) {
130: if (element.getKind() != ElementKind.CONSTRUCTOR) {
131: return null;
132: }
133: }
134: precheckProblem = new Problem(true, NbBundle.getMessage(
135: PushDownRefactoringPlugin.class,
136: "ERR_PushDown_NoMembers")); // NOI18N
137: // increase progress (step 3)
138: fireProgressListenerStep();
139: return precheckProblem;
140: } finally {
141: fireProgressListenerStop();
142: }
143: }
144:
145: @Override
146: public Problem checkParameters() {
147: return null;
148: }
149:
150: @Override
151: protected Problem fastCheckParameters(CompilationController info) {
152: // #1 - check whether there are any members to pull up
153: if (refactoring.getMembers().length == 0) {
154: return new Problem(true, NbBundle.getMessage(
155: PushDownRefactoringPlugin.class,
156: "ERR_PushDown_NoMembersSelected")); // NOI18N
157: }
158: return null;
159: }
160:
161: private Set<FileObject> getRelevantFiles(TreePathHandle handle) {
162: ClasspathInfo cpInfo = getClasspathInfo(refactoring);
163: ClassIndex idx = cpInfo.getClassIndex();
164: Set<FileObject> set = new HashSet<FileObject>();
165: set.add(RetoucheUtils.getFileObject(handle));
166: set.addAll(idx.getResources(RetoucheUtils
167: .getElementHandle(handle), EnumSet
168: .of(ClassIndex.SearchKind.IMPLEMENTORS), EnumSet
169: .of(ClassIndex.SearchScope.SOURCE)));
170: return set;
171: }
172:
173: public Problem prepare(RefactoringElementsBag refactoringElements) {
174: Set<FileObject> a = getRelevantFiles(treePathHandle);
175: fireProgressListenerStart(ProgressEvent.START, a.size());
176: PushDownTransformer pdt = new PushDownTransformer(refactoring
177: .getMembers());
178: TransformTask task = new TransformTask(pdt, treePathHandle);
179: createAndAddElements(a, task, refactoringElements, refactoring);
180: fireProgressListenerStop();
181: return pdt.getProblem();
182: }
183:
184: protected FileObject getFileObject() {
185: return treePathHandle.getFileObject();
186: }
187: }
|