Example usage for org.eclipse.jdt.core IJavaProject exists

List of usage examples for org.eclipse.jdt.core IJavaProject exists

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject exists.

Prototype

boolean exists();

Source Link

Document

Returns whether this Java element exists in the model.

Usage

From source file:org.eclipse.xtext.common.types.ui.trace.JarEntryAwareTrace.java

License:Open Source License

@Override
public AbsoluteURI tryResolvePath(IStorage localStorage, SourceRelativeURI path) {
    if (localStorage instanceof IFile) {
        IProject project = ((IFile) localStorage).getProject();
        if (project != null) {
            IJavaProject javaProject = JavaCore.create(project);
            if (javaProject != null && javaProject.exists())
                return resolvePath(javaProject, path);
            return null;
        }/*from   w w w. j av a  2s  . c  o  m*/
    } else if (localStorage instanceof IJarEntryResource) {
        return resolvePath((IJarEntryResource) localStorage, path);
    }
    return null;
}

From source file:org.eclipse.xtext.common.types.xtext.ui.JdtAwareProjectByResourceProvider.java

License:Open Source License

@Override
public IProject getProjectContext(Resource resource) {
    IProject result = super.getProjectContext(resource);
    if (result != null) {
        return result;
    }/*from   w w w .  j  av a  2 s  . com*/
    IJavaProject javaProject = javaProjectProvider.getJavaProject(resource.getResourceSet());
    if (javaProject != null && javaProject.exists()) {
        return javaProject.getProject();
    }
    return null;
}

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;/*from   w w  w . ja  va2s. c  o m*/
    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.containers.JavaProjectsStateHelper.java

License:Open Source License

protected IPackageFragmentRoot getJarWithEntry(URI uri) {
    Iterable<Pair<IStorage, IProject>> storages = getStorages(uri);
    IPackageFragmentRoot result = null;// w w  w  . j av a  2 s .  c  o m
    for (Pair<IStorage, IProject> storage2Project : storages) {
        IStorage storage = storage2Project.getFirst();
        if (storage instanceof IJarEntryResource) {
            IPackageFragmentRoot fragmentRoot = ((IJarEntryResource) storage).getPackageFragmentRoot();
            if (fragmentRoot != null) {
                // IPackageFragmentRoot has some unexpected caching - it may return a different project
                // thus we use the one that was used to record the IPackageFragmentRoot
                IProject actualProject = storage2Project.getSecond();
                IJavaProject javaProject = JavaCore.create(actualProject);
                if (!javaProject.exists()) {
                    javaProject = fragmentRoot.getJavaProject();
                }
                if (isAccessibleXtextProject(javaProject.getProject())) {
                    // if both projects are the same - fine
                    if (javaProject.equals(fragmentRoot.getJavaProject()))
                        return fragmentRoot;
                    // otherwise re-obtain the fragment root from the real project
                    if (fragmentRoot.isExternal()) {
                        IPackageFragmentRoot actualRoot = javaProject
                                .getPackageFragmentRoot(fragmentRoot.getPath().toString());
                        if (actualProject.exists()) {
                            return actualRoot;
                        }
                    } else {
                        IPackageFragmentRoot actualRoot = javaProject
                                .getPackageFragmentRoot(fragmentRoot.getResource());
                        if (actualRoot.exists()) {
                            return actualRoot;
                        }
                    }
                    result = fragmentRoot;
                }
                if (result == null)
                    result = fragmentRoot;
            }
        }
    }
    return result;
}

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

License:Open Source License

@Override
public void initialize(ResourceSet resourceSet, IProject project) {
    if (resourceSet instanceof XtextResourceSet) {
        XtextResourceSet casted = (XtextResourceSet) resourceSet;
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject != null && javaProject.exists()) {
            casted.getURIConverter().getURIMap().putAll(computePlatformURIMap(javaProject));
            casted.setClasspathURIContext(javaProject);
            casted.setClasspathUriResolver(new JdtClasspathUriResolver());
        }/*from   w w w. j  av  a  2 s . co m*/
    }
}

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

License:Open Source License

