Search in sources :

Example 1 with IResourceProxyVisitor

use of org.eclipse.core.resources.IResourceProxyVisitor in project che by eclipse.

the class ClasspathBuilderTest method elementShouldBeAddedAsLibToClasspathFromLibFolder.

@Test
public void elementShouldBeAddedAsLibToClasspathFromLibFolder() throws Exception {
    Path jarPath = new Path(LIBRARY + "/a.jar");
    library.add(LIBRARY);
    IFolder libraryFolder1 = mock(IFolder.class);
    IResourceProxy iResourceProxy = mock(IResourceProxy.class);
    IResource iResource = mock(IResource.class);
    when(iProject.getFolder(LIBRARY)).thenReturn(libraryFolder1);
    when(libraryFolder1.exists()).thenReturn(true);
    when(iResourceProxy.getType()).thenReturn(IResource.FILE);
    when(iResourceProxy.requestFullPath()).thenReturn(jarPath);
    when(iResourceProxy.requestResource()).thenReturn(iResource);
    when(iResource.getLocation()).thenReturn(jarPath);
    classpathBuilder.generateClasspath(iJavaProject, sourceFolders, library);
    ArgumentCaptor<IResourceProxyVisitor> resourceProxyVisitorArgumentCaptor = ArgumentCaptor.forClass(IResourceProxyVisitor.class);
    verify(libraryFolder1).accept(resourceProxyVisitorArgumentCaptor.capture(), eq(IContainer.INCLUDE_PHANTOMS));
    resourceProxyVisitorArgumentCaptor.getValue().visit(iResourceProxy);
    verify(iResourceProxy).getType();
    assertEquals(jarPath, iResource.getLocation());
}
Also used : Path(org.eclipse.core.runtime.Path) IResourceProxyVisitor(org.eclipse.core.resources.IResourceProxyVisitor) IResourceProxy(org.eclipse.core.resources.IResourceProxy) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder) Test(org.testng.annotations.Test) BaseTest(org.eclipse.che.plugin.java.plain.server.BaseTest)

Example 2 with IResourceProxyVisitor

use of org.eclipse.core.resources.IResourceProxyVisitor 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 3 with IResourceProxyVisitor

use of org.eclipse.core.resources.IResourceProxyVisitor in project che by eclipse.

the class Resource method accept.

@Override
public void accept(final IResourceVisitor visitor, int depth, int memberFlags) throws CoreException {
    //use the fast visitor if visiting to infinite depth
    if (depth == IResource.DEPTH_INFINITE) {
        accept(new IResourceProxyVisitor() {

            public boolean visit(IResourceProxy proxy) throws CoreException {
                return visitor.visit(proxy.requestResource());
            }
        }, memberFlags);
        return;
    }
    // it is invalid to call accept on a phantom when INCLUDE_PHANTOMS is not specified
    final boolean includePhantoms = (memberFlags & IContainer.INCLUDE_PHANTOMS) != 0;
    ResourceInfo info = getResourceInfo(includePhantoms, false);
    int flags = getFlags(info);
    if ((memberFlags & IContainer.DO_NOT_CHECK_EXISTENCE) == 0)
        checkAccessible(flags);
    //check that this resource matches the member flags
    if (!isMember(flags, memberFlags))
        return;
    // visit this resource
    if (!visitor.visit(this) || depth == DEPTH_ZERO)
        return;
    // get the info again because it might have been changed by the visitor
    info = getResourceInfo(includePhantoms, false);
    if (info == null)
        return;
    // thread safety: (cache the type to avoid changes -- we might not be inside an operation)
    int type = info.getType();
    if (type == FILE)
        return;
    // if we had a gender change we need to fix up the resource before asking for its members
    IContainer resource = getType() != type ? (IContainer) workspace.newResource(getFullPath(), type) : (IContainer) this;
    IResource[] members = resource.members(memberFlags);
    for (int i = 0; i < members.length; i++) members[i].accept(visitor, DEPTH_ZERO, memberFlags | IContainer.DO_NOT_CHECK_EXISTENCE);
}
Also used : IResourceProxyVisitor(org.eclipse.core.resources.IResourceProxyVisitor) CoreException(org.eclipse.core.runtime.CoreException) IResourceProxy(org.eclipse.core.resources.IResourceProxy) IContainer(org.eclipse.core.resources.IContainer) IResource(org.eclipse.core.resources.IResource)

Example 4 with IResourceProxyVisitor

