View | Details | Raw Unified | Return to bug 215139 | Differences between
and this patch

Collapse All | Expand All

(-)search/org/eclipse/jdt/internal/core/search/HierarchyScope.java (-2 / +41 lines)
Lines 44-49 Link Here
44
	
44
	
45
	public boolean needsRefresh;
45
	public boolean needsRefresh;
46
46
47
	private boolean onlySubtypes= false;
48
	private HashSet subTypes; // only set when onlySubtypes==true
49
	private IJavaProject javaProject= null;
50
47
	/* (non-Javadoc)
51
	/* (non-Javadoc)
48
	 * Adds the given resource to this search scope.
52
	 * Adds the given resource to this search scope.
49
	 */
53
	 */
Lines 59-64 Link Here
59
		elements[elementCount++] = element;
63
		elements[elementCount++] = element;
60
	}
64
	}
61
	
65
	
66
	/**
67
	 * Creates a new hierarchy scope for the given type.
68
	 * @param project      constrain the search result to this project
69
	 * @param type         the root of the hierarchy
70
	 * @param owner 
71
	 * @param onlySubtypes if true search only subtypes of 'type' (not including 'type')
72
	 */
73
	public HierarchyScope(IJavaProject project, IType type, WorkingCopyOwner owner, boolean onlySubtypes) 
74
			throws JavaModelException 
75
	{
76
		this(type, owner);
77
		this.javaProject= project;
78
		this.onlySubtypes= onlySubtypes;
79
	}
80
62
	/* (non-Javadoc)
81
	/* (non-Javadoc)
63
	 * Creates a new hiearchy scope for the given type.
82
	 * Creates a new hiearchy scope for the given type.
64
	 */
83
	 */
