001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.search.dependencies;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IProgressMonitor;
015: import org.eclipse.core.runtime.SubProgressMonitor;
016: import org.eclipse.jdt.core.IClassFile;
017: import org.eclipse.jdt.core.ICompilationUnit;
018: import org.eclipse.jdt.core.IJavaElement;
019: import org.eclipse.jdt.core.IJavaProject;
020: import org.eclipse.jdt.core.IPackageFragment;
021: import org.eclipse.jdt.core.IType;
022: import org.eclipse.jdt.core.JavaCore;
023: import org.eclipse.jdt.core.search.IJavaSearchConstants;
024: import org.eclipse.jdt.core.search.IJavaSearchScope;
025: import org.eclipse.jdt.core.search.SearchEngine;
026: import org.eclipse.jdt.core.search.SearchMatch;
027: import org.eclipse.jdt.core.search.SearchParticipant;
028: import org.eclipse.jdt.core.search.SearchPattern;
029: import org.eclipse.jdt.core.search.SearchRequestor;
030: import org.eclipse.pde.core.ISourceObject;
031: import org.eclipse.pde.core.plugin.IPluginExtension;
032: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
033: import org.eclipse.pde.core.plugin.IPluginModelBase;
034: import org.eclipse.pde.core.plugin.PluginRegistry;
035: import org.eclipse.pde.internal.core.search.PluginJavaSearchUtil;
036: import org.eclipse.pde.internal.ui.PDEUIMessages;
037: import org.eclipse.search.ui.ISearchResult;
038: import org.eclipse.search.ui.text.Match;
039:
040: public class DependencyExtentOperation {
041:
042: class TypeReferenceSearchRequestor extends SearchRequestor {
043: boolean fUsed = false;
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
047: */
048: public void acceptSearchMatch(SearchMatch match)
049: throws CoreException {
050: if (match.getAccuracy() == SearchMatch.A_ACCURATE
051: && !match.isInsideDocComment()) {
052: fUsed = true;
053: }
054: }
055:
056: public boolean containMatches() {
057: return fUsed;
058: }
059: }
060:
061: class TypeDeclarationSearchRequestor extends SearchRequestor {
062:
063: private Match fMatch;
064:
065: /* (non-Javadoc)
066: * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
067: */
068: public void acceptSearchMatch(SearchMatch match)
069: throws CoreException {
070: if (!match.isInsideDocComment())
071: fMatch = new Match(match.getElement(),
072: Match.UNIT_CHARACTER, match.getOffset(), match
073: .getLength());
074: }
075:
076: public Match getMatch() {
077: return fMatch;
078: }
079: }
080:
081: private DependencyExtentSearchResult fSearchResult;
082: private String fImportID;
083: private IPluginModelBase fModel;
084: private IProject fProject;
085:
086: public DependencyExtentOperation(IProject project, String importID,
087: ISearchResult searchResult) {
088: fSearchResult = (DependencyExtentSearchResult) searchResult;
089: fProject = project;
090: fImportID = importID;
091: fModel = PluginRegistry.findModel(project);
092: }
093:
094: public void execute(IProgressMonitor monitor) {
095: IPluginModelBase[] plugins = PluginJavaSearchUtil
096: .getPluginImports(fImportID);
097: monitor.beginTask(
098: PDEUIMessages.DependencyExtentOperation_searching
099: + " " + fImportID + "...", 10); //$NON-NLS-1$//$NON-NLS-2$
100: checkForJavaDependencies(plugins, new SubProgressMonitor(
101: monitor, 9));
102: for (int i = 0; i < plugins.length; i++) {
103: checkForExtensionPointsUsed(plugins[i]);
104: }
105: monitor.done();
106: }
107:
108: private void checkForExtensionPointsUsed(IPluginModelBase model) {
109: IPluginExtensionPoint[] extPoints = model.getPluginBase()
110: .getExtensionPoints();
111: for (int i = 0; i < extPoints.length; i++) {
112: findMatches(extPoints[i]);
113: }
114: }
115:
116: private void findMatches(IPluginExtensionPoint point) {
117: String fullID = point.getFullId();
118: if (fullID == null)
119: return;
120:
121: IPluginExtension[] extensions = fModel.getPluginBase()
122: .getExtensions();
123: for (int i = 0; i < extensions.length; i++) {
124: if (fullID.equals(extensions[i].getPoint())) {
125: int line = ((ISourceObject) extensions[i])
126: .getStartLine() - 1;
127: if (line >= 0) {
128: fSearchResult.addMatch(new Match(point,
129: Match.UNIT_LINE, line, 1));
130: break;
131: }
132: }
133: }
134: }
135:
136: private void checkForJavaDependencies(IPluginModelBase[] models,
137: IProgressMonitor monitor) {
138: try {
139: if (!fProject.hasNature(JavaCore.NATURE_ID))
140: return;
141:
142: IJavaProject jProject = JavaCore.create(fProject);
143: IPackageFragment[] packageFragments = PluginJavaSearchUtil
144: .collectPackageFragments(models, jProject, true);
145: monitor.beginTask("", packageFragments.length); //$NON-NLS-1$
146: SearchEngine engine = new SearchEngine();
147: for (int i = 0; i < packageFragments.length; i++) {
148: if (monitor.isCanceled())
149: break;
150: IPackageFragment pkgFragment = packageFragments[i];
151: monitor
152: .subTask(PDEUIMessages.DependencyExtentOperation_inspecting
153: + " " + pkgFragment.getElementName()); //$NON-NLS-1$
154: if (pkgFragment.hasChildren()) {
155: IJavaElement[] children = pkgFragment.getChildren();
156: for (int j = 0; j < children.length; j++) {
157: if (monitor.isCanceled())
158: break;
159: IJavaElement child = children[j];
160: IType[] types = new IType[0];
161: if (child instanceof IClassFile) {
162: types = new IType[] { ((IClassFile) child)
163: .getType() };
164: } else if (child instanceof ICompilationUnit) {
165: types = ((ICompilationUnit) child)
166: .getTypes();
167: }
168: if (types.length > 0)
169: searchForTypesUsed(engine, child, types,
170: PluginJavaSearchUtil
171: .createSeachScope(jProject));
172: }
173: }
174: monitor.worked(1);
175: }
176: } catch (CoreException e) {
177: } finally {
178: monitor.done();
179: }
180: }
181:
182: private void searchForTypesUsed(SearchEngine engine,
183: IJavaElement parent, IType[] types, IJavaSearchScope scope)
184: throws CoreException {
185: for (int i = 0; i < types.length; i++) {
186: if (types[i].isAnonymous())
187: continue;
188: TypeReferenceSearchRequestor requestor = new TypeReferenceSearchRequestor();
189: engine.search(SearchPattern.createPattern(types[i],
190: IJavaSearchConstants.REFERENCES),
191: new SearchParticipant[] { SearchEngine
192: .getDefaultSearchParticipant() }, scope,
193: requestor, null);
194: if (requestor.containMatches()) {
195: TypeDeclarationSearchRequestor decRequestor = new TypeDeclarationSearchRequestor();
196: engine
197: .search(
198: SearchPattern
199: .createPattern(
200: types[i],
201: IJavaSearchConstants.DECLARATIONS),
202: new SearchParticipant[] { SearchEngine
203: .getDefaultSearchParticipant() },
204: SearchEngine
205: .createJavaSearchScope(new IJavaElement[] { parent }),
206: decRequestor, null);
207: Match match = decRequestor.getMatch();
208: if (match != null)
209: fSearchResult.addMatch(match);
210: }
211: }
212:
213: }
214:
215: }
|