Search in sources :

Example 16 with JavaProject

use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.

the class IndexAllProject method execute.

/**
     * Ensure consistency of a project index. Need to walk all nested resources,
     * and discover resources which have either been changed, added or deleted
     * since the index was produced.
     */
public boolean execute(IProgressMonitor progressMonitor) {
    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled())
        return true;
    // nothing to do
    if (!this.project.isAccessible())
        return true;
    ReadWriteMonitor monitor = null;
    try {
        // Get source folder entries. Libraries are done as a separate job
        JavaProject javaProject = (JavaProject) JavaCore.create(this.project);
        // Do not create marker while getting raw classpath (see bug 41859)
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        int length = entries.length;
        IClasspathEntry[] sourceEntries = new IClasspathEntry[length];
        int sourceEntriesNumber = 0;
        for (int i = 0; i < length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE)
                sourceEntries[sourceEntriesNumber++] = entry;
        }
        if (sourceEntriesNumber == 0) {
            IPath projectPath = javaProject.getPath();
            for (int i = 0; i < length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(projectPath)) {
                    // the project is also a library folder (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=89815)
                    // ensure a job exists to index it as a binary folder
                    this.manager.indexLibrary(projectPath, this.project, ((ClasspathEntry) entry).getLibraryIndexLocation());
                    return true;
                }
            }
            // nothing to index but want to save an empty index file so its not 'rebuilt' when part of a search request
            Index index = this.manager.getIndexForUpdate(this.containerPath, true, /*reuse index file*/
            true);
            if (index != null)
                this.manager.saveIndex(index);
            return true;
        }
        if (sourceEntriesNumber != length)
            System.arraycopy(sourceEntries, 0, sourceEntries = new IClasspathEntry[sourceEntriesNumber], 0, sourceEntriesNumber);
        Index index = this.manager.getIndexForUpdate(this.containerPath, true, /*reuse index file*/
        true);
        if (index == null)
            return true;
        monitor = index.monitor;
        // index got deleted since acquired
        if (monitor == null)
            return true;
        // ask permission to read
        monitor.enterRead();
        // all file names //$NON-NLS-1$
        String[] paths = index.queryDocumentNames("");
        int max = paths == null ? 0 : paths.length;
        final SimpleLookupTable indexedFileNames = new SimpleLookupTable(max == 0 ? 33 : max + 11);
        //$NON-NLS-1$
        final String OK = "OK";
        //$NON-NLS-1$
        final String DELETED = "DELETED";
        if (paths != null) {
            for (int i = 0; i < max; i++) indexedFileNames.put(paths[i], DELETED);
        }
        final long indexLastModified = max == 0 ? 0L : index.getIndexLastModified();
        IWorkspaceRoot root = this.project.getWorkspace().getRoot();
        for (int i = 0; i < sourceEntriesNumber; i++) {
            if (this.isCancelled)
                return false;
            IClasspathEntry entry = sourceEntries[i];
            IResource sourceFolder = root.findMember(entry.getPath());
            if (sourceFolder != null) {
                // collect output locations if source is project (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=32041)
                final HashSet outputs = new HashSet();
                if (sourceFolder.getType() == IResource.PROJECT) {
                    // Do not create marker while getting output location (see bug 41859)
                    outputs.add(javaProject.getOutputLocation());
                    for (int j = 0; j < sourceEntriesNumber; j++) {
                        IPath output = sourceEntries[j].getOutputLocation();
                        if (output != null) {
                            outputs.add(output);
                        }
                    }
                }
                final boolean hasOutputs = !outputs.isEmpty();
                final char[][] inclusionPatterns = ((ClasspathEntry) entry).fullInclusionPatternChars();
                final char[][] exclusionPatterns = ((ClasspathEntry) entry).fullExclusionPatternChars();
                if (max == 0) {
                    sourceFolder.accept(new IResourceProxyVisitor() {

                        public boolean visit(IResourceProxy proxy) {
                            if (IndexAllProject.this.isCancelled)
                                return false;
                            switch(proxy.getType()) {
                                case IResource.FILE:
                                    if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) {
                                        IFile file = (IFile) proxy.requestResource();
                                        if (exclusionPatterns != null || inclusionPatterns != null)
                                            if (Util.isExcluded(file, inclusionPatterns, exclusionPatterns))
                                                return false;
                                        indexedFileNames.put(Util.relativePath(file.getFullPath(), project.getFullPath().segmentCount()), file);
                                    }
                                    return false;
                                case IResource.FOLDER:
                                    if (exclusionPatterns != null && inclusionPatterns == null) {
                                        // if there are inclusion patterns then we must walk the children
                                        if (Util.isExcluded(proxy.requestFullPath(), inclusionPatterns, exclusionPatterns, true))
                                            return false;
                                    }
                                    if (hasOutputs && outputs.contains(proxy.requestFullPath()))
                                        return false;
                            }
                            return true;
                        }
                    }, IResource.NONE);
                } else {
                    sourceFolder.accept(new IResourceProxyVisitor() {

                        public boolean visit(IResourceProxy proxy) throws CoreException {
                            if (IndexAllProject.this.isCancelled)
                                return false;
                            switch(proxy.getType()) {
                                case IResource.FILE:
                                    if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) {
                                        IFile file = (IFile) proxy.requestResource();
                                        URI location = file.getLocationURI();
                                        if (location == null)
                                            return false;
                                        if (exclusionPatterns != null || inclusionPatterns != null)
                                            if (Util.isExcluded(file, inclusionPatterns, exclusionPatterns))
                                                return false;
                                        String relativePathString = Util.relativePath(file.getFullPath(), project.getFullPath().segmentCount());
                                        indexedFileNames.put(relativePathString, indexedFileNames.get(relativePathString) == null || indexLastModified < EFS.getStore(location).fetchInfo().getLastModified() ? (Object) file : (Object) OK);
                                    }
                                    return false;
                                case IResource.FOLDER:
                                    if (exclusionPatterns != null || inclusionPatterns != null)
                                        if (Util.isExcluded(proxy.requestResource(), inclusionPatterns, exclusionPatterns))
                                            return false;
                                    if (hasOutputs && outputs.contains(proxy.requestFullPath()))
                                        return false;
                            }
                            return true;
                        }
                    }, IResource.NONE);
                }
            }
        }
        SourceElementParser parser = this.manager.getSourceElementParser(javaProject, null);
        Object[] names = indexedFileNames.keyTable;
        Object[] values = indexedFileNames.valueTable;
        for (int i = 0, namesLength = names.length; i < namesLength; i++) {
            String name = (String) names[i];
            if (name != null) {
                if (this.isCancelled)
                    return false;
                Object value = values[i];
                if (value != OK) {
                    if (value == DELETED)
                        this.manager.remove(name, this.containerPath);
                    else
                        this.manager.addSource((IFile) value, this.containerPath, parser);
                }
            }
        }
        // request to save index when all cus have been indexed... also sets state to SAVED_STATE
        this.manager.request(new SaveIndex(this.containerPath, this.manager));
    } catch (CoreException e) {
        if (JobManager.VERBOSE) {
            Util.verbose("-> failed to index " + this.project + " because of the following exception:", //$NON-NLS-1$ //$NON-NLS-2$
            System.err);
            e.printStackTrace();
        }
        this.manager.removeIndex(this.containerPath);
        return false;
    } catch (IOException e) {
        if (JobManager.VERBOSE) {
            Util.verbose("-> failed to index " + this.project + " because of the following exception:", //$NON-NLS-1$ //$NON-NLS-2$
            System.err);
            e.printStackTrace();
        }
        this.manager.removeIndex(this.containerPath);
        return false;
    } finally {
        if (monitor != null)
            // free read lock
            monitor.exitRead();
    }
    return true;
}
Also used : IFile(org.eclipse.core.resources.IFile) IResourceProxyVisitor(org.eclipse.core.resources.IResourceProxyVisitor) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Index(org.eclipse.jdt.internal.core.index.Index) URI(java.net.URI) SourceElementParser(org.eclipse.jdt.internal.compiler.SourceElementParser) IResourceProxy(org.eclipse.core.resources.IResourceProxy) ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) HashSet(java.util.HashSet) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IPath(org.eclipse.core.runtime.IPath) SimpleLookupTable(org.eclipse.jdt.internal.compiler.util.SimpleLookupTable) IOException(java.io.IOException) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) IResource(org.eclipse.core.resources.IResource)

