Search in sources :

Example 91 with IResource

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

the class JavaSearchScope method packageFragmentRoot.

/**
     * @see AbstractJavaSearchScope#packageFragmentRoot(String, int, String)
     */
public IPackageFragmentRoot packageFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPath) {
    int index = -1;
    boolean isJarFile = jarSeparatorIndex != -1;
    if (isJarFile) {
        // internal or external jar (case 3, 4, or 5)
        String relativePath = resourcePathString.substring(jarSeparatorIndex + 1);
        index = indexOf(jarPath, relativePath);
    } else {
        // resource in workspace (case 1 or 2)
        index = indexOf(resourcePathString);
    }
    if (index >= 0) {
        int idx = this.projectIndexes[index];
        String projectPath = idx == -1 ? null : (String) this.projectPaths.get(idx);
        if (projectPath != null) {
            IJavaProject project = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath));
            if (isJarFile) {
                IResource resource = JavaModel.getWorkspaceTarget(new Path(jarPath));
                if (resource != null)
                    return project.getPackageFragmentRoot(resource);
                return project.getPackageFragmentRoot(jarPath);
            }
            Object target = JavaModel.getWorkspaceTarget(new Path(this.containerPaths[index] + '/' + this.relativePaths[index]));
            if (target != null) {
                if (target instanceof IProject) {
                    return project.getPackageFragmentRoot((IProject) target);
                }
                IJavaElement element = JavaModelManager.create((IResource) target, project);
                return (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            }
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) IJavaProject(org.eclipse.jdt.core.IJavaProject) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 92 with IResource

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

the class JavaSearchScope method add.

/**
     * Add an element to the java search scope.
     * @param element The element we want to add to current java search scope
     * @throws JavaModelException May happen if some Java Model info are not available
     */
public void add(IJavaElement element) throws JavaModelException {
    IPath containerPath = null;
    String containerPathToString = null;
    PackageFragmentRoot root = null;
    int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES;
    switch(element.getElementType()) {
        case IJavaElement.JAVA_MODEL:
            // a workspace sope should be used
            break;
        case IJavaElement.JAVA_PROJECT:
            add((JavaProject) element, null, includeMask, new HashSet(2), new HashSet(2), null);
            break;
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            root = (PackageFragmentRoot) element;
            IPath rootPath = root.internalPath();
            containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : rootPath;
            containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
            IResource rootResource = root.resource();
            String projectPath = root.getJavaProject().getPath().toString();
            if (rootResource != null && rootResource.isAccessible()) {
                String relativePath = Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount());
                add(projectPath, relativePath, containerPathToString, false, /*not a package*/
                null);
            } else {
                //$NON-NLS-1$
                add(projectPath, "", containerPathToString, false, /*not a package*/
                null);
            }
            break;
        case IJavaElement.PACKAGE_FRAGMENT:
            root = (PackageFragmentRoot) element.getParent();
            projectPath = root.getJavaProject().getPath().toString();
            if (root.isArchive()) {
                String relativePath = Util.concatWith(((PackageFragment) element).names, '/');
                containerPath = root.getPath();
                containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
                add(projectPath, relativePath, containerPathToString, true, /*package*/
                null);
            } else {
                IResource resource = ((JavaElement) element).resource();
                if (resource != null) {
                    if (resource.isAccessible()) {
                        containerPath = root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.internalPath();
                    } else {
                        // for working copies, get resource container full path
                        containerPath = resource.getParent().getFullPath();
                    }
                    containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
                    String relativePath = Util.relativePath(resource.getFullPath(), containerPath.segmentCount());
                    add(projectPath, relativePath, containerPathToString, true, /*package*/
                    null);
                }
            }
            break;
        default:
            // remember sub-cu (or sub-class file) java elements
            if (element instanceof IMember) {
                if (this.elements == null) {
                    this.elements = new ArrayList();
                }
                this.elements.add(element);
            }
            root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            projectPath = root.getJavaProject().getPath().toString();
            String relativePath;
            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                containerPath = root.getParent().getPath();
                relativePath = Util.relativePath(getPath(element, false), root.getJavaProject().getPath().segmentCount());
            } else {
                containerPath = root.internalPath();
                relativePath = getPath(element, true).toString();
            }
            containerPathToString = containerPath.getDevice() == null ? containerPath.toString() : containerPath.toOSString();
            add(projectPath, relativePath, containerPathToString, false, /*not a package*/
            null);
    }
    if (root != null)
        addEnclosingProjectOrJar(root.getKind() == IPackageFragmentRoot.K_SOURCE ? root.getParent().getPath() : root.getPath());
}
Also used : PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaElement(org.eclipse.jdt.internal.core.JavaElement) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) IMember(org.eclipse.jdt.core.IMember) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 93 with IResource

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

