KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > hierarchy > RegionBasedTypeHierarchy


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.hierarchy;
12
13 import java.util.ArrayList JavaDoc;
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     /**
26      * The region of types for which to build the hierarchy
27      */

28     protected IRegion region;
29     
30 /**
31  * Creates a TypeHierarchy on the types in the specified region,
32  * considering first the given working copies,
33  * using the projects in the given region for a name lookup context. If a specific
34  * type is also specified, the type hierarchy is pruned to only
35  * contain the branch including the specified type.
36  */

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                 //"new" element added to region
44
removeAllChildren(element);
45                 fRootElements.add(element);
46                 if (element.getElementType() == IJavaElement.JAVA_PROJECT) {
47                     // add jar roots as well so that jars don't rely on their parent to know
48
// if they are contained in the region
49
// (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=146615)
50
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                         // project doesn't exist
58
}
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 /*
74  * @see TypeHierarchy#initializeRegions
75  */

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 JavaDoc());
83         } else {
84             Openable o = (Openable) ((JavaElement) root).getOpenableParent();
85             if (o != null) {
86                 this.files.put(o, new ArrayList JavaDoc());
87             }
88         }
89         checkCanceled();
90     }
91 }
92 /**
93  * Compute this type hierarchy.
94  */

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     // change to working copy
101
if (element instanceof CompilationUnit && ((CompilationUnit)element).isWorkingCopy()) {
102         return super.isAffectedByOpenable(delta, element);
103     }
104
105     // if no focus, hierarchy is affected if the element is part of the region
106
if (this.focusType == null) {
107         return this.region.contains(element);
108     } else {
109         return super.isAffectedByOpenable(delta, element);
110     }
111 }
112 /**
113  * Returns the java project this hierarchy was created in.
114  */

115 public IJavaProject javaProject() {
116     return this.project;
117 }
118 public void pruneDeadBranches() {
119     pruneDeadBranches(getRootClasses());
120     pruneDeadBranches(getRootInterfaces());
121 }
122 /*
123  * Returns whether all subtypes of the given type have been pruned.
124  */

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 /**
141  * Removes all the subtypes of the given type from the type hierarchy,
142  * removes its superclass entry and removes the references from its super types.
143  */

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