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; |
15 |
import org.eclipse.core.runtime.IPath; |
17 |
import org.eclipse.core.runtime.IPath; |
|
|
18 |
import org.eclipse.jdt.core.IClasspathEntry; |
16 |
import org.eclipse.jdt.core.IJavaElement; |
19 |
import org.eclipse.jdt.core.IJavaElement; |
17 |
import org.eclipse.jdt.core.IJavaElementDelta; |
20 |
import org.eclipse.jdt.core.IJavaElementDelta; |
|
|
21 |
import org.eclipse.jdt.core.IJavaProject; |
18 |
import org.eclipse.jdt.core.JavaModelException; |
22 |
import org.eclipse.jdt.core.JavaModelException; |
19 |
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; |
23 |
import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; |
|
|
24 |
import org.eclipse.jdt.internal.core.JavaModel; |
20 |
import org.eclipse.jdt.internal.core.JavaModelManager; |
25 |
import org.eclipse.jdt.internal.core.JavaModelManager; |
21 |
import org.eclipse.jdt.internal.core.JavaProject; |
26 |
import org.eclipse.jdt.internal.core.JavaProject; |
22 |
import org.eclipse.jdt.core.IJavaProject; |
27 |
import org.eclipse.jdt.internal.core.util.Util; |
23 |
|
28 |
|
24 |
/** |
29 |
/** |
25 |
* A Java-specific scope for searching the entire workspace. |
30 |
* A Java-specific scope for searching the entire workspace. |
26 |
* The scope can be configured to not search binaries. By default, binaries |
31 |
* The scope can be configured to not search binaries. By default, binaries |
27 |
* are included. |
32 |
* are included. |
28 |
*/ |
33 |
*/ |
29 |
public class JavaWorkspaceScope extends JavaSearchScope { |
34 |
public class JavaWorkspaceScope extends AbstractJavaSearchScope { |
30 |
|
35 |
|
31 |
protected boolean needsInitialize; |
36 |
IPath[] enclosingPaths = null; |
|
|
37 |
|
38 |
public JavaWorkspaceScope() { |
39 |
// As nothing is stored in the JavaWorkspaceScope now, no initialization is longer needed |
40 |
} |
32 |
|
41 |
|
33 |
public boolean encloses(IJavaElement element) { |
42 |
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 |
43 |
/*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 |
44 |
* and thus enclosingProjectAndJars() returns indexes on the classpath only and that these |
42 |
* indexes are consistent.) |
45 |
* indexes are consistent.) |
Lines 45-56
Link Here
|
45 |
return true; |
48 |
return true; |
46 |
} |
49 |
} |
47 |
public boolean encloses(String resourcePathString) { |
50 |
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 |
51 |
/*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 |
52 |
* and thus enclosingProjectAndJars() returns indexes on the classpath only and that these |
56 |
* indexes are consistent.) |
53 |
* indexes are consistent.) |
Lines 58-145
Link Here
|
58 |
*/ |
55 |
*/ |
59 |
return true; |
56 |
return true; |
60 |
} |
57 |
} |
|
|
58 |
/* (non-Javadoc) |
59 |
* @see org.eclipse.jdt.core.search.IJavaSearchScope#enclosingProjectsAndJars() |
60 |
*/ |
61 |
public IPath[] enclosingProjectsAndJars() { |
61 |
public IPath[] enclosingProjectsAndJars() { |
62 |
if (this.needsInitialize) { |
62 |
if (this.enclosingPaths != null) return this.enclosingPaths; |
63 |
this.initialize(5); |
63 |
long start = System.currentTimeMillis(); |
|
|
64 |
try { |
65 |
IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects(); |
66 |
Set paths = new HashSet(projects.length * 2); |
67 |
for (int i = 0, length = projects.length; i < length; i++) { |
68 |
JavaProject javaProject = (JavaProject) projects[i]; |
69 |
|
70 |
// Add project full path |
71 |
IPath projectPath = javaProject.getProject().getFullPath(); |
72 |
paths.add(projectPath); |
73 |
|
74 |
// Add project libraries paths |
75 |
IClasspathEntry[] entries = javaProject.getResolvedClasspath(); |
76 |
for (int j = 0, eLength = entries.length; j < eLength; j++) { |
77 |
IClasspathEntry entry = entries[j]; |
78 |
if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { |
79 |
IPath path = entry.getPath(); |
80 |
Object target = JavaModel.getTarget(path, false/*don't check existence*/); |
81 |
if (target instanceof IFolder) // case of an external folder |
82 |
path = ((IFolder) target).getFullPath(); |
83 |
paths.add(entry.getPath()); |
84 |
} |
85 |
} |
86 |
} |
87 |
enclosingPaths = new IPath[paths.size()]; |
88 |
paths.toArray(enclosingPaths); |
89 |
} catch (JavaModelException ignored) { |
90 |
// ignore |
91 |
} |
92 |
if (BasicSearchEngine.VERBOSE) { |
93 |
long time = System.currentTimeMillis() - start; |
94 |
int length = enclosingPaths == null ? 0 : enclosingPaths.length; |
95 |
Util.verbose("JavaWorkspaceScope.enclosingProjectsAndJars: "+length+" paths computed in "+time+"ms."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
64 |
} |
96 |
} |
65 |
return super.enclosingProjectsAndJars(); |
97 |
return enclosingPaths; |
66 |
} |
98 |
} |
|
|
99 |
|
67 |
public boolean equals(Object o) { |
100 |
public boolean equals(Object o) { |
68 |
return o instanceof JavaWorkspaceScope; |
101 |
return o == this; // use the singleton pattern |
69 |
} |
102 |
} |
|
|
103 |
|
70 |
public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath) { |
104 |
public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath) { |
71 |
if (this.pathRestrictions == null) |
105 |
// Do not consider access rules on workspace scope |
72 |
return null; |
106 |
return null; |
73 |
return super.getAccessRuleSet(relativePath, containerPath); |
|
|
74 |
} |
107 |
} |
|
|
108 |
|
75 |
public int hashCode() { |
109 |
public int hashCode() { |
76 |
return JavaWorkspaceScope.class.hashCode(); |
110 |
return JavaWorkspaceScope.class.hashCode(); |
77 |
} |
111 |
} |
78 |
public void initialize(int size) { |
112 |
|
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) { |
113 |
public void processDelta(IJavaElementDelta delta, int eventType) { |
92 |
if (this.needsInitialize) return; |
114 |
// 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 |
} |
115 |
} |
|
|
116 |
|
117 |
|
142 |
public String toString() { |
118 |
public String toString() { |
143 |
return "JavaWorkspaceScope"; //$NON-NLS-1$ |
119 |
StringBuffer result = new StringBuffer("JavaWorkspaceScope on "); //$NON-NLS-1$ |
|
|
120 |
IPath[] paths = enclosingProjectsAndJars(); |
121 |
int length = paths == null ? 0 : paths.length; |
122 |
if (length == 0) { |
123 |
result.append("[empty scope]"); //$NON-NLS-1$ |
124 |
} else { |
125 |
result.append("["); //$NON-NLS-1$ |
126 |
for (int i = 0; i < length; i++) { |
127 |
result.append("\n\t"); //$NON-NLS-1$ |
128 |
result.append(paths[i]); |
129 |
} |
130 |
result.append("\n]"); //$NON-NLS-1$ |
131 |
} |
132 |
return result.toString(); |
144 |
} |
133 |
} |
145 |
} |
134 |
} |