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

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

Introduction

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

Prototype

IClasspathEntry[] getResolvedClasspath(boolean ignoreUnresolvedEntry) throws JavaModelException;

Source Link

Document

This is a helper method returning the resolved classpath for the project as a list of simple (non-variable, non-container) classpath entries.

Usage

From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java

License:Open Source License

/**
 * Creates a ClassLoader using the project's build path.
 *
 * @param javaProject the Java project.//w  w  w .  j  a  v a 2s.  co m
 * @param parentClassLoader the parent class loader, may be null.
 *
 * @return a new ClassLoader based on the project's build path.
 *
 * @throws Exception if something goes wrong.
 */
public static ClassLoader getProjectClassLoader(IJavaProject javaProject, ClassLoader parentClassLoader)
        throws Exception {
    IProject project = javaProject.getProject();
    IWorkspaceRoot root = project.getWorkspace().getRoot();
    List<URL> urls = new ArrayList<>();
    urls.add(
            new File(project.getLocation() + "/" + javaProject.getOutputLocation().removeFirstSegments(1) + "/") //$NON-NLS-2$ //$NON-NLS-2$
                    .toURI().toURL());
    for (IClasspathEntry classpathEntry : javaProject.getResolvedClasspath(true)) {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IPath projectPath = classpathEntry.getPath();
            IProject otherProject = root.getProject(projectPath.segment(0));
            IJavaProject otherJavaProject = JavaCore.create(otherProject);
            urls.add(new File(otherProject.getLocation() + "/" //$NON-NLS-1$
                    + otherJavaProject.getOutputLocation().removeFirstSegments(1) + "/").toURI().toURL()); //$NON-NLS-1$
        } else if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            urls.add(new File(classpathEntry.getPath().toOSString()).toURI().toURL());
        }
    }
    if (parentClassLoader == null) {
        return new URLClassLoader(urls.toArray(new URL[urls.size()]));
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), parentClassLoader);
}

From source file:org.jboss.tools.as.itests.ProjectRuntimeClasspathTest.java

License:Open Source License

@Test
public void testJBIDE1657EquivilentEntries() {
    try {/*from  ww  w . ja  v  a 2s.c om*/
        IJavaProject jp = JavaCore.create(project);

        // lets try a runtime
        IRuntime createdRuntime = server.getRuntime();
        ProjectRuntimeUtil.setTargetRuntime(createdRuntime, project);
        IClasspathEntry[] raw1 = jp.getRawClasspath();
        IClasspathEntry[] resolved1 = jp.getResolvedClasspath(false);

        IClasspathEntry[] raw2 = cloneAndReplace(raw1, createdRuntime.getName());
        jp.setRawClasspath(raw2, new NullProgressMonitor());
        IClasspathEntry[] resolved2 = jp.getResolvedClasspath(false);
        assertEquals("New classpath container path should return the same classpath entries as the old. ",
                resolved1.length, resolved2.length);
        assertTrue("Should be more than one classpath entry", resolved1.length > 0);
    } catch (CoreException ce) {
        ce.printStackTrace();
        fail(ce.getMessage());
    }
}

From source file:org.jboss.tools.as.itests.ProjectRuntimeClasspathTest.java

License:Open Source License

protected void verifyPostRuntimeCPE(IJavaProject jp) throws CoreException {
    IClasspathEntry[] entries = jp.getRawClasspath();
    String[] required = new String[] { "org.eclipse.jst.server.core.container", projectName,
            "org.eclipse.jst.j2ee.internal.web.container", "org.eclipse.jdt.launching.JRE_CONTAINER" };
    assertTrue(entries.length >= required.length);
    jp.getResolvedClasspath(false); // make sure it can resolve all
    verifyClasspathEntries(entries, required);
}

From source file:org.jboss.tools.as.itests.ProjectRuntimeClasspathTest.java

License:Open Source License

protected void verifyInitialClasspathEntries(IJavaProject jp) throws CoreException {
    IClasspathEntry[] entries = jp.getRawClasspath();
    jp.getResolvedClasspath(false); // make sure it can resolve all
    String[] required = new String[] { "org.eclipse.jst.j2ee.internal.web.container", projectName };
    verifyClasspathEntries(entries, required);
}

From source file:org.jboss.tools.batch.internal.core.scanner.lib.ClassPathMonitor.java

License:Open Source License