Example 17 with JavaProject

use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.

the class IndexManager method indexAll.

/**
     * Trigger addition of the entire content of a project
     * Note: the actual operation is performed in background
     */
public void indexAll(IProject project) {
    // determine the new children
    try {
        JavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
        JavaProject javaProject = (JavaProject) model.getJavaProject(project);
        // only consider immediate libraries - each project will do the same
        // NOTE: force to resolve CP variables before calling indexer - 19303, so that initializers
        // will be run in the current thread.
        IClasspathEntry[] entries = javaProject.getResolvedClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
                indexLibrary(entry.getPath(), project, ((ClasspathEntry) entry).getLibraryIndexLocation());
        }
    } catch (JavaModelException e) {
    // cannot retrieve classpath info
    }
    // check if the same request is not already in the queue
    IndexRequest request = new IndexAllProject(project, this);
    if (!isJobWaiting(request))
        request(request);
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) JavaModel(org.eclipse.jdt.internal.core.JavaModel) ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 18 with JavaProject

use of org.eclipse.jdt.internal.core.JavaProject in project che by eclipse.

the class JavaSearchScope method add.

/**
     * Add a path to current java search scope or all project fragment roots if null.
     * Use project resolved classpath to retrieve and store access restriction on each classpath entry.
     * Recurse if dependent projects are found.
     * @param javaProject Project used to get resolved classpath entries
     * @param pathToAdd Path to add in case of single element or null if user want to add all project package fragment roots
     * @param includeMask Mask to apply on classpath entries
     * @param projectsToBeAdded Set to avoid infinite recursion
     * @param visitedProjects Set to avoid adding twice the same project
     * @param referringEntry Project raw entry in referring project classpath
     * @throws JavaModelException May happen while getting java model info
     */
