1 11 package org.eclipse.pde.internal.ui.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.Path; 20 import org.eclipse.jdt.core.IJavaElement; 21 import org.eclipse.jdt.core.IJavaProject; 22 import org.eclipse.jdt.core.IPackageFragment; 23 import org.eclipse.jdt.core.IPackageFragmentRoot; 24 import org.eclipse.jdt.core.JavaCore; 25 import org.eclipse.jdt.core.JavaModelException; 26 import org.eclipse.pde.core.plugin.IFragment; 27 import org.eclipse.pde.core.plugin.IPlugin; 28 import org.eclipse.pde.core.plugin.IPluginBase; 29 import org.eclipse.pde.core.plugin.IPluginImport; 30 import org.eclipse.pde.core.plugin.IPluginLibrary; 31 import org.eclipse.pde.internal.core.ClasspathUtilCore; 32 import org.eclipse.pde.internal.core.PDECore; 33 import org.eclipse.pde.internal.core.plugin.WorkspacePluginModelBase; 34 35 public class PluginJavaSearchUtil { 36 37 public static void collectAllPrerequisites(IPlugin plugin, HashSet set) { 38 if (!set.add(plugin)) 39 return; 40 41 if (plugin.getModel() instanceof WorkspacePluginModelBase) { 42 IFragment[] fragments = 43 PDECore.getDefault().getWorkspaceModelManager().getFragmentsFor( 44 plugin.getId(), 45 plugin.getVersion()); 46 for (int i = 0; i < fragments.length; i++) { 47 set.add(fragments[i]); 48 } 49 } 50 51 IPluginImport[] imports = plugin.getImports(); 52 for (int i = 0; i < imports.length; i++) { 53 if (imports[i].isReexported()) { 54 IPlugin child = PDECore.getDefault().findPlugin(imports[i].getId()); 55 if (child != null) 56 collectAllPrerequisites(child, set); 57 } 58 } 59 } 60 61 public static IPackageFragment[] collectPackageFragments( 62 IPluginBase[] models, 63 IProject parentProject) 64 throws JavaModelException { 65 ArrayList result = new ArrayList (); 66 IPackageFragmentRoot[] roots = 67 JavaCore.create(parentProject).getAllPackageFragmentRoots(); 68 69 for (int i = 0; i < models.length; i++) { 70 IPluginBase preReq = models[i]; 71 IResource resource = preReq.getModel().getUnderlyingResource(); 72 if (resource == null) { 73 ArrayList libraryPaths = getLibraryPaths(preReq); 74 for (int j = 0; j < roots.length; j++) { 75 if (libraryPaths.contains(roots[j].getPath())) { 76 extractFragments(roots[j], result); 77 } 78 } 79 } else { 80 IProject project = resource.getProject(); 81 for (int j = 0; j < roots.length; j++) { 82 IJavaProject jProject = (IJavaProject) roots[j].getParent(); 83 if (jProject.getProject().equals(project)) { 84 extractFragments(roots[j], result); 85 } 86 } 87 } 88 } 89 return (IPackageFragment[]) result.toArray(new IPackageFragment[result.size()]); 90 91 } 92 93 private static void extractFragments(IPackageFragmentRoot root, ArrayList result) { 94 try { 95 IJavaElement[] children = root.getChildren(); 96 for (int i = 0; i < children.length; i++) { 97 IPackageFragment fragment = (IPackageFragment) children[i]; 98 if (fragment.getChildren().length > 0) 99 result.add(fragment); 100 } 101 } catch (JavaModelException e) { 102 } 103 } 104 105 private static ArrayList getLibraryPaths(IPluginBase plugin) { 106 ArrayList libraryPaths = new ArrayList (); 107 IFragment[] fragments = 108 PDECore.getDefault().findFragmentsFor(plugin.getId(), plugin.getVersion()); 109 110 IPluginLibrary[] libraries = plugin.getLibraries(); 111 for (int i = 0; i < libraries.length; i++) { 112 String libraryName = 113 ClasspathUtilCore.expandLibraryName(libraries[i].getName()); 114 String path = 115 plugin.getModel().getInstallLocation() + Path.SEPARATOR + libraryName; 116 if (new File (path).exists()) { 117 libraryPaths.add(new Path(path)); 118 } else { 119 findLibraryInFragments(fragments, libraryName, libraryPaths); 120 } 121 } 122 return libraryPaths; 123 } 124 125 private static void findLibraryInFragments( 126 IFragment[] fragments, 127 String libraryName, 128 ArrayList libraryPaths) { 129 for (int i = 0; i < fragments.length; i++) { 130 String path = 131 fragments[i].getModel().getInstallLocation() 132 + Path.SEPARATOR 133 + libraryName; 134 if (new File (path).exists()) { 135 libraryPaths.add(new Path(path)); 136 break; 137 } 138 } 139 } 140 141 } 142 | Popular Tags |