PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/eclipse-wtp-jeetools-3.4.0/org.eclipse.jst.j2ee.ui/j2ee_ui/org/eclipse/jst/j2ee/internal/dialogs/TypeJavaSearchScope.java

#
Java | 352 lines | 230 code | 29 blank | 93 comment | 59 complexity | d12ab69eb760a606fa612a6ecf96f61d MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2003, 2005 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.jst.j2ee.internal.dialogs;
  12. import java.util.ArrayList;
  13. import java.util.HashSet;
  14. import org.eclipse.core.resources.IProject;
  15. import org.eclipse.core.resources.IResource;
  16. import org.eclipse.core.runtime.IPath;
  17. import org.eclipse.core.runtime.Path;
  18. import org.eclipse.jdt.core.IClasspathEntry;
  19. import org.eclipse.jdt.core.IJavaElement;
  20. import org.eclipse.jdt.core.IJavaModel;
  21. import org.eclipse.jdt.core.IJavaProject;
  22. import org.eclipse.jdt.core.IMember;
  23. import org.eclipse.jdt.core.IOpenable;
  24. import org.eclipse.jdt.core.IPackageFragment;
  25. import org.eclipse.jdt.core.IPackageFragmentRoot;
  26. import org.eclipse.jdt.core.JavaModelException;
  27. import org.eclipse.jdt.core.search.IJavaSearchScope;
  28. /**
  29. * This class was derived from JavaSearchScope as that class did not have a
  30. * provision to exclude classpath entries that are not exported A Java-specific
  31. * scope for searching relative to one or more java elements.
  32. */
  33. public class TypeJavaSearchScope implements IJavaSearchScope {
  34. private boolean includeExportedClassPathEntriesOnly = true;
  35. private ArrayList elements;
  36. /*
  37. * The paths of the resources in this search scope (or the classpath
  38. * entries' paths if the resources are projects)
  39. */
  40. private IPath[] paths;
  41. private boolean[] pathWithSubFolders;
  42. private int pathsCount;
  43. private IPath[] enclosingProjectsAndJars;
  44. public TypeJavaSearchScope() {
  45. this.initialize();
  46. // disabled for now as this could be expensive
  47. // JavaModelManager.getJavaModelManager().rememberScope(this);
  48. }
  49. private void addEnclosingProjectOrJar(IPath path) {
  50. int length = this.enclosingProjectsAndJars.length;
  51. for (int i = 0; i < length; i++) {
  52. if (this.enclosingProjectsAndJars[i].equals(path))
  53. return;
  54. }
  55. System.arraycopy(this.enclosingProjectsAndJars, 0, this.enclosingProjectsAndJars = new IPath[length + 1], 0, length);
  56. this.enclosingProjectsAndJars[length] = path;
  57. }
  58. /**
  59. * Method addProject. This method adds all the classpath entries for the
  60. * current project to the search scope.
  61. *
  62. * @param javaProject
  63. * @param includesPrereqProjects
  64. * @param visitedProjects
  65. * @throws JavaModelException
  66. */
  67. public void addProject(IJavaProject javaProject, boolean includesPrereqProjects, HashSet visitedProjects) throws JavaModelException {
  68. IProject project = javaProject.getProject();
  69. if (!project.isAccessible() || !visitedProjects.add(project))
  70. return;
  71. this.addEnclosingProjectOrJar(project.getFullPath());
  72. IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
  73. IJavaModel model = javaProject.getJavaModel();
  74. for (int i = 0, length = entries.length; i < length; i++) {
  75. IClasspathEntry entry = entries[i];
  76. switch (entry.getEntryKind()) {
  77. case IClasspathEntry.CPE_LIBRARY :
  78. IPath path = entry.getPath();
  79. this.add(path, true);
  80. this.addEnclosingProjectOrJar(path);
  81. break;
  82. case IClasspathEntry.CPE_PROJECT :
  83. if (includesPrereqProjects) {
  84. this.add(model.getJavaProject(entry.getPath().lastSegment()), true, visitedProjects);
  85. }
  86. break;
  87. case IClasspathEntry.CPE_SOURCE :
  88. this.add(entry.getPath(), true);
  89. break;
  90. }
  91. }
  92. }
  93. /**
  94. * Method add. This method filters out all the classpath entries of the
  95. * project which are not exported.
  96. *
  97. * @param javaProject
  98. * @param includesPrereqProjects
  99. * @param visitedProjects
  100. * @throws JavaModelException
  101. */
  102. public void add(IJavaProject javaProject, boolean includesPrereqProjects, HashSet visitedProjects) throws JavaModelException {
  103. IProject project = javaProject.getProject();
  104. if (!project.isAccessible() || !visitedProjects.add(project))
  105. return;
  106. this.addEnclosingProjectOrJar(project.getFullPath());
  107. IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
  108. IJavaModel model = javaProject.getJavaModel();
  109. for (int i = 0, length = entries.length; i < length; i++) {
  110. IClasspathEntry entry = entries[i];
  111. if (includeExportedClassPathEntriesOnly()) {
  112. if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE)
  113. continue;
  114. }
  115. switch (entry.getEntryKind()) {
  116. case IClasspathEntry.CPE_LIBRARY :
  117. IPath path = entry.getPath();
  118. this.add(path, true);
  119. this.addEnclosingProjectOrJar(path);
  120. break;
  121. case IClasspathEntry.CPE_PROJECT :
  122. if (includesPrereqProjects) {
  123. this.add(model.getJavaProject(entry.getPath().lastSegment()), true, visitedProjects);
  124. }
  125. break;
  126. case IClasspathEntry.CPE_SOURCE :
  127. this.add(entry.getPath(), true);
  128. break;
  129. }
  130. }
  131. }
  132. public void add(IJavaElement element) throws JavaModelException {
  133. IPackageFragmentRoot root = null;
  134. switch (element.getElementType()) {
  135. case IJavaElement.JAVA_MODEL :
  136. // a workspace sope should be used
  137. break;
  138. case IJavaElement.JAVA_PROJECT :
  139. this.add((IJavaProject) element, true, new HashSet(2));
  140. break;
  141. case IJavaElement.PACKAGE_FRAGMENT_ROOT :
  142. root = (IPackageFragmentRoot) element;
  143. this.add(root.getPath(), true);
  144. break;
  145. case IJavaElement.PACKAGE_FRAGMENT :
  146. root = (IPackageFragmentRoot) element.getParent();
  147. if (root.isArchive()) {
  148. this.add(root.getPath().append(new Path(element.getElementName().replace('.', '/'))), false);
  149. } else {
  150. IResource resource = element.getUnderlyingResource();
  151. if (resource != null && resource.isAccessible()) {
  152. this.add(resource.getFullPath(), false);
  153. }
  154. }
  155. break;
  156. default :
  157. // remember sub-cu (or sub-class file) java elements
  158. if (element instanceof IMember) {
  159. if (this.elements == null) {
  160. this.elements = new ArrayList();
  161. }
  162. this.elements.add(element);
  163. }
  164. this.add(this.fullPath(element), true);
  165. // find package fragment root including this java element
  166. IJavaElement parent = element.getParent();
  167. while (parent != null && !(parent instanceof IPackageFragmentRoot)) {
  168. parent = parent.getParent();
  169. }
  170. if (parent instanceof IPackageFragmentRoot) {
  171. root = (IPackageFragmentRoot) parent;
  172. }
  173. }
  174. if (root != null) {
  175. if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
  176. this.addEnclosingProjectOrJar(root.getPath());
  177. } else {
  178. this.addEnclosingProjectOrJar(root.getJavaProject().getProject().getFullPath());
  179. }
  180. }
  181. }
  182. /**
  183. * Adds the given path to this search scope. Remember if subfolders need to
  184. * be included as well.
  185. */
  186. private void add(IPath path, boolean withSubFolders) {
  187. if (this.paths.length == this.pathsCount) {
  188. System.arraycopy(this.paths, 0, this.paths = new IPath[this.pathsCount * 2], 0, this.pathsCount);
  189. System.arraycopy(this.pathWithSubFolders, 0, this.pathWithSubFolders = new boolean[this.pathsCount * 2], 0, this.pathsCount);
  190. }
  191. this.paths[this.pathsCount] = path;
  192. this.pathWithSubFolders[this.pathsCount++] = withSubFolders;
  193. }
  194. /*
  195. * (non-Javadoc)
  196. *
  197. * @see IJavaSearchScope#encloses(String)
  198. */
  199. public boolean encloses(String resourcePathString) {
  200. IPath resourcePath;
  201. int separatorIndex = resourcePathString.indexOf(JAR_FILE_ENTRY_SEPARATOR);
  202. if (separatorIndex != -1) {
  203. resourcePath = new Path(resourcePathString.substring(0, separatorIndex)).append(new Path(resourcePathString.substring(separatorIndex + 1)));
  204. } else {
  205. resourcePath = new Path(resourcePathString);
  206. }
  207. return this.encloses(resourcePath);
  208. }
  209. /**
  210. * Returns whether this search scope encloses the given path.
  211. */
  212. private boolean encloses(IPath path) {
  213. for (int i = 0; i < this.pathsCount; i++) {
  214. if (this.pathWithSubFolders[i]) {
  215. if (this.paths[i].isPrefixOf(path)) {
  216. return true;
  217. }
  218. } else {
  219. IPath scopePath = this.paths[i];
  220. if (scopePath.isPrefixOf(path) && (scopePath.segmentCount() == path.segmentCount() - 1)) {
  221. return true;
  222. }
  223. }
  224. }
  225. return false;
  226. }
  227. /*
  228. * (non-Javadoc)
  229. *
  230. * @see IJavaSearchScope#encloses(IJavaElement)
  231. */
  232. public boolean encloses(IJavaElement element) {
  233. if (this.elements != null) {
  234. for (int i = 0, length = this.elements.size(); i < length; i++) {
  235. IJavaElement scopeElement = (IJavaElement) this.elements.get(i);
  236. IJavaElement searchedElement = element;
  237. while (searchedElement != null) {
  238. if (searchedElement.equals(scopeElement)) {
  239. return true;
  240. }
  241. searchedElement = searchedElement.getParent();
  242. }
  243. }
  244. return false;
  245. }
  246. return this.encloses(this.fullPath(element));
  247. }
  248. /*
  249. * (non-Javadoc)
  250. *
  251. * @see IJavaSearchScope#enclosingProjectsAndJars()
  252. */
  253. public IPath[] enclosingProjectsAndJars() {
  254. return this.enclosingProjectsAndJars;
  255. }
  256. private IPath fullPath(IJavaElement element) {
  257. if (element instanceof IPackageFragmentRoot) {
  258. return ((IPackageFragmentRoot) element).getPath();
  259. }
  260. IJavaElement parent = element.getParent();
  261. IPath parentPath = parent == null ? null : this.fullPath(parent);
  262. IPath childPath;
  263. if (element instanceof IPackageFragment) {
  264. childPath = new Path(element.getElementName().replace('.', '/'));
  265. } else if (element instanceof IOpenable) {
  266. childPath = new Path(element.getElementName());
  267. } else {
  268. return parentPath;
  269. }
  270. return parentPath == null ? childPath : parentPath.append(childPath);
  271. }
  272. protected void initialize() {
  273. this.paths = new IPath[1];
  274. this.pathWithSubFolders = new boolean[1];
  275. this.pathsCount = 0;
  276. this.enclosingProjectsAndJars = new IPath[0];
  277. }
  278. /**
  279. * Gets the includeExportedClassPathEntriesOnly.
  280. *
  281. * @return Returns a boolean
  282. */
  283. public boolean includeExportedClassPathEntriesOnly() {
  284. return includeExportedClassPathEntriesOnly;
  285. }
  286. /**
  287. * Sets the includeExportedClassPathEntriesOnly.
  288. *
  289. * @param includeExportedClassPathEntriesOnly
  290. * The includeExportedClassPathEntriesOnly to set
  291. */
  292. public void setIncludeExportedClassPathEntriesOnly(boolean includeExportedClassPathEntriesOnly) {
  293. this.includeExportedClassPathEntriesOnly = includeExportedClassPathEntriesOnly;
  294. }
  295. /**
  296. * @see IJavaSearchScope#includesBinaries()
  297. * @deprecated
  298. */
  299. public boolean includesBinaries() {
  300. return true;
  301. }
  302. /**
  303. * @see IJavaSearchScope#includesClasspaths()
  304. * @deprecated
  305. */
  306. public boolean includesClasspaths() {
  307. return true;
  308. }
  309. /**
  310. * @see IJavaSearchScope#setIncludesBinaries(boolean)
  311. * @deprecated
  312. */
  313. public void setIncludesBinaries(boolean includesBinaries) {
  314. //Default nothing
  315. }
  316. /**
  317. * @see IJavaSearchScope#setIncludesClasspaths(boolean)
  318. * @deprecated
  319. */
  320. public void setIncludesClasspaths(boolean includesClasspaths) {
  321. //Default nothing
  322. }
  323. }