public static List<IBatchProject> getProjects(IProject project) throws CoreException {
    List<IBatchProject> list = new ArrayList<IBatchProject>();
    IJavaProject javaProject = EclipseUtil.getJavaProject(project);
    if (javaProject != null) {
        IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < es.length; i++) {
            if (es[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(es[i].getPath().lastSegment());
                if (p == null || !p.isAccessible())
                    continue;
                IBatchProject sp = BatchProjectFactory.getBatchProject(p, false);
                if (sp != null)
                    list.add(sp);//w  ww  .  j a  v  a  2  s.c  o m
            }
        }
    }
    return list;
}

From source file:org.jboss.tools.batch.internal.core.scanner.lib.Libs.java

License:Open Source License

private void updateProjects() throws JavaModelException {
    Set<String> result = new HashSet<String>();
    IJavaProject javaProject = EclipseUtil.getJavaProject(getProjectResource());
    if (javaProject != null) {
        result.add(getProjectResource().getName());
        IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < es.length; i++) {
            if (es[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(es[i].getPath().lastSegment());
                if (p == null || !p.isAccessible())
                    continue;
                result.add(p.getName());
            }//from  w  ww . j  av a2s  .c  o m
        }
    }
    projects = result;
}

From source file:org.jboss.tools.batch.internal.core.scanner.lib.Libs.java

License:Open Source License

private int computeExcludedState() throws JavaModelException {
    int result = 0;
    IJavaProject javaProject = EclipseUtil.getJavaProject(getProjectResource());
    if (javaProject != null) {
        IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < es.length; i++) {
            IPath p = es[i].getPath();// www . j  av  a  2  s  . c  om
            IPath[] ps = es[i].getExclusionPatterns();
            if (ps != null && ps.length > 0) {
                for (int j = 0; j < ps.length; j++) {
                    String key = p.toString() + "/" + ps[j].toString(); //$NON-NLS-1$
                    result += key.hashCode();
                }
            }
        }
    }
    return result;
}

From source file:org.jboss.tools.cdi.internal.core.scanner.lib.ClassPathMonitor.java

License:Open Source License

public static List<CDICoreNature> getProjects(IProject project) throws CoreException {
    List<CDICoreNature> list = new ArrayList<CDICoreNature>();
    IJavaProject javaProject = EclipseResourceUtil.getJavaProject(project);
    if (javaProject != null) {
        IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < es.length; i++) {
            if (es[i].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(es[i].getPath().lastSegment());
                if (p == null || !p.isAccessible())
                    continue;
                CDICoreNature sp = CDICorePlugin.getCDI(p, false);
                if (sp != null)
                    list.add(sp);//from   ww  w . ja va  2 s. com
            }
        }
    }
    return list;
}

From source file:org.jboss.tools.common.EclipseUtil.java

License:Open Source License

public static IResource[] getJavaSourceRoots(IProject project) {
    IJavaProject javaProject = getJavaProject(project);
    if (javaProject == null)
        return null;
    List<IResource> resources = new ArrayList<IResource>();
    try {/* w  w w.  j  ava  2 s  . c om*/
        IClasspathEntry[] es = javaProject.getResolvedClasspath(true);
        for (int i = 0; i < es.length; i++) {
            if (es[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IResource findMember = ResourcesPlugin.getWorkspace().getRoot().findMember(es[i].getPath());
                if (findMember != null && findMember.exists()) {
                    resources.add(findMember);
                }
            }
        }
    } catch (CoreException ce) {
        CommonPlugin.getPluginLog().logError("Error while locating java source roots for " + project, ce); //$NON-NLS-1$
    }
    return resources.toArray(new IResource[resources.size()]);
}

From source file:org.jboss.tools.common.java.generation.JavaBeanGenerator.java

License:Open Source License

private String getSrcLocation(IJavaProject javaproject) throws CoreException {
    IClasspathEntry[] entries = javaproject.getResolvedClasspath(true);
    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getEntryKind() != IClasspathEntry.CPE_SOURCE)
            continue;
        IResource resource = null;//from   w  w  w  . j  av a 2s  . com
        IPath projectPath = ModelPlugin.getWorkspace().getRoot().getFullPath();
        if (projectPath != null && projectPath.equals(projectPath.append(entries[i].getPath()))) {
            resource = ModelPlugin.getWorkspace().getRoot();
        } else {
            resource = ModelPlugin.getWorkspace().getRoot().getFolder(entries[i].getPath());
        }
        return resource.getLocation().toString();
    }
    return null;
}