### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: search/org/eclipse/jdt/internal/core/search/JavaWorkspaceScope.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/JavaWorkspaceScope.java,v retrieving revision 1.30 diff -u -r1.30 JavaWorkspaceScope.java --- search/org/eclipse/jdt/internal/core/search/JavaWorkspaceScope.java 27 Sep 2007 16:33:52 -0000 1.30 +++ search/org/eclipse/jdt/internal/core/search/JavaWorkspaceScope.java 4 Mar 2008 14:00:29 -0000 @@ -11,32 +11,35 @@ package org.eclipse.jdt.internal.core.search; import java.util.HashSet; +import java.util.Set; +import org.eclipse.core.resources.IFolder; import org.eclipse.core.runtime.IPath; +import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaElementDelta; +import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; +import org.eclipse.jdt.internal.core.JavaModel; import org.eclipse.jdt.internal.core.JavaModelManager; import org.eclipse.jdt.internal.core.JavaProject; -import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.internal.core.util.Util; /** * A Java-specific scope for searching the entire workspace. * The scope can be configured to not search binaries. By default, binaries * are included. */ -public class JavaWorkspaceScope extends JavaSearchScope { +public class JavaWorkspaceScope extends AbstractJavaSearchScope { -protected boolean needsInitialize; +IPath[] enclosingPaths = null; + +public JavaWorkspaceScope() { + // As nothing is stored in the JavaWorkspaceScope now, no initialization is longer needed +} public boolean encloses(IJavaElement element) { - /* - if (this.needsInitialize) { - this.initialize(); - } - return super.encloses(element); - */ /*A workspace scope encloses all java elements (this assumes that the index selector * and thus enclosingProjectAndJars() returns indexes on the classpath only and that these * indexes are consistent.) @@ -45,12 +48,6 @@ return true; } public boolean encloses(String resourcePathString) { - /* - if (this.needsInitialize) { - this.initialize(); - } - return super.encloses(resourcePathString); - */ /*A workspace scope encloses all resources (this assumes that the index selector * and thus enclosingProjectAndJars() returns indexes on the classpath only and that these * indexes are consistent.) @@ -58,38 +55,65 @@ */ return true; } +/* (non-Javadoc) + * @see org.eclipse.jdt.core.search.IJavaSearchScope#enclosingProjectsAndJars() + */ public IPath[] enclosingProjectsAndJars() { - if (this.needsInitialize) { - this.initialize(5); + if (this.enclosingPaths != null) { + return this.enclosingPaths; } - return super.enclosingProjectsAndJars(); -} -public boolean equals(Object o) { - return o instanceof JavaWorkspaceScope; -} -public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath) { - if (this.pathRestrictions == null) - return null; - return super.getAccessRuleSet(relativePath, containerPath); -} -public int hashCode() { - return JavaWorkspaceScope.class.hashCode(); -} -public void initialize(int size) { - super.initialize(size); + long start = System.currentTimeMillis(); try { IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects(); + Set paths = new HashSet(projects.length * 2); for (int i = 0, length = projects.length; i < length; i++) { - int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES; - add((JavaProject) projects[i], null, includeMask, new HashSet(length*2, 1), null); + JavaProject javaProject = (JavaProject) projects[i]; + + // Add project full path + IPath projectPath = javaProject.getProject().getFullPath(); + paths.add(projectPath); + + // Add project libraries paths + IClasspathEntry[] entries = javaProject.getResolvedClasspath(); + for (int j = 0, eLength = entries.length; j < eLength; j++) { + IClasspathEntry entry = entries[j]; + if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) { + IPath path = entry.getPath(); + Object target = JavaModel.getTarget(path, false/*don't check existence*/); + if (target instanceof IFolder) // case of an external folder + path = ((IFolder) target).getFullPath(); + paths.add(entry.getPath()); + } + } } + enclosingPaths = new IPath[paths.size()]; + paths.toArray(enclosingPaths); } catch (JavaModelException ignored) { // ignore } - this.needsInitialize = false; + if (BasicSearchEngine.VERBOSE) { + long time = System.currentTimeMillis() - start; + int length = enclosingPaths == null ? 0 : enclosingPaths.length; + Util.verbose("JavaWorkspaceScope.enclosingProjectsAndJars: "+length+" paths computed in "+time+"ms."); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + return enclosingPaths; +} + +public boolean equals(Object o) { + return o == this; // use the singleton pattern +} + +public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath) { + // Do not consider access rules on workspace scope + return null; +} + +public int hashCode() { + return JavaWorkspaceScope.class.hashCode(); } + public void processDelta(IJavaElementDelta delta, int eventType) { - if (this.needsInitialize) return; + if (this.enclosingPaths == null) return; IJavaElement element = delta.getElement(); switch (element.getElementType()) { case IJavaElement.JAVA_MODEL: @@ -104,13 +128,13 @@ switch (kind) { case IJavaElementDelta.ADDED: case IJavaElementDelta.REMOVED: - this.needsInitialize = true; + this.enclosingPaths = null; break; case IJavaElementDelta.CHANGED: int flags = delta.getFlags(); if ((flags & IJavaElementDelta.F_CLOSED) != 0 || (flags & IJavaElementDelta.F_OPENED) != 0) { - this.needsInitialize = true; + this.enclosingPaths = null; } else { children = delta.getAffectedChildren(); for (int i = 0, length = children.length; i < length; i++) { @@ -126,20 +150,35 @@ switch (kind) { case IJavaElementDelta.ADDED: case IJavaElementDelta.REMOVED: - this.needsInitialize = true; + this.enclosingPaths = null; break; case IJavaElementDelta.CHANGED: int flags = delta.getFlags(); if ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) > 0 || (flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) > 0) { - this.needsInitialize = true; + this.enclosingPaths = null; } break; } break; } } + + public String toString() { - return "JavaWorkspaceScope"; //$NON-NLS-1$ + StringBuffer result = new StringBuffer("JavaWorkspaceScope on "); //$NON-NLS-1$ + IPath[] paths = enclosingProjectsAndJars(); + int length = paths == null ? 0 : paths.length; + if (length == 0) { + result.append("[empty scope]"); //$NON-NLS-1$ + } else { + result.append("["); //$NON-NLS-1$ + for (int i = 0; i < length; i++) { + result.append("\n\t"); //$NON-NLS-1$ + result.append(paths[i]); + } + result.append("\n]"); //$NON-NLS-1$ + } + return result.toString(); } } Index: search/org/eclipse/jdt/internal/core/search/JavaSearchScope.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/JavaSearchScope.java,v retrieving revision 1.66 diff -u -r1.66 JavaSearchScope.java --- search/org/eclipse/jdt/internal/core/search/JavaSearchScope.java 26 Feb 2008 10:11:47 -0000 1.66 +++ search/org/eclipse/jdt/internal/core/search/JavaSearchScope.java 4 Mar 2008 14:00:29 -0000 @@ -43,7 +43,7 @@ /** * A Java-specific scope for searching relative to one or more java elements. */ -public class JavaSearchScope extends AbstractSearchScope { +public class JavaSearchScope extends AbstractJavaSearchScope { private ArrayList elements; Index: search/org/eclipse/jdt/internal/core/search/AbstractJavaSearchScope.java =================================================================== RCS file: search/org/eclipse/jdt/internal/core/search/AbstractJavaSearchScope.java diff -N search/org/eclipse/jdt/internal/core/search/AbstractJavaSearchScope.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ search/org/eclipse/jdt/internal/core/search/AbstractJavaSearchScope.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2000, 2008 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.jdt.internal.core.search; + +import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; + + +public abstract class AbstractJavaSearchScope extends AbstractSearchScope { + +/** + * Get access rule set corresponding to a given path. + * @param relativePath The path user want to have restriction access + * @return The access rule set for given path or null if none is set for it. + * Returns specific unit access rule set when scope does not enclose the given path. + */ +abstract public AccessRuleSet getAccessRuleSet(String relativePath, String containerPath); +}