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.eclipse.xtend.typesystem.emf.ui.ProjectAnalyzer.java

License:Open Source License

private void loadMetamodelsForProject(final IJavaProject javaProject, final ResourceSet rs,
        final IProgressMonitor monitor) {
    try {//from  www .  j ava2 s  .c o m
        final String ext = Messages.ProjectAnalyzer_2;
        if (!javaProject.exists())
            return;
        for (final IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
            if (!root.isArchive()) {
                IResource rootResource = null;
                if (root instanceof ExternalPackageFragmentRoot) {
                    rootResource = ((ExternalPackageFragmentRoot) root).resource();
                } else {
                    rootResource = root.getUnderlyingResource();
                }
                if (rootResource != null) {
                    try {
                        // if (!rootResource.exists() || !rootResource.isAccessible())
                        rootResource.refreshLocal(IResource.DEPTH_INFINITE, monitor);
                        rootResource.accept(new IResourceVisitor() {
                            public boolean visit(final IResource resource) throws CoreException {
                                if (resource instanceof IFile
                                        && ext.equals(((IFile) resource).getFileExtension())) {
                                    loadModelFromStorage(rs, (IFile) resource);
                                }
                                return true;
                            }
                        });
                    } catch (final CoreException e) {
                        EmfToolsLog.logError(e);
                    }
                }
            } else {
                // skip JRE jars
                if (((JarPackageFragmentRoot) root).getPath().toString().contains(Messages.ProjectAnalyzer_3)) {
                    if (EmfToolsPlugin.trace) {
                        System.out.println(Messages.ProjectAnalyzer_4
                                + ((JarPackageFragmentRoot) root).getPath().toString());
                    }
                    continue;
                }

                root.open(monitor);
                try {
                    final ZipFile zip = ((JarPackageFragmentRoot) root).getJar();

                    final Enumeration<? extends ZipEntry> entries = zip.entries();
                    while (entries.hasMoreElements()) {
                        final ZipEntry entry = entries.nextElement();
                        final String name = entry.getName();
                        if (name.endsWith(ext)) {
                            final String fqn = name.substring(0, name.length() - ext.length() - 1)
                                    .replaceAll(Messages.ProjectAnalyzer_5, Messages.ProjectAnalyzer_6);
                            final ResourceID resourceID = new ResourceID(fqn, ext);
                            final IStorage findStorage = JDTUtil.loadFromJar(resourceID, root);
                            if (findStorage != null) {
                                loadModelFromStorage(rs, findStorage);
                            }
                        }
                    }
                } catch (final CoreException e) {
                    EmfToolsLog.logError(e);
                } finally {
                    root.close();
                }
            }
        }
    } catch (final JavaModelException e) {
        EmfToolsLog.logError(e);
    }
}

From source file:org.eclipse.xtext.common.types.access.jdt.JdtTypeProvider.java

License:Open Source License

/**
 * @since 2.9/*from  ww w. j av a  2  s.c om*/
 */
protected boolean canLink(IType type) throws JavaModelException {
    IndexedJvmTypeAccess indexedJvmTypeAccess = this.getIndexedJvmTypeAccess();
    if (indexedJvmTypeAccess != null && indexedJvmTypeAccess.isIndexingPhase(getResourceSet())) {
        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;
    }
    return true;
}

From source file:org.eclipse.xtext.ui.containers.JavaProjectsStateHelper.java

License:Open Source License

protected IPackageFragmentRoot getJavaElement(final IFile file) {
    IJavaProject jp = JavaCore.create(file.getProject());
    if (!jp.exists())
        return null;
    IPackageFragmentRoot[] roots;/*  w  w w .  j av  a 2 s  .com*/
    try {
        roots = jp.getPackageFragmentRoots();
        for (IPackageFragmentRoot root : roots) {
            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                IResource resource2 = root.getUnderlyingResource();
                if (resource2.contains(file))
                    return root;
            }
        }
    } catch (JavaModelException e) {
        if (!e.isDoesNotExist())
            log.error(e.getMessage(), e);
    }
    return null;
}

From source file:org.eclipse.xtext.ui.resource.PackageFragmentRootWalker.java

License:Open Source License

private boolean existsPhysically(IPackageFragmentRoot root) throws JavaModelException {
    if (root.isExternal())
        return root.getPath().toFile().exists();
    else/*from w  w  w . j a v  a2 s  .  c om*/
        return root.getUnderlyingResource().exists();
}

From source file:org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl.java

