Search in sources :

Example 1 with IndexManager

use of org.eclipse.jdt.internal.core.search.indexing.IndexManager in project che by eclipse.

the class IndexSelector method initializeIndexLocations.

/*
 *  Compute the list of paths which are keying index files.
 */
private void initializeIndexLocations() {
    IPath[] projectsAndJars = this.searchScope.enclosingProjectsAndJars();
    IndexManager manager = JavaModelManager.getIndexManager();
    // use a linked set to preserve the order during search: see bug 348507
    LinkedHashSet locations = new LinkedHashSet();
    IJavaElement focus = MatchLocator.projectOrJarFocus(this.pattern);
    if (focus == null) {
        for (int i = 0; i < projectsAndJars.length; i++) {
            IPath path = projectsAndJars[i];
            Object target = JavaModel.getTarget(path, false);
            if (// case of an external folder
            target instanceof IFolder)
                path = ((IFolder) target).getFullPath();
            locations.add(manager.computeIndexLocation(path));
        }
    } else {
        try {
            // See whether the state builder might be used to reduce the number of index locations
            // find the projects from projectsAndJars that see the focus then walk those projects looking for the jars from projectsAndJars
            int length = projectsAndJars.length;
            JavaProject[] projectsCanSeeFocus = new JavaProject[length];
            SimpleSet visitedProjects = new SimpleSet(length);
            int projectIndex = 0;
            SimpleSet externalLibsToCheck = new SimpleSet(length);
            ObjectVector superTypes = new ObjectVector();
            IJavaElement[] focuses = getFocusedElementsAndTypes(this.pattern, focus, superTypes);
            char[][][] focusQualifiedNames = null;
            boolean isAutoBuilding = ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding();
            if (isAutoBuilding && focus instanceof IJavaProject) {
                focusQualifiedNames = getQualifiedNames(superTypes);
            }
            IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
            for (int i = 0; i < length; i++) {
                IPath path = projectsAndJars[i];
                JavaProject project = (JavaProject) getJavaProject(path, model);
                if (project != null) {
                    visitedProjects.add(project);
                    /*We are adding all modules to the locations for searching in each one.
					Now the location contains not only current module and Jars on which depends, but also all modules from the workspace.*/
                    locations.add(manager.computeIndexLocation(path));
                /*int canSeeFocus = canSeeFocus(focuses, project, focusQualifiedNames);
					if (canSeeFocus == PROJECT_CAN_SEE_FOCUS) {
						locations.add(manager.computeIndexLocation(path));
					}
					if (canSeeFocus != PROJECT_CAN_NOT_SEE_FOCUS) {
						projectsCanSeeFocus[projectIndex++] = project;
					}*/
                } else {
                    externalLibsToCheck.add(path);
                }
            }
            for (int i = 0; i < projectIndex && externalLibsToCheck.elementSize > 0; i++) {
                IClasspathEntry[] entries = projectsCanSeeFocus[i].getResolvedClasspath();
                for (int j = entries.length; --j >= 0; ) {
                    IClasspathEntry entry = entries[j];
                    if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPath path = entry.getPath();
                        if (externalLibsToCheck.remove(path) != null) {
                            Object target = JavaModel.getTarget(path, false);
                            if (// case of an external folder
                            target instanceof IFolder)
                                path = ((IFolder) target).getFullPath();
                            locations.add(manager.computeIndexLocation(path));
                        }
                    }
                }
            }
            // jar files can be included in the search scope without including one of the projects that references them, so scan all projects that have not been visited
            if (externalLibsToCheck.elementSize > 0) {
                IJavaProject[] allProjects = model.getJavaProjects();
                for (int i = 0, l = allProjects.length; i < l && externalLibsToCheck.elementSize > 0; i++) {
                    JavaProject project = (JavaProject) allProjects[i];
                    if (!visitedProjects.includes(project)) {
                        IClasspathEntry[] entries = project.getResolvedClasspath();
                        for (int j = entries.length; --j >= 0; ) {
                            IClasspathEntry entry = entries[j];
                            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                                IPath path = entry.getPath();
                                if (externalLibsToCheck.remove(path) != null) {
                                    Object target = JavaModel.getTarget(path, false);
                                    if (// case of an external folder
                                    target instanceof IFolder)
                                        path = ((IFolder) target).getFullPath();
                                    locations.add(manager.computeIndexLocation(path));
                                }
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
        // ignored
        }
    }
    // Ensure no nulls
    locations.remove(null);
    this.indexLocations = (IndexLocation[]) locations.toArray(new IndexLocation[locations.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) SimpleSet(org.eclipse.jdt.internal.compiler.util.SimpleSet) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ObjectVector(org.eclipse.jdt.internal.compiler.util.ObjectVector) IndexManager(org.eclipse.jdt.internal.core.search.indexing.IndexManager) IJavaProject(org.eclipse.jdt.core.IJavaProject) IndexLocation(org.eclipse.jdt.internal.core.index.IndexLocation) IFolder(org.eclipse.core.resources.IFolder) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 2 with IndexManager

use of org.eclipse.jdt.internal.core.search.indexing.IndexManager in project che by eclipse.

the class DeltaProcessor method updateIndex.

private void updateIndex(Openable element, IResourceDelta delta) {
    IndexManager indexManager = this.manager.indexManager;
    if (indexManager == null)
        return;
    switch(element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
            switch(delta.getKind()) {
                case IResourceDelta.ADDED:
                    indexManager.indexAll(element.getJavaProject().getProject());
                    break;
                case IResourceDelta.REMOVED:
                    indexManager.removeIndexFamily(element.getJavaProject().getProject().getFullPath());
                    // NB: Discarding index jobs belonging to this project was done during PRE_DELETE
                    break;
            }
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            if (element instanceof JarPackageFragmentRoot) {
                JarPackageFragmentRoot root = (JarPackageFragmentRoot) element;
                // index jar file only once (if the root is in its declaring project)
                IPath jarPath = root.getPath();
                switch(delta.getKind()) {
                    case IResourceDelta.ADDED:
                        // index the new jar
                        indexManager.indexLibrary(jarPath, root.getJavaProject().getProject(), root.getIndexPath());
                        break;
                    case IResourceDelta.CHANGED:
                        // first remove the index so that it is forced to be re-indexed
                        indexManager.removeIndex(jarPath);
                        // then index the jar
                        indexManager.indexLibrary(jarPath, root.getJavaProject().getProject(), root.getIndexPath());
                        break;
                    case IResourceDelta.REMOVED:
                        // the jar was physically removed: remove the index
                        indexManager.discardJobs(jarPath.toString());
                        indexManager.removeIndex(jarPath);
                        break;
                }
                break;
            }
            int kind = delta.getKind();
            if (kind == IResourceDelta.ADDED || kind == IResourceDelta.REMOVED || (kind == IResourceDelta.CHANGED && (delta.getFlags() & IResourceDelta.LOCAL_CHANGED) != 0)) {
                PackageFragmentRoot root = (PackageFragmentRoot) element;
                updateRootIndex(root, CharOperation.NO_STRINGS, delta);
                break;
            }
        // $FALL-THROUGH$
        case IJavaElement.PACKAGE_FRAGMENT:
            switch(delta.getKind()) {
                case IResourceDelta.CHANGED:
                    if ((delta.getFlags() & IResourceDelta.LOCAL_CHANGED) == 0)
                        break;
                // $FALL-THROUGH$
                case IResourceDelta.ADDED:
                case IResourceDelta.REMOVED:
                    IPackageFragment pkg = null;
                    if (element instanceof IPackageFragmentRoot) {
                        PackageFragmentRoot root = (PackageFragmentRoot) element;
                        pkg = root.getPackageFragment(CharOperation.NO_STRINGS);
                    } else {
                        pkg = (IPackageFragment) element;
                    }
                    RootInfo rootInfo = rootInfo(pkg.getParent().getPath(), delta.getKind());
                    boolean isSource = // if null, defaults to source
                    rootInfo == null || rootInfo.entryKind == IClasspathEntry.CPE_SOURCE;
                    IResourceDelta[] children = (IResourceDelta[]) delta.getAffectedChildren();
                    for (int i = 0, length = children.length; i < length; i++) {
                        IResourceDelta child = children[i];
                        IResource resource = child.getResource();
                        // TODO (philippe) Why do this? Every child is added anyway as the delta is walked
                        if (resource instanceof IFile) {
                            String name = resource.getName();
                            if (isSource) {
                                if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(name)) {
                                    Openable cu = (Openable) pkg.getCompilationUnit(name);
                                    updateIndex(cu, child);
                                }
                            } else if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(name)) {
                                Openable classFile = (Openable) pkg.getClassFile(name);
                                updateIndex(classFile, child);
                            }
                        }
                    }
                    break;
            }
            break;
        case IJavaElement.CLASS_FILE:
            //				}
            break;
        case IJavaElement.COMPILATION_UNIT:
            IFile file = (IFile) delta.getResource();
            switch(delta.getKind()) {
                case IResourceDelta.CHANGED:
                    // no need to index if the content has not changed
                    int flags = delta.getFlags();
                    if ((flags & IResourceDelta.CONTENT) == 0 && (flags & IResourceDelta.ENCODING) == 0)
                        break;
                // $FALL-THROUGH$
                case IResourceDelta.ADDED:
                    indexManager.addSource(file, element.getJavaProject().getPath(), getSourceElementParser(element));
                    // Clean file from secondary types cache but do not update indexing secondary type cache as it will be updated through indexing itself
                    this.manager.secondaryTypesRemoving(file, false);
                    break;
                case IResourceDelta.REMOVED:
                    indexManager.remove(Util.relativePath(file.getFullPath(), 1), element.getJavaProject().getPath());
                    // Clean file from secondary types cache and update indexing secondary type cache as indexing cannot remove secondary types from cache
                    this.manager.secondaryTypesRemoving(file, true);
                    break;
            }
    }
}
Also used : IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IndexManager(org.eclipse.jdt.internal.core.search.indexing.IndexManager) IResource(org.eclipse.core.resources.IResource) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Aggregations

IPath (org.eclipse.core.runtime.IPath)2 IndexManager (org.eclipse.jdt.internal.core.search.indexing.IndexManager)2 LinkedHashSet (java.util.LinkedHashSet)1 IFile (org.eclipse.core.resources.IFile)1 IFolder (org.eclipse.core.resources.IFolder)1 IResource (org.eclipse.core.resources.IResource)1 IResourceDelta (org.eclipse.core.resources.IResourceDelta)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaModel (org.eclipse.jdt.core.IJavaModel)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 ObjectVector (org.eclipse.jdt.internal.compiler.util.ObjectVector)1 SimpleSet (org.eclipse.jdt.internal.compiler.util.SimpleSet)1 JavaProject (org.eclipse.jdt.internal.core.JavaProject)1 IndexLocation (org.eclipse.jdt.internal.core.index.IndexLocation)1