the class JavaModelManager method resolveVariableEntry.

public IClasspathEntry resolveVariableEntry(IClasspathEntry entry, boolean usePreviousSession) {
    if (entry.getEntryKind() != IClasspathEntry.CPE_VARIABLE)
        return entry;
    IPath resolvedPath = getResolvedVariablePath(entry.getPath(), usePreviousSession);
    if (resolvedPath == null)
        return null;
    // By passing a null reference path, we keep it relative to workspace root.
    resolvedPath = ClasspathEntry.resolveDotDot(null, resolvedPath);
    Object target = org.eclipse.jdt.internal.core.JavaModel.getTarget(resolvedPath, false);
    if (target == null)
        return null;
    // inside the workspace
    if (target instanceof IResource) {
        IResource resolvedResource = (IResource) target;
        switch(resolvedResource.getType()) {
            case IResource.PROJECT:
                // internal project
                return JavaCore.newProjectEntry(resolvedPath, entry.getAccessRules(), entry.combineAccessRules(), entry.getExtraAttributes(), entry.isExported());
            case IResource.FILE:
                // internal binary archive
                return JavaCore.newLibraryEntry(resolvedPath, getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession), getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession), entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
            case IResource.FOLDER:
                // internal binary folder
                return JavaCore.newLibraryEntry(resolvedPath, getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession), getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession), entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
        }
    }
    if (target instanceof File) {
        File externalFile = JavaModel.getFile(target);
        if (externalFile != null) {
            // external binary archive
            return JavaCore.newLibraryEntry(resolvedPath, getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession), getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession), entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
        } else {
            // non-existing file
            if (resolvedPath.isAbsolute()) {
                return JavaCore.newLibraryEntry(resolvedPath, getResolvedVariablePath(entry.getSourceAttachmentPath(), usePreviousSession), getResolvedVariablePath(entry.getSourceAttachmentRootPath(), usePreviousSession), entry.getAccessRules(), entry.getExtraAttributes(), entry.isExported());
            }
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) HashtableOfArrayToObject(org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject) IClassFile(org.eclipse.jdt.core.IClassFile) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource)

Example 94 with IResource

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

the class JavaProject method computePackageFragmentRoots.

/**
     * Returns the package fragment roots identified by the given entry. In case it refers to
     * a project, it will follow its classpath so as to find exported roots as well.
     * Only works with resolved entry
     *
     * @param resolvedEntry
     *         IClasspathEntry
     * @param accumulatedRoots
     *         ObjectVector
     * @param rootIDs
     *         HashSet
     * @param referringEntry
     *         the CP entry (project) referring to this entry, or null if initial project
     * @param retrieveExportedRoots
     *         boolean
     * @throws JavaModelException
     */
