Search in sources :

Example 16 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class JdtClasspathUriResolver method findResourceInWorkspace.

protected URI findResourceInWorkspace(IJavaProject javaProject, URI classpathUri) throws CoreException {
    if (javaProject.exists()) {
        String packagePath = classpathUri.trimSegments(1).path();
        String fullPath = classpathUri.path();
        String fileName = classpathUri.lastSegment();
        IPath filePath = new Path(fileName);
        String packageName = isEmpty(packagePath) ? "" : packagePath.substring(1).replace('/', '.');
        for (IPackageFragmentRoot packageFragmentRoot : javaProject.getAllPackageFragmentRoots()) {
            IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(packageName);
            if (packageFragment.exists()) {
                IResource packageFragmentResource = packageFragment.getResource();
                if (packageFragmentResource instanceof IContainer) {
                    IFile file = ((IContainer) packageFragmentResource).getFile(filePath);
                    if (file.exists())
                        return createPlatformResourceURI(file);
                } else {
                    // jar file or external class folder
                    if (packageFragmentRoot.isArchive()) {
                        // jar file
                        Object[] nonJavaResources = getNonJavaResources(packageFragmentRoot, packageFragment);
                        for (Object nonJavaResource : nonJavaResources) {
                            IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
                            if (fullPath.equals(jarEntryResource.getFullPath().toString())) {
                                IResource packageFragmentRootResource = packageFragmentRoot.getResource();
                                if (packageFragmentRootResource != null) {
                                    // we have a resource - use nested platform/resource
                                    URI packageFragmentRootURI = createPlatformResourceURI(packageFragmentRootResource);
                                    URI result = createArchiveURI(packageFragmentRootURI, fullPath);
                                    return result;
                                } else {
                                    // no resource - use file uri
                                    IPath packageFragmentRootPath = packageFragmentRoot.getPath();
                                    URI packageFragmentRootURI = URI.createFileURI(packageFragmentRootPath.toString());
                                    URI result = createArchiveURI(packageFragmentRootURI, fullPath);
                                    return result;
                                }
                            }
                        }
                    } else if (packageFragmentRoot.isExternal()) {
                        // external class folder
                        Object[] nonJavaResources = getNonJavaResources(packageFragmentRoot, packageFragment);
                        for (Object nonJavaResource : nonJavaResources) {
                            IJarEntryResource jarEntryResource = (IJarEntryResource) nonJavaResource;
                            if (fileName.equals(jarEntryResource.getName())) {
                                IResource packageFragmentRootResource = ((ExternalPackageFragmentRoot) packageFragmentRoot).resource();
                                IPath absolutePath = packageFragmentRootResource.getFullPath();
                                absolutePath = absolutePath.append(fullPath);
                                return createPlatformResourceURI(absolutePath);
                            }
                        }
                    }
                }
            }
        }
        // not found in a source folder - look for a resource relative to project root
        // of this project or one of its dependencies
        URI result = findResourceInProjectRoot(javaProject, classpathUri.path(), Sets.<String>newHashSet());
        if (result != null) {
            return result;
        }
    }
    return classpathUri;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) IContainer(org.eclipse.core.resources.IContainer) URI(org.eclipse.emf.common.util.URI) IResource(org.eclipse.core.resources.IResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 17 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class Storage2UriMapperJavaImpl method getUri.

/**
 * @since 2.5
 */
@Override
public URI getUri(/* @NonNull */
IStorage storage) {
    if (storage instanceof IJarEntryResource) {
        final IJarEntryResource casted = (IJarEntryResource) storage;
        IPackageFragmentRoot packageFragmentRoot = casted.getPackageFragmentRoot();
        Map<URI, IStorage> data = getAllEntries(packageFragmentRoot);
        for (Map.Entry<URI, IStorage> entry : data.entrySet()) {
            if (entry.getValue().equals(casted))
                return entry.getKey();
        }
        if (packageFragmentRoot.exists() && packageFragmentRoot.isArchive()) {
            IPath jarPath = packageFragmentRoot.getPath();
            URI jarURI;
            if (packageFragmentRoot.isExternal()) {
                jarURI = URI.createFileURI(jarPath.toOSString());
            } else {
                jarURI = URI.createPlatformResourceURI(jarPath.toString(), true);
            }
            URI result = URI.createURI("archive:" + jarURI + "!" + storage.getFullPath());
            return result;
        }
    }
    return null;
}
Also used : IPath(org.eclipse.core.runtime.IPath) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ForwardingMap(com.google.common.collect.ForwardingMap) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 18 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class JarEntryLocator method getURI.

/**
 * @return a URI for the given jarEntry, can be <code>null</code>.
 */
public URI getURI(IPackageFragmentRoot root, IJarEntryResource jarEntry, TraversalState state) {
    if (root.isArchive()) {
        URI jarURI = JarEntryURIHelper.getUriForPackageFragmentRoot(root);
        URI storageURI = URI.createURI(jarEntry.getFullPath().toString());
        return createJarURI(root.isArchive(), jarURI, storageURI);
    } else if (root instanceof ExternalPackageFragmentRoot) {
        IResource resource = ((ExternalPackageFragmentRoot) root).resource();
        IPath result = resource.getFullPath();
        for (int i = 1; i < state.getParents().size(); i++) {
            Object obj = state.getParents().get(i);
            if (obj instanceof IPackageFragment) {
                result = result.append(new Path(((IPackageFragment) obj).getElementName().replace('.', '/')));
            } else if (obj instanceof IJarEntryResource) {
                result = result.append(((IJarEntryResource) obj).getName());
            }
        }
        result = result.append(jarEntry.getName());
        return URI.createPlatformResourceURI(result.toString(), true);
    } else {
        throw new IllegalStateException("Unexpeced root type: " + root.getClass().getName());
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPath(org.eclipse.core.runtime.IPath) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) ExternalPackageFragmentRoot(org.eclipse.jdt.internal.core.ExternalPackageFragmentRoot) URI(org.eclipse.emf.common.util.URI) IResource(org.eclipse.core.resources.IResource)

Example 19 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class PackageFragmentRootWalker method traverse.

protected T traverse(IJarEntryResource jarEntry, boolean stopOnFirstResult, TraversalState state) {
    T result = null;
    if (jarEntry.isFile()) {
        result = handle(jarEntry, state);
    } else {
        state.push(jarEntry);
        IJarEntryResource[] children = jarEntry.getChildren();
        for (IJarEntryResource child : children) {
            result = traverse(child, stopOnFirstResult, state);
            if (stopOnFirstResult && result != null)
                return result;
        }
        state.pop();
    }
    return result;
}
Also used : IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource)

Example 20 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class PackageFragmentRootWalker method traverse.

protected T traverse(IPackageFragment pack, boolean stopOnFirstResult, TraversalState state) throws JavaModelException {
    T result = null;
    state.push(pack);
    IJavaElement[] children = pack.getChildren();
    for (IJavaElement iJavaElement : children) {
        if (iJavaElement instanceof IPackageFragment) {
            result = traverse((IPackageFragment) iJavaElement, stopOnFirstResult, state);
            if (stopOnFirstResult && result != null)
                return result;
        }
    }
    Object[] resources = pack.getNonJavaResources();
    for (Object object : resources) {
        if (object instanceof IJarEntryResource) {
            result = traverse((IJarEntryResource) object, stopOnFirstResult, state);
            if (stopOnFirstResult && result != null)
                return result;
        }
    }
    state.pop();
    return result;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource)

Aggregations

IJarEntryResource (org.eclipse.jdt.core.IJarEntryResource)33 IJavaElement (org.eclipse.jdt.core.IJavaElement)15 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)15 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)12 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)8 IPath (org.eclipse.core.runtime.IPath)8 IStorage (org.eclipse.core.resources.IStorage)6 URI (org.eclipse.emf.common.util.URI)5 IJavaProject (org.eclipse.jdt.core.IJavaProject)5 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)5 JavaModelException (org.eclipse.jdt.core.JavaModelException)5 ArrayList (java.util.ArrayList)4 CoreException (org.eclipse.core.runtime.CoreException)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 Path (org.eclipse.core.runtime.Path)3 Javadoc (org.eclipse.jdt.core.dom.Javadoc)3 IEditorInput (org.eclipse.ui.IEditorInput)3 Properties (java.util.Properties)2 IProject (org.eclipse.core.resources.IProject)2