License:Open Source License

/**
 * @since 2.4/*  www. j  av a  2s .co  m*/
 */
@Override
public Pair<URI, URI> getURIMapping(IPackageFragmentRoot root) throws JavaModelException {
    PackageFragmentRootData data = getData(root);
    if (data.uriPrefix == null)
        return null;
    IPath path = root.isExternal() ? root.getPath() : root.getUnderlyingResource().getLocation();
    URI physical = null;
    if (root.isArchive()) {
        String archiveScheme = "zip".equalsIgnoreCase(root.getPath().getFileExtension()) ? "zip" : "jar";
        physical = URI.createURI(archiveScheme + ":file:" + path.toFile().getPath() + "!/");
    } else {
        physical = URI.createFileURI(path.toFile().getPath() + "/");
    }
    return Tuples.create(data.uriPrefix, physical);
}

From source file:org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl.java

License:Open Source License

/**
 * @since 2.4//ww w .  j a v a 2 s .co  m
 */
@Override
public Map<URI, IStorage> getAllEntries(IPackageFragmentRoot root) {
    try {
        IResource underlyingResource = root.getUnderlyingResource();
        if (underlyingResource instanceof IFolder) {
            return host.getAllEntries((IFolder) underlyingResource);
        }
    } catch (JavaModelException e) {
        if (!e.isDoesNotExist())
            log.error(e.getMessage(), e);
        return emptyMap();
    }
    PackageFragmentRootData data = getData(root);
    return data.uri2Storage;
}

From source file:org.eclipse.xtext.ui.resource.Storage2UriMapperJavaImpl.java

License:Open Source License

private Object computeModificationStamp(IPackageFragmentRoot root) {
    try {//from w ww  .  j  a  v  a  2  s.  c  o m
        if (root.exists()) {
            IResource resource = root.getUnderlyingResource();
            if (resource != null) {
                Object result = getLastModified(resource);
                if (result != null) {
                    return result;
                }
            }
            return root.getPath().toFile().lastModified();
        }
    } catch (CoreException e) {
        log.error(e.getMessage(), e);
    }
    return new Object();
}

From source file:org.jboss.mapper.eclipse.internal.util.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.
 *
 * @param project the Java project//w w  w  .  java2s .c  om
 *
 * @return the resource root; may be null.
 */
public static IResource getFirstResourceRoot(IJavaProject project) {
    if (project == null) {
        return null;
    }
    try {
        IResource sourceRoot = null;
        for (IPackageFragmentRoot frag : project.getPackageFragmentRoots()) {
            if (frag.getKind() == IPackageFragmentRoot.K_SOURCE) {
                sourceRoot = frag.getUnderlyingResource();
                break;
            }
        }
        return sourceRoot;
    } catch (JavaModelException e) {
        return null;
    }
}

From source file:org.lunifera.dsl.entity.xtext.ui.type.EntityJdtTypeProvider.java

License:Open Source License

private boolean canLink(String typeName) {
    if (typeName == null) {
        return false;
    }/*from   ww  w.  java2s .c om*/
    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 = getJavaProject().findType(typeName);
            if (type != null && type.exists()) {
                String enabled = System.getProperty("lunifera.linker.constraint", "true");
                if (Boolean.valueOf(enabled)) {
                    IResource underlyingResource = type.getUnderlyingResource();
                    if (underlyingResource == null) {
                        return true;
                    }
                    for (IPackageFragmentRoot root : getJavaProject().getPackageFragmentRoots()) {
                        if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                            IResource srcUnderlyingResource = root.getUnderlyingResource();
                            if (srcUnderlyingResource != null
                                    && srcUnderlyingResource.contains(underlyingResource)) {
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
        } catch (JavaModelException e) {
            LOGGER.error(e.getMessage(), e);
        }
        return false;
    }
    return true;
}

From source file:org.sf.feeling.decompiler.editor.DecompilerSourceMapper.java

License:Open Source License

protected String getArchivePath(IPackageFragmentRoot root) {
    String archivePath = null;/*w  ww.ja  va 2s.c  o m*/
    IResource resource;

    try {
        if ((resource = root.getUnderlyingResource()) != null)
            // jar in workspace
            archivePath = resource.getLocation().toOSString();
        else
            // external jar
            archivePath = root.getPath().toOSString();
    } catch (JavaModelException e) {
        throw new RuntimeException("Unexpected Java model exception: " //$NON-NLS-1$
                + e.toString());
    }
    return archivePath;
}