1 11 package org.eclipse.pde.internal.ui.search; 12 13 import java.util.ArrayList ; 14 import java.util.HashSet ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 import org.eclipse.jdt.core.IClassFile; 22 import org.eclipse.jdt.core.ICompilationUnit; 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IPackageFragment; 25 import org.eclipse.jdt.core.IPackageFragmentRoot; 26 import org.eclipse.jdt.core.IType; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.core.search.IJavaSearchConstants; 30 import org.eclipse.jdt.core.search.IJavaSearchResultCollector; 31 import org.eclipse.jdt.core.search.IJavaSearchScope; 32 import org.eclipse.jdt.core.search.SearchEngine; 33 import org.eclipse.jface.operation.*; 34 import org.eclipse.pde.core.plugin.IPluginBase; 35 import org.eclipse.pde.core.plugin.IPluginExtension; 36 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 37 import org.eclipse.pde.core.plugin.IPluginImport; 38 import org.eclipse.pde.core.plugin.IPluginModelBase; 39 import org.eclipse.pde.internal.core.PDECore; 40 import org.eclipse.pde.internal.ui.PDEPlugin; 41 42 public class UnusedDependenciesOperation implements IRunnableWithProgress { 43 private IPluginModelBase model; 44 private IProject parentProject; 45 HashSet unused = new HashSet (); 46 47 class SearchResultCollector implements IJavaSearchResultCollector { 48 int count = 0; 49 50 public void accept( 51 IResource resource, 52 int start, 53 int end, 54 IJavaElement enclosingElement, 55 int accuracy) 56 throws CoreException { 57 if (accuracy == IJavaSearchConstants.EXACT_MATCH) { 58 count += 1; 59 } 60 } 61 62 public void aboutToStart() {} 63 64 public void done() {} 65 66 public IProgressMonitor getProgressMonitor() { 67 return null; 68 } 69 70 public boolean isEmpty() { 71 return count == 0; 72 } 73 } 74 75 public UnusedDependenciesOperation(IPluginModelBase model) { 76 this.model = model; 77 this.parentProject = model.getUnderlyingResource().getProject(); 78 } 79 80 public void run(IProgressMonitor monitor) { 81 try { 82 IPluginImport[] imports = model.getPluginBase().getImports(); 83 if (imports.length == 0) 84 return; 85 86 monitor.setTaskName( 87 PDEPlugin.getResourceString("UnusedDependencies.analyze")); monitor.beginTask("", imports.length); for (int i = 0; i < imports.length; i++) { 90 if (!isUsed(imports[i], new SubProgressMonitor(monitor, 1))) 91 unused.add(imports[i]); 92 monitor.setTaskName( 93 PDEPlugin.getResourceString("UnusedDependencies.analyze") + unused.size() 95 + " " + PDEPlugin.getResourceString("UnusedDependencies.unused") + " " + (unused.size() == 1 99 ? PDEPlugin.getResourceString("DependencyExtent.singular") : PDEPlugin.getResourceString("DependencyExtent.plural")) + " " + PDEPlugin.getResourceString("DependencyExtent.found")); } 104 } finally { 105 monitor.done(); 106 } 107 108 } 109 110 private boolean isUsed(IPluginImport dependency, IProgressMonitor monitor) { 111 try { 112 HashSet set = new HashSet (); 113 PluginJavaSearchUtil.collectAllPrerequisites( 114 PDECore.getDefault().findPlugin(dependency.getId()), 115 set); 116 IPluginBase[] models = 117 (IPluginBase[]) set.toArray(new IPluginBase[set.size()]); 118 119 IPackageFragment[] packageFragments = new IPackageFragment[0]; 120 if (parentProject.hasNature(JavaCore.NATURE_ID)) 121 packageFragments = 122 PluginJavaSearchUtil.collectPackageFragments(models, parentProject); 123 124 monitor.beginTask("", packageFragments.length + 1); 126 if (providesExtensionPoint(models)) 127 return true; 128 monitor.worked(1); 129 130 if (packageFragments.length > 0) 131 return doJavaSearch(packageFragments, new SubProgressMonitor(monitor, packageFragments.length)); 132 133 } catch (JavaModelException e) { 134 } catch (CoreException e) { 135 } finally { 136 monitor.done(); 137 } 138 return false; 139 } 140 141 public IPluginImport[] getUnusedDependencies() { 142 return (IPluginImport[]) unused.toArray(new IPluginImport[unused.size()]); 143 } 144 145 private boolean providesExtensionPoint(IPluginBase[] models) { 146 IPluginExtension[] extensions = model.getPluginBase().getExtensions(); 147 for (int i = 0; i < extensions.length; i++) { 148 for (int j = 0; j < models.length; j++) { 149 if (providesExtensionPoint(models[j], extensions[i].getPoint())) 150 return true; 151 } 152 } 153 return false; 154 } 155 156 private boolean providesExtensionPoint(IPluginBase model, String targetID) { 157 IPluginExtensionPoint[] extPoints = model.getExtensionPoints(); 158 for (int i = 0; i < extPoints.length; i++) { 159 if (extPoints[i].getFullId().equals(targetID)) 160 return true; 161 } 162 return false; 163 } 164 165 private boolean doJavaSearch( 166 IPackageFragment[] packageFragments, 167 IProgressMonitor monitor) 168 throws JavaModelException { 169 SearchEngine searchEngine = new SearchEngine(); 170 IJavaSearchScope scope = getSearchScope(); 171 172 for (int i = 0; i < packageFragments.length; i++) { 173 IPackageFragment packageFragment = packageFragments[i]; 174 boolean used = false; 175 if (!packageFragment.hasSubpackages()) { 176 SearchResultCollector collector = new SearchResultCollector(); 177 searchEngine.search( 178 PDEPlugin.getWorkspace(), 179 SearchEngine.createSearchPattern( 180 packageFragment.getElementName() + ".*", IJavaSearchConstants.TYPE, 182 IJavaSearchConstants.REFERENCES, 183 true), 184 scope, 185 collector); 186 used = !collector.isEmpty(); 187 } else { 188 used = searchForTypes(packageFragment, searchEngine, scope, monitor); 189 } 190 monitor.worked(1); 191 if (used) 192 return true; 193 } 194 return false; 195 } 196 197 private boolean searchForTypes( 198 IPackageFragment fragment, 199 SearchEngine searchEngine, 200 IJavaSearchScope scope, 201 IProgressMonitor monitor) 202 throws JavaModelException { 203 IJavaElement[] children = fragment.getChildren(); 204 for (int i = 0; i < children.length; i++) { 205 IJavaElement child = children[i]; 206 IType[] types = new IType[0]; 207 if (child instanceof IClassFile) 208 types = new IType[] {((IClassFile) child).getType()}; 209 else if (child instanceof ICompilationUnit) 210 types = ((ICompilationUnit) child).getAllTypes(); 211 212 for (int j = 0; j < types.length; j++) { 213 SearchResultCollector collector = new SearchResultCollector(); 214 searchEngine.search( 215 PDEPlugin.getWorkspace(), 216 SearchEngine.createSearchPattern( 217 types[j], 218 IJavaSearchConstants.REFERENCES), 219 scope, 220 collector); 221 if (!collector.isEmpty()) { 222 return true; 223 } 224 } 225 } 226 return false; 227 } 228 229 private IJavaSearchScope getSearchScope() throws JavaModelException { 230 IPackageFragmentRoot[] roots = 231 JavaCore.create(parentProject).getPackageFragmentRoots(); 232 ArrayList filteredRoots = new ArrayList (); 233 for (int i = 0; i < roots.length; i++) { 234 if (roots[i].getResource() != null 235 && roots[i].getResource().getProject().equals(parentProject)) { 236 filteredRoots.add(roots[i]); 237 } 238 } 239 return SearchEngine.createJavaSearchScope( 240 (IJavaElement[]) filteredRoots.toArray( 241 new IJavaElement[filteredRoots.size()])); 242 } 243 244 } 245 | Popular Tags |