Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6c430f69440711f713196e67690f8943fd99eedc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*******************************************************************************
 * Copyright (c) 2003, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.dialogs;

import java.util.ArrayList;
import java.util.HashSet;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IOpenable;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchScope;

/**
 * This class was derived from JavaSearchScope as that class did not have a
 * provision to exclude classpath entries that are not exported A Java-specific
 * scope for searching relative to one or more java elements.
 */
public class TypeJavaSearchScope implements IJavaSearchScope {

	private boolean includeExportedClassPathEntriesOnly = true;

	private ArrayList elements;

	/*
	 * The paths of the resources in this search scope (or the classpath
	 * entries' paths if the resources are projects)
	 */
	private IPath[] paths;
	private boolean[] pathWithSubFolders;
	private int pathsCount;

	private IPath[] enclosingProjectsAndJars;

	public TypeJavaSearchScope() {
		this.initialize();

		// disabled for now as this could be expensive
		// JavaModelManager.getJavaModelManager().rememberScope(this);
	}

	private void addEnclosingProjectOrJar(IPath path) {
		int length = this.enclosingProjectsAndJars.length;
		for (int i = 0; i < length; i++) {
			if (this.enclosingProjectsAndJars[i].equals(path))
				return;
		}
		System.arraycopy(this.enclosingProjectsAndJars, 0, this.enclosingProjectsAndJars = new IPath[length + 1], 0, length);
		this.enclosingProjectsAndJars[length] = path;
	}

	/**
	 * Method addProject. This method adds all the classpath entries for the
	 * current project to the search scope.
	 * 
	 * @param javaProject
	 * @param includesPrereqProjects
	 * @param visitedProjects
	 * @throws JavaModelException
	 */
	public void addProject(IJavaProject javaProject, boolean includesPrereqProjects, HashSet visitedProjects) throws JavaModelException {
		IProject project = javaProject.getProject();
		if (!project.isAccessible() || !visitedProjects.add(project))
			return;

		this.addEnclosingProjectOrJar(project.getFullPath());

		IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
		IJavaModel model = javaProject.getJavaModel();
		for (int i = 0, length = entries.length; i < length; i++) {
			IClasspathEntry entry = entries[i];
			switch (entry.getEntryKind()) {
				case IClasspathEntry.CPE_LIBRARY :
					IPath path = entry.getPath();
					this.add(path, true);
					this.addEnclosingProjectOrJar(path);
					break;
				case IClasspathEntry.CPE_PROJECT :
					if (includesPrereqProjects) {
						this.add(model.getJavaProject(entry.getPath().lastSegment()), true, visitedProjects);
					}
					break;
				case IClasspathEntry.CPE_SOURCE :
					this.add(entry.getPath(), true);
					break;
			}
		}
	}

