1 11 package org.eclipse.pde.internal.core.search; 12 13 import java.io.File ; 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.runtime.IPath; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.jdt.core.IJavaElement; 22 import org.eclipse.jdt.core.IJavaProject; 23 import org.eclipse.jdt.core.IPackageFragment; 24 import org.eclipse.jdt.core.IPackageFragmentRoot; 25 import org.eclipse.jdt.core.JavaModelException; 26 import org.eclipse.jdt.core.search.IJavaSearchScope; 27 import org.eclipse.jdt.core.search.SearchEngine; 28 import org.eclipse.pde.core.plugin.IFragmentModel; 29 import org.eclipse.pde.core.plugin.IPlugin; 30 import org.eclipse.pde.core.plugin.IPluginBase; 31 import org.eclipse.pde.core.plugin.IPluginImport; 32 import org.eclipse.pde.core.plugin.IPluginLibrary; 33 import org.eclipse.pde.core.plugin.IPluginModelBase; 34 import org.eclipse.pde.core.plugin.PluginRegistry; 35 import org.eclipse.pde.internal.core.ClasspathUtilCore; 36 import org.eclipse.pde.internal.core.PDEManager; 37 38 public class PluginJavaSearchUtil { 39 40 public static IPluginModelBase[] getPluginImports(IPluginImport dep) { 41 return getPluginImports(dep.getId()); 42 } 43 44 public static IPluginModelBase[] getPluginImports(String pluginImportID) { 45 HashSet set = new HashSet (); 46 collectAllPrerequisites(PluginRegistry.findModel(pluginImportID), set); 47 return (IPluginModelBase[]) set.toArray(new IPluginModelBase[set.size()]); 48 } 49 50 public static void collectAllPrerequisites(IPluginModelBase model, HashSet set) { 51 if (model == null || !set.add(model)) 52 return; 53 IPluginImport[] imports = model.getPluginBase().getImports(); 54 for (int i = 0; i < imports.length; i++) { 55 if (imports[i].isReexported()) { 56 IPluginModelBase child = PluginRegistry.findModel(imports[i].getId()); 57 if (child != null) 58 collectAllPrerequisites(child, set); 59 } 60 } 61 } 62 63 public static IPackageFragment[] collectPackageFragments( 64 IPluginModelBase[] models, 65 IJavaProject parentProject, 66 boolean filterEmptyPackages) 67 throws JavaModelException { 68 ArrayList result = new ArrayList (); 69 IPackageFragmentRoot[] roots = parentProject.getAllPackageFragmentRoots(); 70 71 for (int i = 0; i < models.length; i++) { 72 IPluginModelBase model = models[i]; 73 IResource resource = model.getUnderlyingResource(); 74 if (resource == null) { 75 ArrayList libraryPaths = new ArrayList (); 76 addLibraryPaths(model, libraryPaths); 77 for (int j = 0; j < roots.length; j++) { 78 if (libraryPaths.contains(roots[j].getPath())) { 79 extractPackageFragments(roots[j], result, filterEmptyPackages); 80 } 81 } 82 } else { 83 IProject project = resource.getProject(); 84 for (int j = 0; j < roots.length; j++) { 85 IJavaProject jProject = (IJavaProject) roots[j].getParent(); 86 if (jProject.getProject().equals(project)) { 87 extractPackageFragments(roots[j], result, filterEmptyPackages); 88 } 89 } 90 } 91 } 92 return (IPackageFragment[]) result.toArray(new IPackageFragment[result.size()]); 93 } 94 95 private static void extractPackageFragments(IPackageFragmentRoot root, ArrayList result, boolean filterEmpty) { 96 try { 97 IJavaElement[] children = root.getChildren(); 98 for (int i = 0; i < children.length; i++) { 99 IPackageFragment fragment = (IPackageFragment) children[i]; 100 if (!filterEmpty || fragment.hasChildren()) 101 result.add(fragment); 102 } 103 } catch (JavaModelException e) { 104 } 105 } 106 107 private static void addLibraryPaths(IPluginModelBase model, ArrayList libraryPaths) { 108 IPluginBase plugin = model.getPluginBase(); 109 110 IFragmentModel[] fragments = new IFragmentModel[0]; 111 if (plugin instanceof IPlugin) 112 fragments = PDEManager.findFragmentsFor(model); 113 114 File file = new File (model.getInstallLocation()); 115 if (file.isFile()) { 116 libraryPaths.add(new Path(file.getAbsolutePath())); 117 } else { 118 IPluginLibrary[] libraries = plugin.getLibraries(); 119 for (int i = 0; i < libraries.length; i++) { 120 String libraryName = 121 ClasspathUtilCore.expandLibraryName(libraries[i].getName()); 122 String path = 123 plugin.getModel().getInstallLocation() + IPath.SEPARATOR + libraryName; 124 if (new File (path).exists()) { 125 libraryPaths.add(new Path(path)); 126 } else { 127 findLibraryInFragments(fragments, libraryName, libraryPaths); 128 } 129 } 130 } 131 if (ClasspathUtilCore.hasExtensibleAPI(model)) { 132 for (int i = 0; i < fragments.length; i++) { 133 addLibraryPaths(fragments[i], libraryPaths); 134 } 135 } 136 } 137 138 private static void findLibraryInFragments( 139 IFragmentModel[] fragments, 140 String libraryName, 141 ArrayList libraryPaths) { 142 for (int i = 0; i < fragments.length; i++) { 143 String path = 144 fragments[i].getInstallLocation() 145 + IPath.SEPARATOR 146 + libraryName; 147 if (new File (path).exists()) { 148 libraryPaths.add(new Path(path)); 149 break; 150 } 151 } 152 } 153 154 public static IJavaSearchScope createSeachScope(IJavaProject jProject) throws JavaModelException { 155 IPackageFragmentRoot[] roots = jProject.getPackageFragmentRoots(); 156 ArrayList filteredRoots = new ArrayList (); 157 for (int i = 0; i < roots.length; i++) { 158 if (roots[i].getResource() != null 159 && roots[i].getResource().getProject().equals(jProject.getProject())) { 160 filteredRoots.add(roots[i]); 161 } 162 } 163 return SearchEngine.createJavaSearchScope( 164 (IJavaElement[]) filteredRoots.toArray( 165 new IJavaElement[filteredRoots.size()])); 166 } 167 168 169 } 170 | Popular Tags |