1 11 package org.eclipse.jdt.internal.core.hierarchy; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.jdt.core.*; 17 import org.eclipse.jdt.core.search.IJavaSearchScope; 18 import org.eclipse.jdt.internal.core.CompilationUnit; 19 import org.eclipse.jdt.internal.core.JavaElement; 20 import org.eclipse.jdt.internal.core.Openable; 21 import org.eclipse.jdt.internal.core.Region; 22 import org.eclipse.jdt.internal.core.TypeVector; 23 24 public class RegionBasedTypeHierarchy extends TypeHierarchy { 25 28 protected IRegion region; 29 30 37 public RegionBasedTypeHierarchy(IRegion region, ICompilationUnit[] workingCopies, IType type, boolean computeSubtypes) { 38 super(type, workingCopies, (IJavaSearchScope)null, computeSubtypes); 39 40 Region newRegion = new Region() { 41 public void add(IJavaElement element) { 42 if (!contains(element)) { 43 removeAllChildren(element); 45 fRootElements.add(element); 46 if (element.getElementType() == IJavaElement.JAVA_PROJECT) { 47 try { 51 IPackageFragmentRoot[] roots = ((IJavaProject) element).getPackageFragmentRoots(); 52 for (int i = 0, length = roots.length; i < length; i++) { 53 if (roots[i].isArchive() && !fRootElements.contains(roots[i])) 54 fRootElements.add(roots[i]); 55 } 56 } catch (JavaModelException e) { 57 } 59 } 60 fRootElements.trimToSize(); 61 } 62 } 63 }; 64 IJavaElement[] elements = region.getElements(); 65 for (int i = 0, length = elements.length; i < length; i++) { 66 newRegion.add(elements[i]); 67 68 } 69 this.region = newRegion; 70 if (elements.length > 0) 71 this.project = elements[0].getJavaProject(); 72 } 73 76 protected void initializeRegions() { 77 super.initializeRegions(); 78 IJavaElement[] roots = this.region.getElements(); 79 for (int i = 0; i < roots.length; i++) { 80 IJavaElement root = roots[i]; 81 if (root instanceof IOpenable) { 82 this.files.put(root, new ArrayList ()); 83 } else { 84 Openable o = (Openable) ((JavaElement) root).getOpenableParent(); 85 if (o != null) { 86 this.files.put(o, new ArrayList ()); 87 } 88 } 89 checkCanceled(); 90 } 91 } 92 95 protected void compute() throws JavaModelException, CoreException { 96 HierarchyBuilder builder = new RegionBasedHierarchyBuilder(this); 97 builder.build(this.computeSubtypes); 98 } 99 protected boolean isAffectedByOpenable(IJavaElementDelta delta, IJavaElement element) { 100 if (element instanceof CompilationUnit && ((CompilationUnit)element).isWorkingCopy()) { 102 return super.isAffectedByOpenable(delta, element); 103 } 104 105 if (this.focusType == null) { 107 return this.region.contains(element); 108 } else { 109 return super.isAffectedByOpenable(delta, element); 110 } 111 } 112 115 public IJavaProject javaProject() { 116 return this.project; 117 } 118 public void pruneDeadBranches() { 119 pruneDeadBranches(getRootClasses()); 120 pruneDeadBranches(getRootInterfaces()); 121 } 122 125 private boolean pruneDeadBranches(IType type) { 126 TypeVector subtypes = (TypeVector)this.typeToSubtypes.get(type); 127 if (subtypes == null) return true; 128 pruneDeadBranches(subtypes.copy().elements()); 129 subtypes = (TypeVector)this.typeToSubtypes.get(type); 130 return (subtypes == null || subtypes.size == 0); 131 } 132 private void pruneDeadBranches(IType[] types) { 133 for (int i = 0, length = types.length; i < length; i++) { 134 IType type = types[i]; 135 if (pruneDeadBranches(type) && !this.region.contains(type)) { 136 removeType(type); 137 } 138 } 139 } 140 144 protected void removeType(IType type) { 145 IType[] subtypes = this.getSubtypes(type); 146 this.typeToSubtypes.remove(type); 147 if (subtypes != null) { 148 for (int i= 0; i < subtypes.length; i++) { 149 this.removeType(subtypes[i]); 150 } 151 } 152 IType superclass = (IType)this.classToSuperclass.remove(type); 153 if (superclass != null) { 154 TypeVector types = (TypeVector)this.typeToSubtypes.get(superclass); 155 if (types != null) types.remove(type); 156 } 157 IType[] superinterfaces = (IType[])this.typeToSuperInterfaces.remove(type); 158 if (superinterfaces != null) { 159 for (int i = 0, length = superinterfaces.length; i < length; i++) { 160 IType superinterface = superinterfaces[i]; 161 TypeVector types = (TypeVector)this.typeToSubtypes.get(superinterface); 162 if (types != null) types.remove(type); 163 } 164 } 165 this.interfaces.remove(type); 166 } 167 168 } 169 | Popular Tags |