Lines 10-42
Link Here
|
10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.jdt.internal.core.search; |
11 |
package org.eclipse.jdt.internal.core.search; |
12 |
|
12 |
|
|
|
13 |
import java.util.HashMap; |
13 |
import java.util.HashSet; |
14 |
import java.util.HashSet; |
|
|
15 |
import java.util.Set; |
14 |
|
16 |
|
|
|
17 |
import org.eclipse.core.resources.IFolder; |
15 |
import org.eclipse.core.runtime.IPath; |
18 |
import org.eclipse.core.runtime.IPath; |
|
|
19 |
import org.eclipse.core.runtime.Path; |
20 |
import org.eclipse.jdt.core.IClasspathEntry; |
16 |
import org.eclipse.jdt.core.IJavaElement; |
21 |
import org.eclipse.jdt.core.IJavaElement; |
17 |
import org.eclipse.jdt.core.IJavaElementDelta; |
22 |
import org.eclipse.jdt.core.IJavaElementDelta; |
|
|
23 |
import org.eclipse.jdt.core.IJavaProject; |
24 |
import org.eclipse.jdt.core.IPackageFragmentRoot; |
18 |
import org.eclipse.jdt.core.JavaModelException; |
25 |
import org.eclipse.jdt.core.JavaModelException; |
19 |
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; |
26 |
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; |
|
|
27 |
import org.eclipse.jdt.internal.core.DeltaProcessor; |
28 |
import org.eclipse.jdt.internal.core.JavaModel; |
20 |
import org.eclipse.jdt.internal.core.JavaModelManager; |
29 |
import org.eclipse.jdt.internal.core.JavaModelManager; |
21 |
import org.eclipse.jdt.internal.core.JavaProject; |
30 |
import org.eclipse.jdt.internal.core.JavaProject; |
22 |
import org.eclipse.jdt.core.IJavaProject; |
31 |
import org.eclipse.jdt.internal.core.util.Util; |
23 |
|
32 |
|
24 |
/** |
33 |
/** |
25 |
* A Java-specific scope for searching the entire workspace. |
34 |
* A Java-specific scope for searching the entire workspace. |
26 |
* The scope can be configured to not search binaries. By default, binaries |
35 |
* The scope can be configured to not search binaries. By default, binaries |
27 |
* are included. |
36 |
* are included. |
28 |
*/ |
37 |
*/ |
29 |
public class JavaWorkspaceScope extends JavaSearchScope { |
38 |
public class JavaWorkspaceScope extends AbstractJavaSearchScope { |
30 |
|
39 |
|
31 |
protected boolean needsInitialize; |
40 |
private IPath[] enclosingPaths = null; |
|
|
41 |
|
42 |
public JavaWorkspaceScope() { |
43 |
// As nothing is stored in the JavaWorkspaceScope now, no initialization is longer needed |
44 |
} |
32 |
|
45 |
|
33 |
public boolean encloses(IJavaElement element) { |
46 |
public boolean encloses(IJavaElement element) { |
34 |
/* |
|
|
35 |
if (this.needsInitialize) { |
36 |
this.initialize(); |
37 |
} |
38 |
return super.encloses(element); |
39 |
*/ |
40 |
/*A workspace scope encloses all java elements (this assumes that the index selector |
47 |
/*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 |
48 |
* and thus enclosingProjectAndJars() returns indexes on the classpath only and that these |
42 |
* indexes are consistent.) |
49 |
* indexes are consistent.) |
Lines 45-56
Link Here
|
45 |
return true; |
52 |
return true; |
46 |
} |
53 |
} |
47 |
public boolean encloses(String resourcePathString) { |
54 |
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 |
55 |
/*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 |
56 |
* and thus enclosingProjectAndJars() returns indexes on the classpath only and that these |
56 |
* indexes are consistent.) |
57 |
* indexes are consistent.) |
Lines 58-95
Link Here
|
58 |
*/ |
59 |
*/ |
59 |
return true; |
60 |
return true; |
60 |
} |
61 |
} |
|
|
62 |
/* (non-Javadoc) |
63 |
* @see org.eclipse.jdt.core.search.IJavaSearchScope#enclosingProjectsAndJars() |
64 |
*/ |
61 |
public IPath[] enclosingProjectsAndJars() { |
65 |
public IPath[] enclosingProjectsAndJars() { |
62 |
if (this.needsInitialize) { |
66 |
if (this.enclosingPaths != null) { |
63 |
this.initialize(5); |
67 |
return this.enclosingPaths; |
|
|
68 |
} |
69 |
long start = BasicSearchEngine.VERBOSE ? System.currentTimeMillis() : -1; |
70 |
try { |
71 |
IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects(); |
72 |
Set paths = new HashSet(projects.length * 2); |
73 |
for (int i = 0, length = projects.length; i < length; i++) { |
74 |
JavaProject javaProject = (JavaProject) projects[i]; |
75 |
|
76 |
// Add project full path |
77 |
IPath projectPath = javaProject.getProject().getFullPath(); |
78 |
paths.add(projectPath); |
79 |
|
80 |
// Add project libraries paths |
81 |
IClasspathEntry[] entries = javaProject.getResolvedClasspath(); |
82 |
for (int j = 0, eLength = entries.length; j < eLength; j++) { |
83 |
IClasspathEntry entry = entries[j]; |
84 |
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { |
85 |
IPath path = entry.getPath(); |
86 |
Object target = JavaModel.getTarget(path, false/*don't check existence*/); |
87 |
if (target instanceof IFolder) // case of an external folder |
88 |
path = ((IFolder) target).getFullPath(); |
89 |
paths.add(entry.getPath()); |
90 |
} |
91 |
} |
92 |
} |
93 |
IPath[] result = new IPath[paths.size()]; |
94 |
paths.toArray(result); |
95 |
return this.enclosingPaths = result; |
96 |
} catch (JavaModelException e) { |
97 |
Util.log(e, "Exception while computing workspace scope's enclosing projects and jars"); //$NON-NLS-1$ |
98 |
return new IPath[0]; |
99 |
} finally { |
100 |
if (BasicSearchEngine.VERBOSE) { |
101 |
long time = System.currentTimeMillis() - start; |
102 |
int length = this.enclosingPaths == null ? 0 : this.enclosingPaths.length; |
103 |
Util.verbose("JavaWorkspaceScope.enclosingProjectsAndJars: "+length+" paths computed in "+time+"ms."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
104 |
} |
64 |
} |
105 |
} |
65 |
return super.enclosingProjectsAndJars(); |
|
|
66 |
} |
106 |
} |
|
|
107 |
|
67 |
public boolean equals(Object o) { |
108 |
public boolean equals(Object o) { |
68 |
return o instanceof JavaWorkspaceScope; |
109 |
return o == this; // use the singleton pattern |
69 |
} |
110 |
} |
|
|
111 |
|
70 |
public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath) { |
112 |
public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath) { |
71 |
if (this.pathRestrictions == null) |
113 |
// Do not consider access rules on workspace scope |
72 |
return null; |
114 |
return null; |
73 |
return super.getAccessRuleSet(relativePath, containerPath); |
|
|
74 |
} |
115 |
} |
|
|
116 |
|
75 |
public int hashCode() { |
117 |
public int hashCode() { |
76 |
return JavaWorkspaceScope.class.hashCode(); |
118 |
return JavaWorkspaceScope.class.hashCode(); |
77 |
} |
119 |
} |
78 |
public void initialize(int size) { |
120 |
|
79 |
super.initialize(size); |
121 |
/** |
80 |
try { |
122 |
* @see AbstractJavaSearchScope#packageFragmentRoot(String, int, String) |
81 |
IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects(); |
123 |
*/ |
82 |
for (int i = 0, length = projects.length; i < length; i++) { |
124 |
public IPackageFragmentRoot packageFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPath) { |
83 |
int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES; |
125 |
HashMap rootInfos = JavaModelManager.getDeltaState().roots; |
84 |
add((JavaProject) projects[i], null, includeMask, new HashSet(length*2, 1), null); |
126 |
DeltaProcessor.RootInfo rootInfo = null; |
|
|
127 |
if (jarPath != null) { |
128 |
IPath path = new Path(jarPath); |
129 |
rootInfo = (DeltaProcessor.RootInfo) rootInfos.get(path); |
130 |
if (rootInfo == null) |
131 |
return null; |
132 |
return rootInfo.project.getPackageFragmentRoot0(path); |
133 |
} else { |
134 |
// resource in workspace |
135 |
IPath path = new Path(resourcePathString); |
136 |
rootInfo = (DeltaProcessor.RootInfo) rootInfos.get(path); |
137 |
while (rootInfo == null && path.segmentCount() > 0) { |
138 |
path = path.removeLastSegments(1); |
139 |
rootInfo = (DeltaProcessor.RootInfo) rootInfos.get(path); |
85 |
} |
140 |
} |
86 |
} catch (JavaModelException ignored) { |
141 |
if (rootInfo == null) |
87 |
// ignore |
142 |
return null; |
|
|
143 |
return rootInfo.getPackageFragmentRoot(null/*no resource hint*/); |
88 |
} |
144 |
} |
89 |
this.needsInitialize = false; |
|
|
90 |
} |
145 |
} |
|
|
146 |
|
91 |
public void processDelta(IJavaElementDelta delta, int eventType) { |
147 |
public void processDelta(IJavaElementDelta delta, int eventType) { |
92 |
if (this.needsInitialize) return; |
148 |
if (this.enclosingPaths == null) return; |
93 |
IJavaElement element = delta.getElement(); |
149 |
IJavaElement element = delta.getElement(); |
94 |
switch (element.getElementType()) { |
150 |
switch (element.getElementType()) { |
95 |
case IJavaElement.JAVA_MODEL: |
151 |
case IJavaElement.JAVA_MODEL: |
Lines 104-116
Link Here
|
104 |
switch (kind) { |
160 |
switch (kind) { |
105 |
case IJavaElementDelta.ADDED: |
161 |
case IJavaElementDelta.ADDED: |
106 |
case IJavaElementDelta.REMOVED: |
162 |
case IJavaElementDelta.REMOVED: |
107 |
this.needsInitialize = true; |
163 |
this.enclosingPaths = null; |
108 |
break; |
164 |
break; |
109 |
case IJavaElementDelta.CHANGED: |
165 |
case IJavaElementDelta.CHANGED: |
110 |
int flags = delta.getFlags(); |
166 |
int flags = delta.getFlags(); |
111 |
if ((flags & IJavaElementDelta.F_CLOSED) != 0 |
167 |
if ((flags & IJavaElementDelta.F_CLOSED) != 0 |
112 |
|| (flags & IJavaElementDelta.F_OPENED) != 0) { |
168 |
|| (flags & IJavaElementDelta.F_OPENED) != 0) { |
113 |
this.needsInitialize = true; |
169 |
this.enclosingPaths = null; |
114 |
} else { |
170 |
} else { |
115 |
children = delta.getAffectedChildren(); |
171 |
children = delta.getAffectedChildren(); |
116 |
for (int i = 0, length = children.length; i < length; i++) { |
172 |
for (int i = 0, length = children.length; i < length; i++) { |
Lines 126-145
Link Here
|
126 |
switch (kind) { |
182 |
switch (kind) { |
127 |
case IJavaElementDelta.ADDED: |
183 |
case IJavaElementDelta.ADDED: |
128 |
case IJavaElementDelta.REMOVED: |
184 |
case IJavaElementDelta.REMOVED: |
129 |
this.needsInitialize = true; |
185 |
this.enclosingPaths = null; |
130 |
break; |
186 |
break; |
131 |
case IJavaElementDelta.CHANGED: |
187 |
case IJavaElementDelta.CHANGED: |
132 |
int flags = delta.getFlags(); |
188 |
int flags = delta.getFlags(); |
133 |
if ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) > 0 |
189 |
if ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) > 0 |
134 |
|| (flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) > 0) { |
190 |
|| (flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) > 0) { |
135 |
this.needsInitialize = true; |
191 |
this.enclosingPaths = null; |
136 |
} |
192 |
} |
137 |
break; |
193 |
break; |
138 |
} |
194 |
} |
139 |
break; |
195 |
break; |
140 |
} |
196 |
} |
141 |
} |
197 |
} |
|
|
198 |
|
199 |
|
142 |
public String toString() { |
200 |
public String toString() { |
143 |
return "JavaWorkspaceScope"; //$NON-NLS-1$ |
201 |
StringBuffer result = new StringBuffer("JavaWorkspaceScope on "); //$NON-NLS-1$ |
|
|
202 |
IPath[] paths = enclosingProjectsAndJars(); |
203 |
int length = paths == null ? 0 : paths.length; |
204 |
if (length == 0) { |
205 |
result.append("[empty scope]"); //$NON-NLS-1$ |
206 |
} else { |
207 |
result.append("["); //$NON-NLS-1$ |
208 |
for (int i = 0; i < length; i++) { |
209 |
result.append("\n\t"); //$NON-NLS-1$ |
210 |
result.append(paths[i]); |
211 |
} |
212 |
result.append("\n]"); //$NON-NLS-1$ |
213 |
} |
214 |
return result.toString(); |
144 |
} |
215 |
} |
145 |
} |
216 |
} |