void add(JavaProject javaProject, IPath pathToAdd, int includeMask, HashSet projectsToBeAdded, HashSet visitedProjects, IClasspathEntry referringEntry) throws JavaModelException {
    IProject project = javaProject.getProject();
    if (!project.isAccessible() || !visitedProjects.add(project))
        return;
    IPath projectPath = project.getFullPath();
    String projectPathString = projectPath.toString();
    addEnclosingProjectOrJar(projectPath);
    IClasspathEntry[] entries = javaProject.getResolvedClasspath();
    IJavaModel model = javaProject.getJavaModel();
    JavaModelManager.PerProjectInfo perProjectInfo = javaProject.getPerProjectInfo();
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        AccessRuleSet access = null;
        ClasspathEntry cpEntry = (ClasspathEntry) entry;
        if (referringEntry != null) {
            // Source folder are implicitly exported.
            if (!entry.isExported() && entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
                continue;
            }
            cpEntry = cpEntry.combineWith((ClasspathEntry) referringEntry);
        //				cpEntry = ((ClasspathEntry)referringEntry).combineWith(cpEntry);
        }
        access = cpEntry.getAccessRuleSet();
        switch(entry.getEntryKind()) {
            case IClasspathEntry.CPE_LIBRARY:
                IClasspathEntry rawEntry = null;
                Map rootPathToRawEntries = perProjectInfo.rootPathToRawEntries;
                if (rootPathToRawEntries != null) {
                    rawEntry = (IClasspathEntry) rootPathToRawEntries.get(entry.getPath());
                }
                if (rawEntry == null)
                    break;
                rawKind: switch(rawEntry.getEntryKind()) {
                    case IClasspathEntry.CPE_LIBRARY:
                    case IClasspathEntry.CPE_VARIABLE:
                        if ((includeMask & APPLICATION_LIBRARIES) != 0) {
                            IPath path = entry.getPath();
                            if (pathToAdd == null || pathToAdd.equals(path)) {
                                Object target = JavaModel.getTarget(path, false);
                                if (// case of an external folder
                                target instanceof IFolder)
                                    path = ((IFolder) target).getFullPath();
                                String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                                //$NON-NLS-1$
                                add(projectPath.toString(), "", pathToString, false, /*not a package*/
                                access);
                                addEnclosingProjectOrJar(entry.getPath());
                            }
                        }
                        break;
                    case IClasspathEntry.CPE_CONTAINER:
                        IClasspathContainer container = JavaCore.getClasspathContainer(rawEntry.getPath(), javaProject);
                        if (container == null)
                            break;
                        switch(container.getKind()) {
                            case IClasspathContainer.K_APPLICATION:
                                if ((includeMask & APPLICATION_LIBRARIES) == 0)
                                    break rawKind;
                                break;
                            case IClasspathContainer.K_SYSTEM:
                            case IClasspathContainer.K_DEFAULT_SYSTEM:
                                if ((includeMask & SYSTEM_LIBRARIES) == 0)
                                    break rawKind;
                                break;
                            default:
                                break rawKind;
                        }
                        IPath path = entry.getPath();
                        if (pathToAdd == null || pathToAdd.equals(path)) {
                            Object target = JavaModel.getTarget(path, false);
                            if (// case of an external folder
                            target instanceof IFolder)
                                path = ((IFolder) target).getFullPath();
                            String pathToString = path.getDevice() == null ? path.toString() : path.toOSString();
                            //$NON-NLS-1$
                            add(projectPath.toString(), "", pathToString, false, /*not a package*/
                            access);
                            addEnclosingProjectOrJar(entry.getPath());
                        }
                        break;
                }
                break;
            case IClasspathEntry.CPE_PROJECT:
                if ((includeMask & REFERENCED_PROJECTS) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        JavaProject referencedProject = (JavaProject) model.getJavaProject(path.toOSString());
                        if (!projectsToBeAdded.contains(referencedProject)) {
                            // do not recurse if depending project was used to create the scope
                            add(referencedProject, null, includeMask, projectsToBeAdded, visitedProjects, cpEntry);
                        }
                    }
                }
                break;
            case IClasspathEntry.CPE_SOURCE:
                if ((includeMask & SOURCES) != 0) {
                    IPath path = entry.getPath();
                    if (pathToAdd == null || pathToAdd.equals(path)) {
                        add(projectPath.toString(), Util.relativePath(path, projectPath.segmentCount()), projectPathString, false, /*not a package*/
                        access);
                    }
                }
                break;
        }
    }
}
Also used : JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet) IProject(org.eclipse.core.resources.IProject) JavaModelManager(org.eclipse.jdt.internal.core.JavaModelManager) ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) Map(java.util.Map) IJavaModel(org.eclipse.jdt.core.IJavaModel) IFolder(org.eclipse.core.resources.IFolder)

Aggregations

JavaProject (org.eclipse.jdt.internal.core.JavaProject)18 IJavaProject (org.eclipse.jdt.core.IJavaProject)17 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)11 IPath (org.eclipse.core.runtime.IPath)8 Path (org.eclipse.core.runtime.Path)8 IJavaElement (org.eclipse.jdt.core.IJavaElement)8 IProject (org.eclipse.core.resources.IProject)7 MavenServerManagerTest (org.eclipse.che.plugin.maven.server.rmi.MavenServerManagerTest)6 Test (org.testng.annotations.Test)6 JsonObject (com.google.gson.JsonObject)4 Provider (com.google.inject.Provider)4 File (java.io.File)4 RemoteException (java.rmi.RemoteException)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 List (java.util.List)4 ListIterator (java.util.ListIterator)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4