public void computePackageFragmentRoots(IClasspathEntry resolvedEntry, ObjectVector accumulatedRoots, HashSet rootIDs, IClasspathEntry referringEntry, boolean retrieveExportedRoots, Map rootToResolvedEntries) throws JavaModelException {
    String rootID = ((ClasspathEntry) resolvedEntry).rootID();
    if (rootIDs.contains(rootID))
        return;
    IPath projectPath = this.project.getFullPath();
    IPath entryPath = resolvedEntry.getPath();
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IPackageFragmentRoot root = null;
    switch(resolvedEntry.getEntryKind()) {
        // source folder
        case IClasspathEntry.CPE_SOURCE:
            if (projectPath.isPrefixOf(entryPath)) {
                Object target = getTarget(entryPath, true);
                if (target == null)
                    return;
                if (target instanceof IFolder || target instanceof IProject) {
                    root = getPackageFragmentRoot((IResource) target);
                }
            }
            break;
        // internal/external JAR or folder
        case IClasspathEntry.CPE_LIBRARY:
            if (referringEntry != null && !resolvedEntry.isExported())
                return;
            Object target = getTarget(entryPath, true);
            if (target == null)
                return;
            if (target instanceof IResource) {
                //                    // internal target
                root = getPackageFragmentRoot((IResource) target, entryPath);
            } else if (target instanceof File) {
                // external target
                if (isFile(target)) {
                    root = new JarPackageFragmentRoot(entryPath, this);
                } else if (((File) target).isDirectory()) {
                    //                        root = new ExternalPackageFragmentRoot(entryPath, this);
                    throw new UnsupportedOperationException();
                }
            }
            break;
        // recurse into required project
        case IClasspathEntry.CPE_PROJECT:
            if (!retrieveExportedRoots)
                return;
            if (referringEntry != null && !resolvedEntry.isExported())
                return;
            IResource member = workspaceRoot.findMember(entryPath);
            if (member != null && member.getType() == IResource.PROJECT) {
                // double check if bound to project (23977)
                IProject requiredProjectRsc = (IProject) member;
                if (org.eclipse.jdt.internal.core.JavaProject.hasJavaNature(requiredProjectRsc)) {
                    // special builder binary output
                    rootIDs.add(rootID);
                    org.eclipse.jdt.internal.core.JavaProject requiredProject = (org.eclipse.jdt.internal.core.JavaProject) JavaCore.create(requiredProjectRsc);
                    requiredProject.computePackageFragmentRoots(requiredProject.getResolvedClasspath(), accumulatedRoots, rootIDs, rootToResolvedEntries == null ? resolvedEntry : ((ClasspathEntry) resolvedEntry).combineWith((ClasspathEntry) referringEntry), // only combine if need to build the reverse map
                    retrieveExportedRoots, rootToResolvedEntries);
                }
                break;
            }
    }
    if (root != null) {
        accumulatedRoots.add(root);
        rootIDs.add(rootID);
        if (rootToResolvedEntries != null)
            rootToResolvedEntries.put(root, ((ClasspathEntry) resolvedEntry).combineWith((ClasspathEntry) referringEntry));
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IProject(org.eclipse.core.resources.IProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) JavaModelManager.isFile(org.eclipse.jdt.internal.core.JavaModelManager.isFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 95 with IResource

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

the class DeltaProcessor method popUntilPrefixOf.

private void popUntilPrefixOf(IPath path) {
    while (this.currentElement != null) {
        IPath currentElementPath = null;
        if (this.currentElement instanceof IPackageFragmentRoot) {
            currentElementPath = ((IPackageFragmentRoot) this.currentElement).getPath();
        } else {
            IResource currentElementResource = this.currentElement.resource();
            if (currentElementResource != null) {
                currentElementPath = currentElementResource.getFullPath();
            }
        }
        if (currentElementPath != null) {
            if (this.currentElement instanceof IPackageFragment && ((IPackageFragment) this.currentElement).isDefaultPackage() && currentElementPath.segmentCount() != path.segmentCount() - 1) {
                // default package and path is not a direct child
                this.currentElement = (Openable) this.currentElement.getParent();
            }
            if (currentElementPath.isPrefixOf(path)) {
                return;
            }
        }
        this.currentElement = (Openable) this.currentElement.getParent();
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPath(org.eclipse.core.runtime.IPath) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

IResource (org.eclipse.core.resources.IResource)559 CoreException (org.eclipse.core.runtime.CoreException)141 IFile (org.eclipse.core.resources.IFile)140 IPath (org.eclipse.core.runtime.IPath)119 IProject (org.eclipse.core.resources.IProject)104 IContainer (org.eclipse.core.resources.IContainer)88 ArrayList (java.util.ArrayList)84 IFolder (org.eclipse.core.resources.IFolder)64 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)52 IStatus (org.eclipse.core.runtime.IStatus)47 IOException (java.io.IOException)46 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)44 Path (org.eclipse.core.runtime.Path)42 File (java.io.File)39 Status (org.eclipse.core.runtime.Status)38 IJavaProject (org.eclipse.jdt.core.IJavaProject)31 IJavaElement (org.eclipse.jdt.core.IJavaElement)30 ISelection (org.eclipse.jface.viewers.ISelection)26 List (java.util.List)25 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)22