KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > search > dependencies > DependencyExtentOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.search.dependencies;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.SubProgressMonitor;
17 import org.eclipse.jdt.core.IClassFile;
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.core.IPackageFragment;
22 import org.eclipse.jdt.core.IType;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.search.IJavaSearchConstants;
25 import org.eclipse.jdt.core.search.IJavaSearchScope;
26 import org.eclipse.jdt.core.search.SearchEngine;
27 import org.eclipse.jdt.core.search.SearchMatch;
28 import org.eclipse.jdt.core.search.SearchParticipant;
29 import org.eclipse.jdt.core.search.SearchPattern;
30 import org.eclipse.jdt.core.search.SearchRequestor;
31 import org.eclipse.pde.core.ISourceObject;
32 import org.eclipse.pde.core.plugin.IPluginExtension;
33 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
34 import org.eclipse.pde.core.plugin.IPluginModelBase;
35 import org.eclipse.pde.core.plugin.PluginRegistry;
36 import org.eclipse.pde.internal.core.search.PluginJavaSearchUtil;
37 import org.eclipse.pde.internal.ui.PDEUIMessages;
38 import org.eclipse.search.ui.ISearchResult;
39 import org.eclipse.search.ui.text.Match;
40
41
42 public class DependencyExtentOperation {
43     
44     class TypeReferenceSearchRequestor extends SearchRequestor {
45         boolean fUsed = false;
46         
47         /* (non-Javadoc)
48          * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
49          */

50         public void acceptSearchMatch(SearchMatch match) throws CoreException {
51             if (match.getAccuracy() == SearchMatch.A_ACCURATE && !match.isInsideDocComment()) {
52                 fUsed = true;
53             }
54         }
55         
56         public boolean containMatches() {
57             return fUsed;
58         }
59     }
60     
61     class TypeDeclarationSearchRequestor extends SearchRequestor {
62         
63         private Match fMatch;
64
65         /* (non-Javadoc)
66          * @see org.eclipse.jdt.core.search.SearchRequestor#acceptSearchMatch(org.eclipse.jdt.core.search.SearchMatch)
67          */

68         public void acceptSearchMatch(SearchMatch match) throws CoreException {
69             if (!match.isInsideDocComment())
70                 fMatch = new Match(match.getElement(), Match.UNIT_CHARACTER, match.getOffset(), match.getLength());
71         }
72         
73         public Match getMatch() {
74             return fMatch;
75         }
76     }
77     
78     private DependencyExtentSearchResult fSearchResult;
79     private String JavaDoc fImportID;
80     private IPluginModelBase fModel;
81     private IProject fProject;
82
83     public DependencyExtentOperation(IProject project, String JavaDoc importID, ISearchResult searchResult) {
84         fSearchResult = (DependencyExtentSearchResult)searchResult;
85         fProject = project;
86         fImportID = importID;
87         fModel = PluginRegistry.findModel(project);
88     }
89     
90     public void execute(IProgressMonitor monitor) {
91         IPluginModelBase[] plugins = PluginJavaSearchUtil.getPluginImports(fImportID);
92         monitor.beginTask(PDEUIMessages.DependencyExtentOperation_searching + " " + fImportID + "...", 10); //$NON-NLS-1$//$NON-NLS-2$
93
checkForJavaDependencies(plugins, new SubProgressMonitor(monitor, 9));
94         for (int i = 0; i < plugins.length; i++) {
95             checkForExtensionPointsUsed(plugins[i]);
96         }
97         monitor.done();
98     }
99
100     private void checkForExtensionPointsUsed(IPluginModelBase model) {
101         IPluginExtensionPoint[] extPoints = model.getPluginBase().getExtensionPoints();
102         for (int i = 0; i < extPoints.length; i++) {
103             findMatches(extPoints[i]);
104         }
105     }
106     
107     private void findMatches(IPluginExtensionPoint point) {
108         String JavaDoc fullID = point.getFullId();
109         if (fullID == null)
110             return;
111
112         IPluginExtension[] extensions = fModel.getPluginBase().getExtensions();
113         for (int i = 0; i < extensions.length; i++) {
114             if (fullID.equals(extensions[i].getPoint())) {
115                 int line = ((ISourceObject)extensions[i]).getStartLine()-1;
116                 if (line >= 0) {
117                     fSearchResult.addMatch(new Match(point, Match.UNIT_LINE, line, 1));
118                     break;
119                 }
120             }
121         }
122     }
123     
124     private void checkForJavaDependencies(IPluginModelBase[] models, IProgressMonitor monitor) {
125         try {
126             if (!fProject.hasNature(JavaCore.NATURE_ID))
127                 return;
128             
129             IJavaProject jProject = JavaCore.create(fProject);
130             IPackageFragment[] packageFragments = PluginJavaSearchUtil.collectPackageFragments(models, jProject, true);
131             monitor.beginTask("", packageFragments.length); //$NON-NLS-1$
132
SearchEngine engine = new SearchEngine();
133             for (int i = 0; i < packageFragments.length; i++) {
134                 if (monitor.isCanceled())
135                     break;
136                 IPackageFragment pkgFragment = packageFragments[i];
137                 monitor.subTask(PDEUIMessages.DependencyExtentOperation_inspecting + " " + pkgFragment.getElementName()); //$NON-NLS-1$
138
if (pkgFragment.hasChildren()) {
139                     IJavaElement[] children = pkgFragment.getChildren();
140                     for (int j = 0; j < children.length; j++) {
141                         if (monitor.isCanceled())
142                             break;
143                         IJavaElement child = children[j];
144                         IType[] types = new IType[0];
145                         if (child instanceof IClassFile) {
146                             types = new IType[] {((IClassFile) child).getType()};
147                         } else if (child instanceof ICompilationUnit) {
148                             types = ((ICompilationUnit) child).getTypes();
149                         }
150                         if (types.length > 0)
151                             searchForTypesUsed(engine, child, types, PluginJavaSearchUtil.createSeachScope(jProject));
152                     }
153                 }
154                 monitor.worked(1);
155             }
156         } catch (CoreException e) {
157         } finally {
158             monitor.done();
159         }
160     }
161     
162     private void searchForTypesUsed(SearchEngine engine, IJavaElement parent, IType[] types, IJavaSearchScope scope) throws CoreException {
163         for (int i = 0; i < types.length; i++) {
164             if (types[i].isAnonymous())
165                 continue;
166             TypeReferenceSearchRequestor requestor = new TypeReferenceSearchRequestor();
167             engine.search(
168                     SearchPattern.createPattern(types[i], IJavaSearchConstants.REFERENCES),
169                     new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
170                     scope,
171                     requestor,
172                     null);
173             if (requestor.containMatches()) {
174                 TypeDeclarationSearchRequestor decRequestor = new TypeDeclarationSearchRequestor();
175                 engine.search(
176                         SearchPattern.createPattern(types[i], IJavaSearchConstants.DECLARATIONS),
177                         new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
178                         SearchEngine.createJavaSearchScope(new IJavaElement[] {parent}),
179                         decRequestor,
180                         null);
181                 Match match = decRequestor.getMatch();
182                 if (match != null)
183                     fSearchResult.addMatch(match);
184             }
185         }
186         
187     }
188
189 }
190
Popular Tags