Lines 11-42
Link Here
|
11 |
package org.eclipse.jdt.internal.core.search; |
11 |
package org.eclipse.jdt.internal.core.search; |
12 |
|
12 |
|
13 |
import java.util.HashSet; |
13 |
import java.util.HashSet; |
|
|
14 |
import java.util.Set; |
14 |
|
15 |
|
|
|
16 |
import org.eclipse.core.resources.IFolder; |
17 |
import org.eclipse.core.resources.IProject; |
15 |
import org.eclipse.core.runtime.IPath; |
18 |
import org.eclipse.core.runtime.IPath; |
|
|
19 |
import org.eclipse.jdt.core.IClasspathEntry; |
16 |
import org.eclipse.jdt.core.IJavaElement; |
20 |
import org.eclipse.jdt.core.IJavaElement; |
17 |
import org.eclipse.jdt.core.IJavaElementDelta; |
21 |
import org.eclipse.jdt.core.IJavaElementDelta; |
|
|
22 |
import org.eclipse.jdt.core.IJavaModel; |
23 |
import org.eclipse.jdt.core.IJavaProject; |
18 |
import org.eclipse.jdt.core.JavaModelException; |
24 |
import org.eclipse.jdt.core.JavaModelException; |
19 |
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; |
25 |
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; |
|
|
26 |
import org.eclipse.jdt.internal.core.ClasspathEntry; |
27 |
import org.eclipse.jdt.internal.core.JavaModel; |
20 |
import org.eclipse.jdt.internal.core.JavaModelManager; |
28 |
import org.eclipse.jdt.internal.core.JavaModelManager; |
21 |
import org.eclipse.jdt.internal.core.JavaProject; |
29 |
import org.eclipse.jdt.internal.core.JavaProject; |
22 |
import org.eclipse.jdt.core.IJavaProject; |
|
|
23 |
|
30 |
|
24 |
/** |
31 |
/** |
25 |
* A Java-specific scope for searching the entire workspace. |
32 |
* A Java-specific scope for searching the entire workspace. |
26 |
* The scope can be configured to not search binaries. By default, binaries |
33 |
* The scope can be configured to not search binaries. By default, binaries |
27 |
* are included. |
34 |
* are included. |
28 |
*/ |
35 |
*/ |
29 |
public class JavaWorkspaceScope extends JavaSearchScope { |
36 |
public class JavaWorkspaceScope extends AbstractJavaSearchScope { |
|
|
37 |
|
38 |
public JavaWorkspaceScope() { |
39 |
// As nothing is stored in the JavaWorkspaceScope now, no initialization is longer needed |
40 |
} |
30 |
|
41 |
|
31 |
protected boolean needsInitialize; |
42 |
private void addEnclosingPath(JavaProject javaProject, IPath pathToAdd, Set enclosingPaths, HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException { |
|
|
43 |
IProject project = javaProject.getProject(); |
44 |
if (!project.isAccessible() || !visitedProjects.add(project)) return; |
32 |
|
45 |
|
33 |
public boolean encloses(IJavaElement element) { |
46 |
IPath projectPath = project.getFullPath(); |
34 |
/* |
47 |
enclosingPaths.add(projectPath); |
35 |
if (this.needsInitialize) { |
48 |
|
36 |
this.initialize(); |
49 |
IClasspathEntry[] entries = javaProject.getResolvedClasspath(); |
|
|
50 |
IJavaModel model = javaProject.getJavaModel(); |
51 |
for (int i = 0, length = entries.length; i < length; i++) { |
52 |
IClasspathEntry entry = entries[i]; |
53 |
ClasspathEntry cpEntry = (ClasspathEntry) entry; |
54 |
if (referringEntry != null) { |
55 |
// Add only exported entries. |
56 |
// Source folder are implicitly exported. |
57 |
if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) { |
58 |
// Need to remove the project from visited projects list to be sure |
59 |
// not to skip library when the project will be added as a top level. |
60 |
// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=176831 |
61 |
visitedProjects.remove(project); |
62 |
continue; |
63 |
} |
64 |
cpEntry = cpEntry.combineWith((ClasspathEntry)referringEntry); |
65 |
} |
66 |
IPath path = entry.getPath(); |
67 |
switch (entry.getEntryKind()) { |
68 |
case IClasspathEntry.CPE_LIBRARY: |
69 |
switch (cpEntry.getEntryKind()) { |
70 |
case IClasspathEntry.CPE_LIBRARY: |
71 |
case IClasspathEntry.CPE_VARIABLE: |
72 |
if (pathToAdd == null || pathToAdd.equals(path)) { |
73 |
Object target = JavaModel.getTarget(path, false/*don't check existence*/); |
74 |
if (target instanceof IFolder) // case of an external folder |
75 |
path = ((IFolder) target).getFullPath(); |
76 |
enclosingPaths.add(entry.getPath()); |
77 |
} |
78 |
break; |
79 |
case IClasspathEntry.CPE_CONTAINER: |
80 |
if (pathToAdd == null || pathToAdd.equals(path)) { |
81 |
Object target = JavaModel.getTarget(path, false/*don't check existence*/); |
82 |
if (target instanceof IFolder) // case of an external folder |
83 |
path = ((IFolder) target).getFullPath(); |
84 |
enclosingPaths.add(entry.getPath()); |
85 |
} |
86 |
break; |
87 |
} |
88 |
break; |
89 |
case IClasspathEntry.CPE_PROJECT: |
90 |
if (pathToAdd == null || pathToAdd.equals(path)) { |
91 |
addEnclosingPath((JavaProject) model.getJavaProject(path.lastSegment()), null, enclosingPaths, visitedProjects, cpEntry); |
92 |
} |
93 |
break; |
94 |
case IClasspathEntry.CPE_SOURCE: |
95 |
break; |
96 |
} |
37 |
} |
97 |
} |
38 |
return super.encloses(element); |
98 |
} |
39 |
*/ |
99 |
|
|
|
100 |
public boolean encloses(IJavaElement element) { |
40 |
/*A workspace scope encloses all java elements (this assumes that the index selector |
101 |
/*A workspace scope encloses all java elements (this assumes that the index selector |
41 |
* and thus enclosingProjectAndJars() returns indexes on the classpath only and that these |
102 |
* and thus enclosingProjectAndJars() returns indexes on the classpath only and that these |
42 |
* indexes are consistent.) |
103 |
* indexes are consistent.) |
Lines 45-56
Link Here
|
45 |
return true; |
106 |
return true; |
46 |
} |
107 |
} |
47 |
public boolean encloses(String resourcePathString) { |
108 |
public boolean encloses(String resourcePathString) { |
48 |
/* |
|
|
49 |
if (this.needsInitialize) { |
50 |
this.initialize(); |
51 |
} |
52 |
return super.encloses(resourcePathString); |
53 |
*/ |
54 |
/*A workspace scope encloses all resources (this assumes that the index selector |
109 |
/*A workspace scope encloses all resources (this assumes that the index selector |
55 |
* and thus enclosingProjectAndJars() returns indexes on the classpath only and that these |
110 |
* and thus enclosingProjectAndJars() returns indexes on the classpath only and that these |
56 |
* indexes are consistent.) |
111 |
* indexes are consistent.) |
Lines 59-145
Link Here
|
59 |
return true; |
114 |
return true; |
60 |
} |
115 |
} |
61 |
public IPath[] enclosingProjectsAndJars() { |
116 |
public IPath[] enclosingProjectsAndJars() { |
62 |
if (this.needsInitialize) { |
117 |
IPath[] results = null; |
63 |
this.initialize(5); |
118 |
try { |
|
|
119 |
IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects(); |
120 |
Set enclosingPaths = new HashSet(projects.length * 2); |
121 |
for (int i = 0, length = projects.length; i < length; i++) { |
122 |
addEnclosingPath((JavaProject) projects[i], null, enclosingPaths, new HashSet(length*2, 1), null); |
123 |
} |
124 |
results = new IPath[enclosingPaths.size()]; |
125 |
enclosingPaths.toArray(results); |
126 |
} catch (JavaModelException ignored) { |
127 |
// ignore |
64 |
} |
128 |
} |
65 |
return super.enclosingProjectsAndJars(); |
129 |
return results; |
66 |
} |
130 |
} |
|
|
131 |
|
67 |
public boolean equals(Object o) { |
132 |
public boolean equals(Object o) { |
68 |
return o instanceof JavaWorkspaceScope; |
133 |
return o == this; // use the singleton pattern |
69 |
} |
134 |
} |
|
|
135 |
|
70 |
public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath) { |
136 |
public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath) { |
71 |
if (this.pathRestrictions == null) |
137 |
// Do not consider access rules on workspace scope |
72 |
return null; |
138 |
return null; |
73 |
return super.getAccessRuleSet(relativePath, containerPath); |
|
|
74 |
} |
139 |
} |
|
|
140 |
|
75 |
public int hashCode() { |
141 |
public int hashCode() { |
76 |
return JavaWorkspaceScope.class.hashCode(); |
142 |
return JavaWorkspaceScope.class.hashCode(); |
77 |
} |
143 |
} |
78 |
public void initialize(int size) { |
144 |
|
79 |
super.initialize(size); |
|
|
80 |
try { |
81 |
IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects(); |
82 |
for (int i = 0, length = projects.length; i < length; i++) { |
83 |
int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES; |
84 |
add((JavaProject) projects[i], null, includeMask, new HashSet(length*2, 1), null); |
85 |
} |
86 |
} catch (JavaModelException ignored) { |
87 |
// ignore |
88 |
} |
89 |
this.needsInitialize = false; |
90 |
} |
91 |
public void processDelta(IJavaElementDelta delta, int eventType) { |
145 |
public void processDelta(IJavaElementDelta delta, int eventType) { |
92 |
if (this.needsInitialize) return; |
146 |
// Do nothing as workspace scope does not retain any information |
93 |
IJavaElement element = delta.getElement(); |
|
|
94 |
switch (element.getElementType()) { |
95 |
case IJavaElement.JAVA_MODEL: |
96 |
IJavaElementDelta[] children = delta.getAffectedChildren(); |
97 |
for (int i = 0, length = children.length; i < length; i++) { |
98 |
IJavaElementDelta child = children[i]; |
99 |
this.processDelta(child, eventType); |
100 |
} |
101 |
break; |
102 |
case IJavaElement.JAVA_PROJECT: |
103 |
int kind = delta.getKind(); |
104 |
switch (kind) { |
105 |
case IJavaElementDelta.ADDED: |
106 |
case IJavaElementDelta.REMOVED: |
107 |
this.needsInitialize = true; |
108 |
break; |
109 |
case IJavaElementDelta.CHANGED: |
110 |
int flags = delta.getFlags(); |
111 |
if ((flags & IJavaElementDelta.F_CLOSED) != 0 |
112 |
|| (flags & IJavaElementDelta.F_OPENED) != 0) { |
113 |
this.needsInitialize = true; |
114 |
} else { |
115 |
children = delta.getAffectedChildren(); |
116 |
for (int i = 0, length = children.length; i < length; i++) { |
117 |
IJavaElementDelta child = children[i]; |
118 |
this.processDelta(child, eventType); |
119 |
} |
120 |
} |
121 |
break; |
122 |
} |
123 |
break; |
124 |
case IJavaElement.PACKAGE_FRAGMENT_ROOT: |
125 |
kind = delta.getKind(); |
126 |
switch (kind) { |
127 |
case IJavaElementDelta.ADDED: |
128 |
case IJavaElementDelta.REMOVED: |
129 |
this.needsInitialize = true; |
130 |
break; |
131 |
case IJavaElementDelta.CHANGED: |
132 |
int flags = delta.getFlags(); |
133 |
if ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) > 0 |
134 |
|| (flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) > 0) { |
135 |
this.needsInitialize = true; |
136 |
} |
137 |
break; |
138 |
} |
139 |
break; |
140 |
} |
141 |
} |
147 |
} |
|
|
148 |
|
149 |
|
142 |
public String toString() { |
150 |
public String toString() { |
143 |
return "JavaWorkspaceScope"; //$NON-NLS-1$ |
151 |
StringBuffer result = new StringBuffer("JavaWorkspaceScope on "); //$NON-NLS-1$ |
|
|
152 |
IPath[] paths = enclosingProjectsAndJars(); |
153 |
int length = paths == null ? 0 : paths.length; |
154 |
if (length == 0) { |
155 |
result.append("[empty scope]"); //$NON-NLS-1$ |
156 |
} else { |
157 |
result.append("["); //$NON-NLS-1$ |
158 |
for (int i = 0; i < length; i++) { |
159 |
result.append("\n\t"); //$NON-NLS-1$ |
160 |
result.append(paths[i]); |
161 |
} |
162 |
result.append("\n]"); //$NON-NLS-1$ |
163 |
} |
164 |
return result.toString(); |
144 |
} |
165 |
} |
145 |
} |
166 |
} |