	/**
	 * Method add. This method filters out all the classpath entries of the
	 * project which are not exported.
	 * 
	 * @param javaProject
	 * @param includesPrereqProjects
	 * @param visitedProjects
	 * @throws JavaModelException
	 */
	public void add(IJavaProject javaProject, boolean includesPrereqProjects, HashSet visitedProjects) throws JavaModelException {
		IProject project = javaProject.getProject();
		if (!project.isAccessible() || !visitedProjects.add(project))
			return;

		this.addEnclosingProjectOrJar(project.getFullPath());

		IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
		IJavaModel model = javaProject.getJavaModel();
		for (int i = 0, length = entries.length; i < length; i++) {
			IClasspathEntry entry = entries[i];
			if (includeExportedClassPathEntriesOnly()) {
				if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE)
					continue;
			}
			switch (entry.getEntryKind()) {
				case IClasspathEntry.CPE_LIBRARY :
					IPath path = entry.getPath();
					this.add(path, true);
					this.addEnclosingProjectOrJar(path);
					break;
				case IClasspathEntry.CPE_PROJECT :
					if (includesPrereqProjects) {
						this.add(model.getJavaProject(entry.getPath().lastSegment()), true, visitedProjects);
					}
					break;
				case IClasspathEntry.CPE_SOURCE :
					this.add(entry.getPath(), true);
					break;
			}
		}
	}
	public void add(IJavaElement element) throws JavaModelException {
		IPackageFragmentRoot root = null;
		switch (element.getElementType()) {
			case IJavaElement.JAVA_MODEL :
				// a workspace sope should be used
				break;
			case IJavaElement.JAVA_PROJECT :
				this.add((IJavaProject) element, true, new HashSet(2));
				break;
			case IJavaElement.PACKAGE_FRAGMENT_ROOT :
				root = (IPackageFragmentRoot) element;
				this.add(root.getPath(), true);
				break;
			case IJavaElement.PACKAGE_FRAGMENT :
				root = (IPackageFragmentRoot) element.getParent();
				if (root.isArchive()) {
					this.add(root.getPath().append(new Path(element.getElementName().replace('.', '/'))), false);
				} else {
					IResource resource = element.getUnderlyingResource();
					if (resource != null && resource.isAccessible()) {
						this.add(resource.getFullPath(), false);
					}
				}
				break;
			default :
				// remember sub-cu (or sub-class file) java elements
				if (element instanceof IMember) {
					if (this.elements == null) {
						this.elements = new ArrayList();
					}
					this.elements.add(element);
				}
				this.add(this.fullPath(element), true);

				// find package fragment root including this java element
				IJavaElement parent = element.getParent();
				while (parent != null && !(parent instanceof IPackageFragmentRoot)) {
					parent = parent.getParent();
				}
				if (parent instanceof IPackageFragmentRoot) {
					root = (IPackageFragmentRoot) parent;
				}
		}

		if (root != null) {
			if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
				this.addEnclosingProjectOrJar(root.getPath());
			} else {
				this.addEnclosingProjectOrJar(root.getJavaProject().getProject().getFullPath());
			}
		}
	}

	/**
	 * Adds the given path to this search scope. Remember if subfolders need to
	 * be included as well.
	 */
	private void add(IPath path, boolean withSubFolders) {
		if (this.paths.length == this.pathsCount) {
			System.arraycopy(this.paths, 0, this.paths = new IPath[this.pathsCount * 2], 0, this.pathsCount);
			System.arraycopy(this.pathWithSubFolders, 0, this.pathWithSubFolders = new boolean[this.pathsCount * 2], 0, this.pathsCount);
		}
		this.paths[this.pathsCount] = path;
		this.pathWithSubFolders[this.pathsCount++] = withSubFolders;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see IJavaSearchScope#encloses(String)
	 */
	public boolean encloses(String resourcePathString) {
		IPath resourcePath;
		int separatorIndex = resourcePathString.indexOf(JAR_FILE_ENTRY_SEPARATOR);
		if (separatorIndex != -1) {
			resourcePath = new Path(resourcePathString.substring(0, separatorIndex)).append(new Path(resourcePathString.substring(separatorIndex + 1)));
		} else {
			resourcePath = new Path(resourcePathString);
		}
		return this.encloses(resourcePath);
	}

	/**
	 * Returns whether this search scope encloses the given path.
	 */
	private boolean encloses(IPath path) {
		for (int i = 0; i < this.pathsCount; i++) {
			if (this.pathWithSubFolders[i]) {
				if (this.paths[i].isPrefixOf(path)) {
					return true;
				}
			} else {
				IPath scopePath = this.paths[i];
				if (scopePath.isPrefixOf(path) && (scopePath.segmentCount() == path.segmentCount() - 1)) {
					return true;
				}
			}
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see IJavaSearchScope#encloses(IJavaElement)
	 */
	public boolean encloses(IJavaElement element) {
		if (this.elements != null) {
			for (int i = 0, length = this.elements.size(); i < length; i++) {
				IJavaElement scopeElement = (IJavaElement) this.elements.get(i);
				IJavaElement searchedElement = element;
				while (searchedElement != null) {
					if (searchedElement.equals(scopeElement)) {
						return true;
					} 
					searchedElement = searchedElement.getParent();
				}
			}
			return false;
		}
		return this.encloses(this.fullPath(element));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see IJavaSearchScope#enclosingProjectsAndJars()
	 */
	public IPath[] enclosingProjectsAndJars() {
		return this.enclosingProjectsAndJars;
	}
	private IPath fullPath(IJavaElement element) {
		if (element instanceof IPackageFragmentRoot) {
			return ((IPackageFragmentRoot) element).getPath();
		} 
		IJavaElement parent = element.getParent();
		IPath parentPath = parent == null ? null : this.fullPath(parent);
		IPath childPath;
		if (element instanceof IPackageFragment) {
			childPath = new Path(element.getElementName().replace('.', '/'));
		} else if (element instanceof IOpenable) {
			childPath = new Path(element.getElementName());
		} else {
			return parentPath;
		}
		return parentPath == null ? childPath : parentPath.append(childPath);
	}

	protected void initialize() {
		this.paths = new IPath[1];
		this.pathWithSubFolders = new boolean[1];
		this.pathsCount = 0;
		this.enclosingProjectsAndJars = new IPath[0];
	}
	/**
	 * Gets the includeExportedClassPathEntriesOnly.
	 * 
	 * @return Returns a boolean
	 */
	public boolean includeExportedClassPathEntriesOnly() {
		return includeExportedClassPathEntriesOnly;
	}

	/**
	 * Sets the includeExportedClassPathEntriesOnly.
	 * 
	 * @param includeExportedClassPathEntriesOnly
	 *            The includeExportedClassPathEntriesOnly to set
	 */
	public void setIncludeExportedClassPathEntriesOnly(boolean includeExportedClassPathEntriesOnly) {
		this.includeExportedClassPathEntriesOnly = includeExportedClassPathEntriesOnly;
	}
	/**
	 * @see IJavaSearchScope#includesBinaries()
	 * @deprecated
	 */
	public boolean includesBinaries() {
		return true;
	}

	/**
	 * @see IJavaSearchScope#includesClasspaths()
	 * @deprecated
	 */
	public boolean includesClasspaths() {
		return true;
	}

	/**
	 * @see IJavaSearchScope#setIncludesBinaries(boolean)
	 * @deprecated
	 */
	public void setIncludesBinaries(boolean includesBinaries) {
		//Default nothing
	}

	/**
	 * @see IJavaSearchScope#setIncludesClasspaths(boolean)
	 * @deprecated
	 */
	public void setIncludesClasspaths(boolean includesClasspaths) {
		//Default nothing
	}

}

Back to the top