protected Map<URI, URI> computePlatformURIMap(IJavaProject javaProject) {
    HashMap<URI, URI> hashMap = newHashMap();
    try {/*from w w  w. j  a va 2s .  com*/
        hashMap.putAll(EcorePlugin.computePlatformURIMap(false));
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
    if (!javaProject.exists())
        return hashMap;
    try {
        IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot root : roots) {
            Pair<URI, URI> uriMapping = storage2UriMapper.getURIMapping(root);
            if (uriMapping != null) {

                // we could just install the prefix mapping, i.e. platform:resource/my.project/ -> file:/my/path/to/my.project/
                // but then we wouldn't be able to load resources when using hosted bundles, because the target path points to the bin folder.
                // so instead we install qualified file mappings, which also makes normalization faster (i.e. just a lookup in a map instead of testing prefix URIs)
                //
                Map<URI, IStorage> mapping = storage2UriMapper.getAllEntries(root);
                for (URI key : mapping.keySet()) {
                    IStorage storage = mapping.get(key);
                    URI physicalURI = null;
                    if (storage instanceof IFile) {
                        physicalURI = URI.createPlatformResourceURI(storage.getFullPath().toString(), true);
                    } else {
                        physicalURI = key.replacePrefix(uriMapping.getFirst(), uriMapping.getSecond());
                    }
                    hashMap.put(key, physicalURI);
                    if (key.isPlatformResource()) {
                        URI pluginURI = URI.createPlatformPluginURI(key.toPlatformString(false), false);
                        hashMap.put(pluginURI, physicalURI);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        LOG.error(e.getMessage(), e);
    } catch (NoClassDefFoundError e) { // guard against broken Eclipse installations / bogus project configuration
        LOG.error(e.getMessage(), e);
    }
    return hashMap;
}

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

License:Open Source License

/**
 * @since 2.4/*from  www.  j  a va  2 s  .  co  m*/
 */
private void updateCache(IJavaProject project) {
    Set<PackageFragmentRootData> datas = newHashSet();
    try {
        if (project.exists() && project.getProject().isAccessible()) {
            for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
                boolean isCachable = root.isArchive() || root.isExternal();
                if (isCachable)
                    datas.add(getCachedData(root));
            }
        }
    } catch (JavaModelException e) {
        if (!e.isDoesNotExist())
            log.error("Error getting package fragments roots of " + project.getElementName(), e);
    } finally {
        clearCache(project, datas);
    }
}

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

License:Open Source License

public ResourceSet get(IProject project) {
    XtextResourceSet set = resourceSetProvider.get();
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null && javaProject.exists()) {
        set.getURIConverter().getURIMap().putAll(computePlatformURIMap(javaProject));
        set.setClasspathURIContext(javaProject);
        set.setClasspathUriResolver(new JdtClasspathUriResolver());
    }/*from  ww w  . j a va  2 s.c o m*/
    return set;
}

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

License:Open Source License

protected Map<URI, URI> computePlatformURIMap(IJavaProject javaProject) {
    HashMap<URI, URI> hashMap = newHashMap();
    try {/*w  w  w  . j  a  va  2  s .com*/
        hashMap.putAll(EcorePlugin.computePlatformURIMap(false));
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
    if (!javaProject.exists())
        return hashMap;
    try {
        IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot root : roots) {
            Pair<URI, URI> uriMapping = storage2UriMapper.getURIMapping(root);
            if (uriMapping != null) {

                // we could just install the prefix mapping, i.e. platform:resource/my.project/ -> file:/my/path/to/my.project/
                // but then we wouldn't be able to load resources when using hosted bundles, because the target path points to the bin folder.
                // so instead we install qualified file mappings, which also makes normalization faster (i.e. just a lookup in a map instead of testing prefix URIs)
                //
                Map<URI, IStorage> mapping = storage2UriMapper.getAllEntries(root);
                for (URI key : mapping.keySet()) {
                    IStorage storage = mapping.get(key);
                    URI physicalURI = null;
                    if (storage instanceof IFile) {
                        physicalURI = URI.createPlatformResourceURI(storage.getFullPath().toString(), true);
                    } else {
                        physicalURI = key.replacePrefix(uriMapping.getFirst(), uriMapping.getSecond());
                    }
                    hashMap.put(key, physicalURI);
                    if (key.isPlatformResource()) {
                        URI pluginURI = URI.createPlatformPluginURI(key.toPlatformString(false), false);
                        hashMap.put(pluginURI, physicalURI);
                    }
                }
            }
        }
        final IProject project = javaProject.getProject();
        for (IProject iProject : project.getWorkspace().getRoot().getProjects()) {
            if (iProject.isAccessible()) {
                IPath location = iProject.getLocation();
                if (location != null) {
                    // append a trailing slash so that URI.isPrefix is true.
                    hashMap.put(URI.createPlatformResourceURI(iProject.getName() + "/", true),
                            URI.createFileURI(location.toFile().getPath() + "/"));
                }
            }
        }
    } catch (JavaModelException e) {
        LOG.error(e.getMessage(), e);
    }
    return hashMap;
}

From source file:org.eclipse.xtext.ui.shared.JdtHelper.java

License:Open Source License

@Override
public boolean isFromOutputPath(IResource resource) {
    IProject project = resource.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null && javaProject.exists()) {
        try {//from  w  w  w  .ja  va 2s. c om
            IPath defaultOutputLocation = javaProject.getOutputLocation();
            IPath resourcePath = resource.getFullPath();
            if (defaultOutputLocation != null && !defaultOutputLocation.isEmpty()
                    && defaultOutputLocation.isPrefixOf(resourcePath)) {
                return true;
            }
            IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
            for (IClasspathEntry classpathEntry : classpathEntries) {
                if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath specializedOutputLocation = classpathEntry.getOutputLocation();
                    if (specializedOutputLocation != null) {
                        if (!specializedOutputLocation.equals(classpathEntry.getPath())
                                && specializedOutputLocation.isPrefixOf(resourcePath)) {
                            return true;
                        }
                    }
                }
            }
        } catch (CoreException e) {
            if (log.isDebugEnabled())
                log.debug("Error in isJavaTargetFolder():" + e.getMessage(), e);
        }
    }
    return false;
}