Example usage for org.eclipse.jdt.core IPackageFragmentRoot getUnderlyingResource

List of usage examples for org.eclipse.jdt.core IPackageFragmentRoot getUnderlyingResource

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IPackageFragmentRoot getUnderlyingResource.

Prototype

IResource getUnderlyingResource() throws JavaModelException;

Source Link

Document

Returns the smallest underlying resource that contains this element, or null if this element is not contained in a resource.

Usage

From source file:org.springframework.ide.eclipse.core.io.EclipsePathMatchingResourcePatternResolver.java

License:Open Source License

private Resource processRawResource(IPackageFragmentRoot[] roots, Resource resource)
        throws IOException, JavaModelException {
    if (resource instanceof FileSystemResource) {
        // This can only be something in the Eclipse workspace

        // first check the location in the project that this pattern resolver is associated with (most likely path)
        Path path = new Path(((FileSystemResource) resource).getPath());
        IPath projectLocation = this.project.getLocation();
        if (projectLocation.isPrefixOf(path)) {
            int segmentsToRemove = projectLocation.segmentCount();
            IPath projectRelativePath = path.removeFirstSegments(segmentsToRemove);
            IFile file = this.project.getFile(projectRelativePath);
            if (file != null && file.exists()) {
                return new FileResource(file);
            }/*from w w  w  . j  a  v  a2  s  .  c  o  m*/
        }

        // then check the simple getFileForLocation (faster in case it is not a linked resource)
        IFile fileForLocation = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
        if (fileForLocation != null) {
            return new FileResource(fileForLocation);
        }

        // fall back to full resolution via findFilesForLocationURI
        IResource[] allResourcesFor = ResourcesPlugin.getWorkspace().getRoot()
                .findFilesForLocationURI(resource.getURI());
        for (IResource res : allResourcesFor) {
            return new FileResource((IFile) res);
        }
    } else if (resource instanceof UrlResource) {
        URL url = resource.getURL();
        String path = url.getPath();
        int ix = path.indexOf('!');
        if (ix > 0) {
            String entryName = path.substring(ix + 1);
            path = path.substring(0, ix);

            try {
                return new ExternalFile(new File(new URI(path)), entryName, project);
            } catch (URISyntaxException e) {
            }
        } else {
            IResource[] allResourcesFor = ResourcesPlugin.getWorkspace().getRoot()
                    .findFilesForLocationURI(resource.getURI());
            for (IResource res : allResourcesFor) {
                return new FileResource((IFile) res);
            }
        }
    } else if (resource instanceof ClassPathResource) {
        String path = ((ClassPathResource) resource).getPath();
        String fileName = path;
        String packageName = "";
        int ix = path.lastIndexOf('/');
        if (ix > 0) {
            fileName = path.substring(ix + 1);
            packageName = path.substring(0, ix).replace('/', '.');
        }
        if (fileName.endsWith(ClassUtils.CLASS_FILE_SUFFIX)) {
            String typeName = packageName + "." + fileName.substring(0, fileName.length() - 6);
            for (IPackageFragmentRoot root : roots) {
                Resource storage = null;

                if ("".equals(packageName) && root.exists()) {
                    storage = processClassResource(path, fileName, typeName, root, root.getChildren());
                }

                IPackageFragment packageFragment = root.getPackageFragment(packageName);
                if (storage == null && packageFragment != null && packageFragment.exists()) {
                    storage = processClassResource(path, fileName, typeName, root,
                            packageFragment.getChildren());
                }

                if (storage != null) {
                    return storage;
                }
            }
        }

        else {
            for (IPackageFragmentRoot root : roots) {
                IStorage storage = null;

                // Look in the root of the package fragment root
                if ("".equals(packageName) && root.exists()) {
                    storage = contains(root.getNonJavaResources(), fileName);
                }

                // Check the package
                IPackageFragment packageFragment = root.getPackageFragment(packageName);
                if (storage == null && packageFragment != null && packageFragment.exists()) {
                    storage = contains(packageFragment.getNonJavaResources(), fileName);
                }

                // Check the root folder in case no package exists 
                if (storage == null) {
                    IResource rootResource = root.getUnderlyingResource();
                    if (rootResource instanceof IFolder) {
                        IFile file = ((IFolder) rootResource).getFile(path);
                        if (file.exists()) {
                            storage = file;
                        }
                    }
                }

                // Found the resource in the package fragment root? -> construct usable Resource
                if (storage != null) {
                    if (storage instanceof IFile) {
                        return new FileResource((IFile) storage);
                    } else if (storage instanceof IJarEntryResource) {

                        // Workspace jar or resource
                        if (root.getResource() != null) {
                            return new StorageResource(new ZipEntryStorage((IFile) root.getResource(),
                                    ((IJarEntryResource) storage).getFullPath().toString()), project);
                        }
                        // Workspace external jar
                        else {
                            File jarFile = root.getPath().toFile();
                            return new ExternalFile(jarFile,
                                    ((IJarEntryResource) storage).getFullPath().toString(), project);
                        }
                    }
                }
            }
        }
    }
    return null;
}