Lines 100-109 Link Here
100
	private void buildResourceVector() {
119
	private void buildResourceVector() {
101
		HashMap resources = new HashMap();
120
		HashMap resources = new HashMap();
102
		HashMap paths = new HashMap();
121
		HashMap paths = new HashMap();
103
		this.types = this.hierarchy.getAllTypes();
122
		if (this.onlySubtypes) {
123
			this.types= this.hierarchy.getAllSubtypes(this.focusType);
124
			this.subTypes= new HashSet(this.types.length);
125
		} else {
126
			this.types = this.hierarchy.getAllTypes();
127
		}
104
		IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
128
		IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
105
		for (int i = 0; i < this.types.length; i++) {
129
		for (int i = 0; i < this.types.length; i++) {
106
			IType type = this.types[i];
130
			IType type = this.types[i];
131
			if (this.onlySubtypes)
132
				this.subTypes.add(type);
107
			IResource resource = type.getResource();
133
			IResource resource = type.getResource();
108
			if (resource != null && resources.get(resource) == null) {
134
			if (resource != null && resources.get(resource) == null) {
109
				resources.put(resource, resource);
135
				resources.put(resource, resource);
Lines 288-293 Link Here
288
		IType type = null;
314
		IType type = null;
289
		if (element instanceof IType) {
315
		if (element instanceof IType) {
290
			type = (IType) element;
316
			type = (IType) element;
317
            if (this.onlySubtypes) {
318
            	// searching subtypes and have a type: only check in this.subTypes:
319
            	if (this.subTypes.contains(type))
320
            		return true;
321
            	// be flexible: look at original element (see bug 14106 and below)
322
                IType original = type.isBinary() ? null : (IType)type.getPrimaryElement();
323
                if (original != type && this.subTypes.contains(original)) 
324
                	return true;
325
                return false;
326
            }
291
		} else if (element instanceof IMember) {
327
		} else if (element instanceof IMember) {
292
			type = ((IMember) element).getDeclaringType();
328
			type = ((IMember) element).getDeclaringType();
293
		}
329
		}
Lines 325-331 Link Here
325
		this.elementCount = 0;
361
		this.elementCount = 0;
326
		this.needsRefresh = false;
362
		this.needsRefresh = false;
327
		if (this.hierarchy == null) {
363
		if (this.hierarchy == null) {
328
			this.hierarchy = this.focusType.newTypeHierarchy(this.owner, null);
364
			if (this.javaProject != null)
365
				this.hierarchy = this.focusType.newTypeHierarchy(this.javaProject, this.owner, null);
366
			else
367
				this.hierarchy = this.focusType.newTypeHierarchy(this.owner, null);
329
		} else {
368
		} else {
330
			this.hierarchy.refresh(null);
369
			this.hierarchy.refresh(null);
331
		}
370
		}
(-)search/org/eclipse/jdt/internal/core/search/BasicSearchEngine.java (+7 lines)
Lines 99-104 Link Here
99
	public static IJavaSearchScope createHierarchyScope(IType type) throws JavaModelException {
99
	public static IJavaSearchScope createHierarchyScope(IType type) throws JavaModelException {
100
		return createHierarchyScope(type, DefaultWorkingCopyOwner.PRIMARY);
100
		return createHierarchyScope(type, DefaultWorkingCopyOwner.PRIMARY);
101
	}
101
	}
102
103
	/**
104
	 * @see SearchEngine#createHierarchyScope(IJavaProject,IType,boolean) for detailed comment.
105
	 */
106
	public static IJavaSearchScope createHierarchyScope(IJavaProject project, IType type, boolean onlySubtypes) throws JavaModelException {
107
		return new HierarchyScope(project, type, DefaultWorkingCopyOwner.PRIMARY, onlySubtypes);
108
	}
102
	
109
	
103
	/**
110
	/**
104
	 * @see SearchEngine#createHierarchyScope(IType,WorkingCopyOwner) for detailed comment.
111
	 * @see SearchEngine#createHierarchyScope(IType,WorkingCopyOwner) for detailed comment.
(-)search/org/eclipse/jdt/core/search/SearchEngine.java (+15 lines)
Lines 168-173 Link Here
168
	public static IJavaSearchScope createHierarchyScope(IType type) throws JavaModelException {
168
	public static IJavaSearchScope createHierarchyScope(IType type) throws JavaModelException {
169
		return BasicSearchEngine.createHierarchyScope(type);
169
		return BasicSearchEngine.createHierarchyScope(type);
170
	}
170
	}
171
172
	/**
173
	 * Returns a Java search scope limited to the hierarchy of the given type and to a given project.
174
	 * The Java elements resulting from a search with this scope will
175
	 * be types in this hierarchy, or members of the types in this hierarchy.
176
	 *
177
	 * @param project the project to which to constrain the search
178
	 * @param type the focus of the hierarchy scope
179
	 * @param onlySubtypes if true only subtypes of 'type' are considered
180
	 * @return a new hierarchy scope
181
	 * @exception JavaModelException if the hierarchy could not be computed on the given type
182
	 */
183
	public static IJavaSearchScope createHierarchyScope(IJavaProject project, IType type, boolean onlySubtypes) throws JavaModelException {
184
		return BasicSearchEngine.createHierarchyScope(project, type, onlySubtypes);
185
	}
171
	
186
	
172
	/**
187
	/**
173
	 * Returns a Java search scope limited to the hierarchy of the given type.
188
	 * Returns a Java search scope limited to the hierarchy of the given type.
(-)src/org/eclipse/jdt/core/tests/model/JavaSearchTests.java (+35 lines)
Lines 2238-2243 Link Here
2238
	
2238
	
2239
}
2239
}
2240
/**
2240
/**
2241
 * Hierarchy scope test.
2242
 * (test for enhancement bug 215139 search: find only subtypes).
2243
 */
2244
public void testSearchScope06() throws CoreException {
2245
    	ICompilationUnit cuB = this. getCompilationUnit("JavaSearch", "src", "a10", "B.java");
2246
        ICompilationUnit cuC = this. getCompilationUnit("JavaSearch", "src", "a10", "C.java");
2247
        ICompilationUnit cuD = this. getCompilationUnit("JavaSearch", "src", "a10", "D.java");
2248
        ICompilationUnit cuE = this. getCompilationUnit("JavaSearch", "src", "a10", "E.java");
2249
        IType type = cuC.getType("C");
2250
        IJavaSearchScope scope = SearchEngine.createHierarchyScope(null, type, true);
2251
        
2252
        // don't include super-classes:
2253
        assertFalse("a10.A should not be included in hierarchy scope", scope.encloses(cuB.getType("A")));
2254
        assertFalse("a10.B should not be included in hierarchy scope", scope.encloses(cuB.getType("B")));
2255
        assertFalse("a10/B.java should not be included in hierarchy scope", scope.encloses(cuB.getUnderlyingResource().getFullPath().toString()));
2256
        
2257
        // don't include focus type:
2258
        assertFalse("a10.C should be not included in hierarchy scope", scope.encloses(type));
2259
        assertFalse("a10/C.java should be included in hierarchy scope", scope.encloses(cuC.getUnderlyingResource().getFullPath().toString()));
2260
        
2261
        // regular sub-types:
2262
        assertTrue("a10.D should be included in hierarchy scope", scope.encloses(cuD.getType("D")));
2263
        assertTrue("a10/D.java should be included in hierarchy scope", scope.encloses(cuD.getUnderlyingResource().getFullPath().toString()));
2264
        
2265
        assertTrue("a10.E should be included in hierarchy scope", scope.encloses(cuE.getType("E")));
2266
        assertTrue("a10.F should be included in hierarchy scope", scope.encloses(cuE.getType("F")));
2267
        assertTrue("a10.I should be included in hierarchy scope", scope.encloses(cuE.getType("H").getType("I")));
2268
        // member of a sub-type:
2269
        assertFalse("a10.G should not be included in hierarchy scope", scope.encloses(cuE.getType("F").getType("G")));
2270
        // enclosing of a sub-type:
2271
        assertFalse("a10.H should not be included in hierarchy scope", scope.encloses(cuE.getType("H")));
2272
        assertTrue("a10/E.java should be included in hierarchy scope", scope.encloses(cuE.getUnderlyingResource().getFullPath().toString()));
2273
}
2274
2275
/**
2241
 * Simple type declaration test.
2276
 * Simple type declaration test.
2242
 */
2277
 */
2243
public void testTypeDeclaration01() throws CoreException { // was testSimpleTypeDeclaration
2278
public void testTypeDeclaration01() throws CoreException { // was testSimpleTypeDeclaration
(-)workspace/JavaSearch/src/a10/D.java (+3 lines)
Added Link Here
1
package a10;
2
3
public class D extends C {}
(-)workspace/JavaSearch/src/a10/C.java (+3 lines)
Added Link Here
1
package a10;
2
3
public class C extends B {}
(-)workspace/JavaSearch/src/a10/E.java (+11 lines)
Added Link Here
1
package a10;
2
3
public class E extends D {}
4
5
class F extends C {
6
	class G {}
7
}
8
9
class H { 
10
	class I extends E {}
11
}
(-)workspace/JavaSearch/src/a10/B.java (+5 lines)
Added Link Here
1
package a10;
2
3
class A { }
4
5
public class B extends A {}

Return to bug 215139