1 11 package org.eclipse.jdt.internal.core.hierarchy; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.SubProgressMonitor; 20 import org.eclipse.jdt.core.IClassFile; 21 import org.eclipse.jdt.core.ICompilationUnit; 22 import org.eclipse.jdt.core.IJavaElement; 23 import org.eclipse.jdt.core.IJavaProject; 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.JavaModelException; 28 import org.eclipse.jdt.internal.core.JavaModelManager; 29 import org.eclipse.jdt.internal.core.JavaProject; 30 import org.eclipse.jdt.internal.core.Openable; 31 import org.eclipse.jdt.internal.core.SearchableEnvironment; 32 33 public class RegionBasedHierarchyBuilder extends HierarchyBuilder { 34 35 public RegionBasedHierarchyBuilder(TypeHierarchy hierarchy) 36 throws JavaModelException { 37 38 super(hierarchy); 39 } 40 41 public void build(boolean computeSubtypes) { 42 43 JavaModelManager manager = JavaModelManager.getJavaModelManager(); 44 try { 45 manager.cacheZipFiles(); 47 48 if (this.hierarchy.focusType == null || computeSubtypes) { 49 IProgressMonitor typeInRegionMonitor = 50 this.hierarchy.progressMonitor == null ? 51 null : 52 new SubProgressMonitor(this.hierarchy.progressMonitor, 30); 53 HashMap allOpenablesInRegion = determineOpenablesInRegion(typeInRegionMonitor); 54 this.hierarchy.initialize(allOpenablesInRegion.size()); 55 IProgressMonitor buildMonitor = 56 this.hierarchy.progressMonitor == null ? 57 null : 58 new SubProgressMonitor(this.hierarchy.progressMonitor, 70); 59 createTypeHierarchyBasedOnRegion(allOpenablesInRegion, buildMonitor); 60 ((RegionBasedTypeHierarchy)this.hierarchy).pruneDeadBranches(); 61 } else { 62 this.hierarchy.initialize(1); 63 this.buildSupertypes(); 64 } 65 } finally { 66 manager.flushZipFiles(); 67 } 68 } 69 72 private void createTypeHierarchyBasedOnRegion(HashMap allOpenablesInRegion, IProgressMonitor monitor) { 73 74 try { 75 int size = allOpenablesInRegion.size(); 76 if (monitor != null) monitor.beginTask("", size * 2); this.infoToHandle = new HashMap (size); 78 Iterator javaProjects = allOpenablesInRegion.entrySet().iterator(); 79 while (javaProjects.hasNext()) { 80 Map.Entry entry = (Map.Entry ) javaProjects.next(); 81 JavaProject project = (JavaProject) entry.getKey(); 82 ArrayList allOpenables = (ArrayList ) entry.getValue(); 83 Openable[] openables = new Openable[allOpenables.size()]; 84 allOpenables.toArray(openables); 85 86 try { 87 SearchableEnvironment searchableEnvironment = project.newSearchableNameEnvironment(this.hierarchy.workingCopies); 89 this.nameLookup = searchableEnvironment.nameLookup; 90 this.hierarchyResolver.resolve(openables, null, monitor); 91 } catch (JavaModelException e) { 92 } 94 } 95 } finally { 96 if (monitor != null) monitor.done(); 97 } 98 } 99 100 104 private HashMap determineOpenablesInRegion(IProgressMonitor monitor) { 105 106 try { 107 HashMap allOpenables = new HashMap (); 108 IJavaElement[] roots = 109 ((RegionBasedTypeHierarchy) this.hierarchy).region.getElements(); 110 int length = roots.length; 111 if (monitor != null) monitor.beginTask("", length); for (int i = 0; i <length; i++) { 113 IJavaElement root = roots[i]; 114 IJavaProject javaProject = root.getJavaProject(); 115 ArrayList openables = (ArrayList ) allOpenables.get(javaProject); 116 if (openables == null) { 117 openables = new ArrayList (); 118 allOpenables.put(javaProject, openables); 119 } 120 switch (root.getElementType()) { 121 case IJavaElement.JAVA_PROJECT : 122 injectAllOpenablesForJavaProject((IJavaProject) root, openables); 123 break; 124 case IJavaElement.PACKAGE_FRAGMENT_ROOT : 125 injectAllOpenablesForPackageFragmentRoot((IPackageFragmentRoot) root, openables); 126 break; 127 case IJavaElement.PACKAGE_FRAGMENT : 128 injectAllOpenablesForPackageFragment((IPackageFragment) root, openables); 129 break; 130 case IJavaElement.CLASS_FILE : 131 case IJavaElement.COMPILATION_UNIT : 132 openables.add(root); 133 break; 134 case IJavaElement.TYPE : 135 IType type = (IType)root; 136 if (type.isBinary()) { 137 openables.add(type.getClassFile()); 138 } else { 139 openables.add(type.getCompilationUnit()); 140 } 141 break; 142 default : 143 break; 144 } 145 worked(monitor, 1); 146 } 147 return allOpenables; 148 } finally { 149 if (monitor != null) monitor.done(); 150 } 151 } 152 153 157 private void injectAllOpenablesForJavaProject( 158 IJavaProject project, 159 ArrayList openables) { 160 try { 161 IPackageFragmentRoot[] devPathRoots = 162 ((JavaProject) project).getPackageFragmentRoots(); 163 if (devPathRoots == null) { 164 return; 165 } 166 for (int j = 0; j < devPathRoots.length; j++) { 167 IPackageFragmentRoot root = devPathRoots[j]; 168 injectAllOpenablesForPackageFragmentRoot(root, openables); 169 } 170 } catch (JavaModelException e) { 171 } 173 } 174 175 179 private void injectAllOpenablesForPackageFragment( 180 IPackageFragment packFrag, 181 ArrayList openables) { 182 183 try { 184 IPackageFragmentRoot root = (IPackageFragmentRoot) packFrag.getParent(); 185 int kind = root.getKind(); 186 if (kind != 0) { 187 boolean isSourcePackageFragment = (kind == IPackageFragmentRoot.K_SOURCE); 188 if (isSourcePackageFragment) { 189 ICompilationUnit[] cus = packFrag.getCompilationUnits(); 190 for (int i = 0, length = cus.length; i < length; i++) { 191 openables.add(cus[i]); 192 } 193 } else { 194 IClassFile[] classFiles = packFrag.getClassFiles(); 195 for (int i = 0, length = classFiles.length; i < length; i++) { 196 openables.add(classFiles[i]); 197 } 198 } 199 } 200 } catch (JavaModelException e) { 201 } 203 } 204 205 209 private void injectAllOpenablesForPackageFragmentRoot( 210 IPackageFragmentRoot root, 211 ArrayList openables) { 212 try { 213 IJavaElement[] packFrags = root.getChildren(); 214 for (int k = 0; k < packFrags.length; k++) { 215 IPackageFragment packFrag = (IPackageFragment) packFrags[k]; 216 injectAllOpenablesForPackageFragment(packFrag, openables); 217 } 218 } catch (JavaModelException e) { 219 return; 220 } 221 } 222 223 } 224 | Popular Tags |