From source file:org.summer.dsl.model.types.access.jdt.JdtTypeProvider.java

License:Open Source License

private boolean canLink(String typeName) {
    if (typeName == null) {
        return false;
    }/*from  w w  w  .  j  av  a  2  s.  com*/
    IndexedJvmTypeAccess indexedJvmTypeAccess = this.getIndexedJvmTypeAccess();
    if (indexedJvmTypeAccess != null && indexedJvmTypeAccess.isIndexingPhase(getResourceSet())) {
        // during indexing we don't see project local types.
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=410594
        try {
            IType type = javaProject.findType(typeName);
            if (type != null && type.exists()) {
                IResource underlyingResource = type.getUnderlyingResource();
                if (underlyingResource == null) {
                    return true;
                }
                for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                    if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        IResource srcUnderlyingResource = root.getUnderlyingResource();
                        if (srcUnderlyingResource != null
                                && srcUnderlyingResource.contains(underlyingResource)) {
                            return false;
                        }
                    }
                }
                return true;
            }
        } catch (JavaModelException e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
    }
    return true;
}

From source file:org.summer.ss.ide.builder.SsBuilderParticipant.java

License:Open Source License

protected void registerCurrentSourceFolder(Delta delta, EclipseResourceFileSystemAccess2 fileSystemAccess) {
    if (fileSystemAccess instanceof SourceRelativeFileSystemAccess) {
        try {//  www . j  a va2  s  .  c om
            Iterable<Pair<IStorage, IProject>> storages = mapper.getStorages(delta.getUri());
            for (Pair<IStorage, IProject> pair : storages) {
                IJavaProject javaProject = JavaCore.create(pair.getSecond());
                final IResource first = (IResource) pair.getFirst();
                IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
                for (IPackageFragmentRoot root : roots) {
                    if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                        final IResource underlyingResource = root.getUnderlyingResource();
                        if (underlyingResource instanceof IFolder && underlyingResource.contains(first)) {
                            ((SourceRelativeFileSystemAccess) fileSystemAccess)
                                    .setCurrentSource((IFolder) underlyingResource);
                            return;
                        }
                    }
                }
            }
        } catch (CoreException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}

From source file:org.switchyard.tools.ui.JavaUtil.java

License:Open Source License

/**
 * Returns the first resource folder in the project. If the project is a
 * maven project, the first resource folder configured will be used.
 * /*from  w  ww . java2 s. com*/
 * @param project the Java project
 * 
 * @return the resource root; may be null.
 */
public static IResource getFirstResourceRoot(IJavaProject project) {
    if (project == null) {
        return null;
    }
    try {
        IResource sourceRoot = null;
        IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(project.getProject());
        if (facade == null) {
            for (IPackageFragmentRoot frag : project.getPackageFragmentRoots()) {
                if (frag.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    sourceRoot = frag.getUnderlyingResource();
                    break;
                }
            }
        } else {
            IProject projectResource = project.getProject();
            for (IPath sourcePath : facade.getResourceLocations()) {
                sourceRoot = projectResource.findMember(sourcePath);
                if (sourceRoot != null) {
                    break;
                }
            }
        }
        return sourceRoot;
    } catch (JavaModelException e) {
        return null;
    }
}

From source file:org.teavm.eclipse.debugger.TeaVMSourceLookupParticipant.java

License:Apache License

@Override
public void sourceContainersChanged(ISourceLookupDirector director) {
    delegateContainers.clear();//from w w w  .  jav a 2 s  .  c o m
    ISourceContainer[] containers = director.getSourceContainers();
    for (int i = 0; i < containers.length; i++) {
        ISourceContainer container = containers[i];
        if (container.getType().getId().equals(ArchiveSourceContainer.TYPE_ID)) {
            IFile file = ((ArchiveSourceContainer) container).getFile();
            IProject project = file.getProject();
            IJavaProject javaProject = JavaCore.create(project);
            if (javaProject.exists()) {
                try {
                    IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
                    for (int j = 0; j < roots.length; j++) {
                        IPackageFragmentRoot root = roots[j];
                        if (file.equals(root.getUnderlyingResource())) {
                            delegateContainers.put(container, new PackageFragmentRootSourceContainer(root));
                        } else {
                            IPath path = root.getSourceAttachmentPath();
                            if (path != null) {
                                if (file.getFullPath().equals(path)) {
                                    delegateContainers.put(container,
                                            new PackageFragmentRootSourceContainer(root));
                                }
                            }
                        }
                    }
                } catch (JavaModelException e) {
                }
            }
        }
    }
}