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

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

Introduction

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

Prototype

IClasspathEntry getResolvedClasspathEntry() throws JavaModelException;

Source Link

Document

Returns the first resolved classpath entry that corresponds to this package fragment root.

Usage

From source file:org.eclipse.che.plugin.maven.server.core.classpath.ClasspathManager.java

License:Open Source License

private boolean downloadSources(IPackageFragmentRoot fragmentRoot) throws JavaModelException {
    fragmentRoot.getAdapter(MavenArtifactKey.class);
    IClasspathEntry classpathEntry = fragmentRoot.getResolvedClasspathEntry();
    MavenArtifactKey artifactKey = getArtifactKey(classpathEntry);
    if (artifactKey != null) {
        MavenServerWrapper mavenServer = wrapperManager.getMavenServer(MavenWrapperManager.ServerType.DOWNLOAD);

        try {/*  ww w. jav  a2 s. c  om*/
            mavenServer.customize(projectManager.copyWorkspaceCache(), terminal, notifier, false, false);

            MavenArtifactKey sourceKey = new MavenArtifactKey(artifactKey.getGroupId(),
                    artifactKey.getArtifactId(), artifactKey.getVersion(), artifactKey.getPackaging(), SOURCES);
            MavenArtifact mavenArtifact = mavenServer.resolveArtifact(sourceKey, Collections.emptyList());
            if (mavenArtifact.isResolved()) {
                updateClasspath(projectManager.findMavenProject(fragmentRoot.getJavaProject().getProject()));
            }
            return mavenArtifact.isResolved();
        } finally {
            wrapperManager.release(mavenServer);
        }
    }
    return false;
}

From source file:org.eclipse.edt.ide.ui.internal.externaltype.wizards.javatype.ExternalTypeFromJavaPage.java

License:Open Source License

private ClassLoader getURLClassLoader() {
    if (urlClassLoader == null) {
        List<URL> classPathURLs = new ArrayList<URL>();

        try {//Add Java class path.
            IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();

            for (IPackageFragmentRoot pRoot : roots) {
                IJavaProject refProject = pRoot.getParent().getJavaProject();
                IPath proRoot = refProject.getProject().getLocation();

                if (pRoot.isArchive() && pRoot.isExternal()) {
                    classPathURLs.add(pRoot.getResolvedClasspathEntry().getPath().toFile().toURI().toURL());
                } else if (pRoot.isArchive() && pRoot.getResource() != null) {
                    classPathURLs.add(proRoot.append(pRoot.getResource().getProjectRelativePath()).toFile()
                            .toURI().toURL());
                } else { //source folder
                    IPath outputRelPath = refProject.getOutputLocation().removeFirstSegments(1);
                    classPathURLs.add(proRoot.append(outputRelPath).toFile().toURI().toURL());
                }/*w  ww  .  jav a2 s.co  m*/
            }
        } catch (Throwable ee) {
            ee.printStackTrace();
        }

        ClassLoader parent = Thread.currentThread().getContextClassLoader();
        URL[] urlPaths = new URL[classPathURLs.size()];
        urlClassLoader = new URLClassLoader(classPathURLs.toArray(urlPaths), parent);
    }

    return urlClassLoader;
}

From source file:org.eclipse.jst.jsf.core.jsfappconfig.AnnotationPackageFragmentRoot.java

License:Open Source License

private final boolean isWebInfClasses(IPackageFragmentRoot root_) {
    IClasspathEntry cpe;/*from w  w w . j  ava  2 s  .com*/
    try {
        cpe = root_.getResolvedClasspathEntry();
        //            IPath rootPath = cpe.getOutputLocation(); 
        return cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE;
        //            if (rootPath == null) {
        //                rootPath = jProject.getOutputLocation();
        //            }
        //            return webInfClassesPath.equals(rootPath);
    } catch (JavaModelException e) {
        return false;
    }
}

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

License:Open Source License

protected Charset getSourceEncoding(final ITypeRoot derivedJavaType) {
    // this should be symmetric to org.eclipse.jdt.internal.core.SourceMapper.findSource(String) 
    try {/*w w w  .  ja  v a 2  s . c  om*/
        IJavaElement current = derivedJavaType.getParent();
        while (current != null) {
            if (current instanceof IPackageFragmentRoot) {
                IPackageFragmentRoot root = (IPackageFragmentRoot) current;
                try {
                    // see org.eclipse.jdt.internal.core.ClasspathEntry.getSourceAttachmentEncoding()
                    IClasspathAttribute[] attributes = root.getResolvedClasspathEntry().getExtraAttributes();
                    for (int i = 0, length = attributes.length; i < length; i++) {
                        IClasspathAttribute attribute = attributes[i];
                        if (SOURCE_ATTACHMENT_ENCODING.equals(attribute.getName()))
                            return Charset.forName(attribute.getValue());
                    }
                } catch (JavaModelException e) {
                }
                return Charset.forName(ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset());
            }
            current = current.getParent();
        }
        return Charset.forName(ResourcesPlugin.getWorkspace().getRoot().getDefaultCharset());
    } catch (CoreException e) {
        log.error("Error determining encoding for source file for " + derivedJavaType.getElementName(), e);
        return Charsets.UTF_8;
    }
}

From source file:org.jboss.tools.maven.conversion.ui.handlers.ConvertToMavenDependencyHandler.java

License:Open Source License

private void addClasspathEntry(IPackageFragmentRoot pfr, Collection<IClasspathEntry> entries) {
    if (pfr.isArchive()) {
        pfr.getResource();/* w  w w  .jav  a  2  s . co m*/
        try {
            IClasspathEntry cpe = pfr.getResolvedClasspathEntry();
            if (cpe != null && cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                entries.add(cpe);
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.springframework.tooling.jdt.ls.commons.java.JavaData.java

License:Open Source License

private ClasspathEntryData createClasspathEntryData(IMember member) {
    ClasspathEntryData data = new ClasspathEntryData();

    IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) member
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (packageFragmentRoot != null) {
        try {/*from   w w w .ja va 2 s .  co  m*/
            IClasspathEntry entry = packageFragmentRoot.getResolvedClasspathEntry();
            if (entry != null) {
                List<CPE> cpes = ClasspathUtil.createCpes(packageFragmentRoot.getJavaProject(), entry);
                Assert.isTrue(cpes.size() < 2);
                if (!cpes.isEmpty()) {
                    data.setCpe(cpes.get(0));
                }
            }
        } catch (JavaModelException | MalformedURLException e) {
            logger.log(e);
        }
    }

    ITypeRoot typeRoot = member.getTypeRoot();
    try {
        if (typeRoot != null && typeRoot.getModule() != null) {
            data.setModule(typeRoot.getModule().getElementName());
        }
    } catch (JavaModelException e) {
        logger.log(e);
    }

    return data;
}