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.cnd.refactoring.plugins;
042:
043: import java.util.Collection;
044: import java.util.EnumSet;
045: import java.util.HashSet;
046: import java.util.LinkedHashSet;
047: import java.util.Set;
048: import org.netbeans.modules.cnd.api.model.CsmClass;
049: import org.netbeans.modules.cnd.api.model.CsmFile;
050: import org.netbeans.modules.cnd.api.model.CsmFunctionDefinition;
051: import org.netbeans.modules.cnd.api.model.CsmMethod;
052: import org.netbeans.modules.cnd.api.model.CsmObject;
053: import org.netbeans.modules.cnd.api.model.CsmUID;
054: import org.netbeans.modules.cnd.api.model.CsmValidable;
055: import org.netbeans.modules.cnd.api.model.services.CsmVirtualInfoQuery;
056: import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
057: import org.netbeans.modules.cnd.api.model.xref.CsmIncludeHierarchyResolver;
058: import org.netbeans.modules.cnd.api.model.xref.CsmReference;
059: import org.netbeans.modules.cnd.api.model.xref.CsmReferenceKind;
060: import org.netbeans.modules.cnd.api.model.xref.CsmReferenceRepository;
061: import org.netbeans.modules.cnd.api.model.xref.CsmReferenceSupport;
062: import org.netbeans.modules.cnd.api.model.xref.CsmTypeHierarchyResolver;
063: import org.netbeans.modules.cnd.refactoring.api.WhereUsedQueryConstants;
064: import org.netbeans.modules.cnd.refactoring.elements.CsmRefactoringElementImpl;
065: import org.netbeans.modules.refactoring.api.Problem;
066: import org.netbeans.modules.refactoring.api.ProgressEvent;
067: import org.netbeans.modules.refactoring.api.WhereUsedQuery;
068: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
069: import org.openide.util.NbBundle;
070:
071: /**
072: * Actual implementation of Find Usages query search for C/C++
073: *
074: * @todo Perform index lookups to determine the set of files to be checked!
075: *
076: * @author Vladimir Voskresensky
077: */
078: public class CsmWhereUsedQueryPlugin extends CsmRefactoringPlugin {
079: private final WhereUsedQuery refactoring;
080: private final CsmObject startReferenceObject;
081:
082: /** Creates a new instance of WhereUsedQuery */
083: public CsmWhereUsedQueryPlugin(WhereUsedQuery refactoring) {
084: this .refactoring = refactoring;
085: startReferenceObject = refactoring.getRefactoringSource()
086: .lookup(CsmObject.class);
087: }
088:
089: public Problem prepare(final RefactoringElementsBag elements) {
090: CsmUID referencedObjectUID = refactoring.getRefactoringSource()
091: .lookup(CsmUID.class);
092: CsmObject referencedObject = referencedObjectUID == null ? null
093: : (CsmObject) referencedObjectUID.getObject();
094: if (referencedObject == null) {
095: return null;
096: }
097: if (isFindUsages()) {
098: if (CsmKindUtilities.isFile(referencedObject)) {
099: fireProgressListenerStart(ProgressEvent.START, 1);
100: processIncludeQuery((CsmFile) referencedObject,
101: elements);
102: fireProgressListenerStep();
103: fireProgressListenerStop();
104: } else {
105: Collection<CsmObject> referencedObjects = getObjectsForFindUsages(referencedObject);
106: CsmFile startFile = getCsmFile(startReferenceObject);
107: Set<CsmFile> files = new HashSet<CsmFile>();
108: for (CsmObject csmObject : referencedObjects) {
109: files.addAll(getRelevantFiles(startFile, csmObject,
110: refactoring));
111: }
112: fireProgressListenerStart(ProgressEvent.START, files
113: .size());
114: processObjectUsagesQuery(referencedObjects, elements,
115: files);
116: fireProgressListenerStop();
117: }
118: } else if (isFindDirectSubclassesOnly() || isFindSubclasses()) {
119: assert CsmKindUtilities.isClass(referencedObject) : "must be class";
120: fireProgressListenerStart(ProgressEvent.START, 1);
121: processSubclassesQuery((CsmClass) referencedObject,
122: elements);
123: fireProgressListenerStep();
124: fireProgressListenerStop();
125: } else if (isFindOverridingMethods()) {
126: assert CsmKindUtilities.isMethod(referencedObject) : "must be method";
127: fireProgressListenerStart(ProgressEvent.START, 1);
128: processOverridenMethodsQuery((CsmMethod) referencedObject,
129: elements);
130: fireProgressListenerStep();
131: fireProgressListenerStop();
132: }
133: return null;
134: }
135:
136: @Override
137: public Problem checkParameters() {
138: return super .checkParameters();
139: }
140:
141: @Override
142: public Problem preCheck() {
143: CsmUID uid = refactoring.getRefactoringSource().lookup(
144: CsmUID.class);
145: Problem invalidContext = new Problem(true, NbBundle.getMessage(
146: CsmWhereUsedQueryPlugin.class,
147: "MSG_InvalidObjectNothingToFind")); // NOI18N;
148: if (uid == null) {
149: CsmFile startFile = getCsmFile(startReferenceObject);
150: if (startFile == null || !startFile.isValid()) {
151: return invalidContext;
152: }
153: return super .preCheck();
154: }
155: CsmObject referencedObject = (CsmObject) uid.getObject();
156: if (referencedObject == null) {
157: return invalidContext;
158: }
159: if (CsmKindUtilities.isValidable(referencedObject)) {
160: if (!((CsmValidable) referencedObject).isValid()) {
161: return invalidContext;
162: }
163: }
164: return super .preCheck();
165: }
166:
167: @Override
168: public Problem fastCheckParameters() {
169: CsmUID uid = refactoring.getRefactoringSource().lookup(
170: CsmUID.class);
171: if (uid != null
172: && CsmKindUtilities.isMethod((CsmObject) uid
173: .getObject())) {
174: return checkParametersForMethod(isFindOverridingMethods(),
175: isFindUsages());
176: } else {
177: return super .fastCheckParameters();
178: }
179: }
180:
181: // //@Override
182: // protected Problem fastCheckParameters(CompilationController info) {
183: // if (searchHandle.getKind() == ElementKind.METHOD) {
184: // return checkParametersForMethod(isFindOverridingMethods(), isFindUsages());
185: // }
186: // return null;
187: // }
188: //
189: // //@Override
190: // protected Problem checkParameters(CompilationController info) {
191: // return null;
192: // }
193:
194: private Problem checkParametersForMethod(boolean overriders,
195: boolean usages) {
196: if (!(usages || overriders)) {
197: return new Problem(true, NbBundle.getMessage(
198: CsmWhereUsedQueryPlugin.class, "MSG_NothingToFind"));
199: } else {
200: return null;
201: }
202: }
203:
204: private Collection<CsmObject> getObjectsForFindUsages(
205: CsmObject referencedObject) {
206: Collection<CsmObject> out = new LinkedHashSet<CsmObject>();
207: if (isFindUsages()) {
208: if (CsmKindUtilities.isMethod(referencedObject)) {
209: CsmMethod method = (CsmMethod) referencedObject;
210: if (CsmVirtualInfoQuery.getDefault().isVirtual(method)) {
211: out.addAll(CsmVirtualInfoQuery.getDefault()
212: .getOverridenMethods(method,
213: isSearchFromBaseClass()));
214: }
215: }
216: out.add(referencedObject);
217: }
218: return out;
219: }
220:
221: private boolean isFindSubclasses() {
222: return refactoring
223: .getBooleanValue(WhereUsedQueryConstants.FIND_SUBCLASSES);
224: }
225:
226: private boolean isFindUsages() {
227: return refactoring
228: .getBooleanValue(WhereUsedQuery.FIND_REFERENCES);
229: }
230:
231: private boolean isFindDirectSubclassesOnly() {
232: return refactoring
233: .getBooleanValue(WhereUsedQueryConstants.FIND_DIRECT_SUBCLASSES);
234: }
235:
236: private boolean isFindOverridingMethods() {
237: return refactoring
238: .getBooleanValue(WhereUsedQueryConstants.FIND_OVERRIDING_METHODS);
239: }
240:
241: private boolean isSearchFromBaseClass() {
242: return refactoring
243: .getBooleanValue(WhereUsedQueryConstants.SEARCH_FROM_BASECLASS);
244: }
245:
246: private boolean isSearchInComments() {
247: return refactoring
248: .getBooleanValue(WhereUsedQuery.SEARCH_IN_COMMENTS);
249: }
250:
251: private void processObjectUsagesQuery(
252: final Collection<CsmObject> csmObjects,
253: final RefactoringElementsBag elements,
254: final Collection<CsmFile> files) {
255: assert isFindUsages() : "must be find usages mode";
256: CsmReferenceRepository xRef = CsmReferenceRepository
257: .getDefault();
258: EnumSet<CsmReferenceKind> kinds = isFindOverridingMethods() ? CsmReferenceKind.ALL
259: : CsmReferenceKind.ANY_USAGE;
260: CsmObject[] objs = csmObjects.toArray(new CsmObject[csmObjects
261: .size()]);
262: for (CsmFile file : files) {
263: if (cancelRequest) {
264: break;
265: }
266: Collection<CsmReference> refs = xRef.getReferences(objs,
267: file, kinds);
268: for (CsmReference csmReference : refs) {
269: elements.add(refactoring, CsmRefactoringElementImpl
270: .create(csmReference, true));
271: }
272: fireProgressListenerStep();
273: }
274: }
275:
276: private void processOverridenMethodsQuery(
277: final CsmMethod csmMethod,
278: final RefactoringElementsBag elements) {
279: assert isFindOverridingMethods() : "must be search for overriden methods";
280: Collection<CsmMethod> overrides = CsmVirtualInfoQuery
281: .getDefault().getOverridenMethods(csmMethod,
282: isSearchFromBaseClass());
283: overrides.add(csmMethod);
284: for (CsmMethod method : overrides) {
285: CsmReference declRef = CsmReferenceSupport
286: .createObjectReference(method);
287: elements.add(refactoring, CsmRefactoringElementImpl.create(
288: declRef, false));
289: // find defintion of method if needed
290: if (!CsmKindUtilities.isFunctionDefinition(method)) {
291: CsmFunctionDefinition def = method.getDefinition();
292: if (def != null) {
293: CsmReference defRef = CsmReferenceSupport
294: .createObjectReference(def);
295: elements.add(refactoring, CsmRefactoringElementImpl
296: .create(defRef, false));
297: }
298: }
299: }
300: }
301:
302: private void processIncludeQuery(final CsmFile csmFile,
303: final RefactoringElementsBag elements) {
304: assert isFindUsages() : "must be find usages";
305: Collection<CsmReference> refs = CsmIncludeHierarchyResolver
306: .getDefault().getIncludes(csmFile);
307: for (CsmReference csmReference : refs) {
308: elements.add(refactoring, CsmRefactoringElementImpl.create(
309: csmReference, false));
310: }
311: }
312:
313: private void processSubclassesQuery(final CsmClass referencedClass,
314: final RefactoringElementsBag elements) {
315: assert isFindDirectSubclassesOnly() || isFindSubclasses() : "must be search of subclasses";
316: boolean directSubtypesOnly = isFindDirectSubclassesOnly();
317: Collection<CsmReference> refs = CsmTypeHierarchyResolver
318: .getDefault().getSubTypes(referencedClass,
319: directSubtypesOnly);
320: for (CsmReference csmReference : refs) {
321: elements.add(refactoring, CsmRefactoringElementImpl.create(
322: csmReference, false));
323: }
324: }
325: }
|