use of org.eclipse.core.resources.IResourceProxyVisitor in project che by eclipse.

the class ClasspathBuilderTest method elementShouldNotBeAddedAsLibToClasspathIfItIsNotJar.

@Test
public void elementShouldNotBeAddedAsLibToClasspathIfItIsNotJar() throws Exception {
    library.add(LIBRARY);
    IFolder libraryFolder1 = mock(IFolder.class);
    IResourceProxy iResourceProxy = mock(IResourceProxy.class);
    when(iProject.getFolder(LIBRARY)).thenReturn(libraryFolder1);
    when(libraryFolder1.exists()).thenReturn(true);
    when(iResourceProxy.getType()).thenReturn(IResource.FILE);
    when(iResourceProxy.requestFullPath()).thenReturn(new Path(LIBRARY));
    classpathBuilder.generateClasspath(iJavaProject, sourceFolders, library);
    ArgumentCaptor<IResourceProxyVisitor> resourceProxyVisitorArgumentCaptor = ArgumentCaptor.forClass(IResourceProxyVisitor.class);
    verify(libraryFolder1).accept(resourceProxyVisitorArgumentCaptor.capture(), eq(IContainer.INCLUDE_PHANTOMS));
    resourceProxyVisitorArgumentCaptor.getValue().visit(iResourceProxy);
    verify(iResourceProxy).getType();
    verifyIfOnlyJREContainerInClasspath();
}
Also used : Path(org.eclipse.core.runtime.Path) IResourceProxyVisitor(org.eclipse.core.resources.IResourceProxyVisitor) IResourceProxy(org.eclipse.core.resources.IResourceProxy) IFolder(org.eclipse.core.resources.IFolder) Test(org.testng.annotations.Test) BaseTest(org.eclipse.che.plugin.java.plain.server.BaseTest)

Example 5 with IResourceProxyVisitor

use of org.eclipse.core.resources.IResourceProxyVisitor in project che by eclipse.

the class ClasspathBuilderTest method elementShouldNotBeAddedAsLibToClasspathIfItIsFolder.

@Test
public void elementShouldNotBeAddedAsLibToClasspathIfItIsFolder() throws Exception {
    library.add(LIBRARY);
    IFolder libraryFolder1 = mock(IFolder.class);
    IResourceProxy iResourceProxy = mock(IResourceProxy.class);
    when(iProject.getFolder(LIBRARY)).thenReturn(libraryFolder1);
    when(libraryFolder1.exists()).thenReturn(true);
    when(iResourceProxy.getType()).thenReturn(IResource.FOLDER);
    classpathBuilder.generateClasspath(iJavaProject, sourceFolders, library);
    ArgumentCaptor<IResourceProxyVisitor> resourceProxyVisitorArgumentCaptor = ArgumentCaptor.forClass(IResourceProxyVisitor.class);
    verify(libraryFolder1).accept(resourceProxyVisitorArgumentCaptor.capture(), eq(IContainer.INCLUDE_PHANTOMS));
    resourceProxyVisitorArgumentCaptor.getValue().visit(iResourceProxy);
    verify(iResourceProxy).getType();
    verifyIfOnlyJREContainerInClasspath();
}
Also used : IResourceProxyVisitor(org.eclipse.core.resources.IResourceProxyVisitor) IResourceProxy(org.eclipse.core.resources.IResourceProxy) IFolder(org.eclipse.core.resources.IFolder) Test(org.testng.annotations.Test) BaseTest(org.eclipse.che.plugin.java.plain.server.BaseTest)

Aggregations

IResourceProxy (org.eclipse.core.resources.IResourceProxy)7 IResourceProxyVisitor (org.eclipse.core.resources.IResourceProxyVisitor)7 IResource (org.eclipse.core.resources.IResource)4 CoreException (org.eclipse.core.runtime.CoreException)4 BaseTest (org.eclipse.che.plugin.java.plain.server.BaseTest)3 IFolder (org.eclipse.core.resources.IFolder)3 Test (org.testng.annotations.Test)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 IFile (org.eclipse.core.resources.IFile)2 IProject (org.eclipse.core.resources.IProject)2 Path (org.eclipse.core.runtime.Path)2 BndEditModel (aQute.bnd.build.model.BndEditModel)1 ExportedPackage (aQute.bnd.build.model.clauses.ExportedPackage)1 ImportPattern (aQute.bnd.build.model.clauses.ImportPattern)1 Document (aQute.bnd.properties.Document)1 IDocument (aQute.bnd.properties.IDocument)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1