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.robovm.eclipse.internal.ib.IBIntegratorManager.java

License:Open Source License

private LinkedHashSet<File> resolveClasspath(IWorkspaceRoot root, IJavaProject javaProject)
        throws JavaModelException {

    LinkedHashSet<File> classpath = new LinkedHashSet<>();
    for (IClasspathEntry cpe : javaProject.getResolvedClasspath(true)) {
        if (cpe.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IJavaProject jproj = JavaCore.create(root.findMember(cpe.getPath()).getProject());
            classpath.addAll(resolveClasspath(root, jproj));
        } else if (cpe.getEntryKind() != IClasspathEntry.CPE_SOURCE && cpe.getPath() != null) {
            File file = cpe.getPath().toFile();
            if (!file.exists()) {
                // Probably a workspace absolute path. Resolve it.
                IResource res = root.findMember(cpe.getPath());
                if (res != null) {
                    file = res.getLocation().toFile();
                }//from   ww  w.ja  va 2s .  c o  m
            }
            if (file.exists()) {
                classpath.add(file);
            }
        }
    }

    classpath.addAll(getOutputLocations(javaProject));

    return classpath;
}

From source file:org.robovm.eclipse.RoboVMPlugin.java

License:Open Source License

private static void getSourcePaths(Set<String> paths, IJavaProject javaProject) throws CoreException {
    try {//  www .  j  a v  a 2s .c o  m
        // add the source jars of rt/objc/cocoatouch etc.
        File libDir = new File(RoboVMPlugin.getRoboVMHome().getBinDir().getParentFile(), "lib");
        paths.add(new File(libDir, "robovm-cocoatouch-sources.jar").getAbsolutePath());
        paths.add(new File(libDir, "robovm-objc-sources.jar").getAbsolutePath());
        paths.add(new File(libDir, "robovm-rt-sources.jar").getAbsolutePath());
    } catch (IOException e) {
        RoboVMPlugin.consoleError("Couldn't retrieve lib/ directory");
    }
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
        IPath path = null;
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IResource resource = root.findMember(entry.getPath());
            if (resource != null) {
                path = resource.getLocation();
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            if (entry.getSourceAttachmentPath() != null) {
                path = entry.getSourceAttachmentPath();
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().toString());
            if (project.isNatureEnabled("org.eclipse.jdt.core.javanature")) {
                getSourcePaths(paths, JavaCore.create(project));
            }
        }
        if (path != null) {
            paths.add(path.toOSString());
        }
    }
}

From source file:org.seasar.diigu.eclipse.util.JavaProjectClassLoader.java

License:Apache License

protected void addClasspathEntries(IJavaProject project, Set already, boolean atFirst) {
    already.add(project);/*from  w  w w .j  av  a 2 s  .  c  o  m*/

    try {
        IContainer workspaceroot = project.getProject().getParent();
        IPath path = project.getOutputLocation();
        addURL(toURL(workspaceroot.getFolder(path).getLocation()));

        IClasspathEntry[] entries = project.getResolvedClasspath(true);
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_SOURCE:
                IPath dist = entry.getOutputLocation();
                if (dist != null) {
                    addURL(toURL(workspaceroot.getFolder(dist).getLocation()));
                }
                break;
            case IClasspathEntry.CPE_LIBRARY:
            case IClasspathEntry.CPE_CONTAINER:
            case IClasspathEntry.CPE_VARIABLE:
                IPath p = entry.getPath();
                if (p.toFile().exists()) {
                    addURL(toURL(p));
                } else {
                    addURL(toURL(workspaceroot.getFile(p).getLocation()));
                }
                break;
            case IClasspathEntry.CPE_PROJECT:
                IJavaProject proj = ProjectUtils.getJavaProject(entry.getPath().segment(0));
                if (proj != null && proj.exists() && already.contains(proj) == false
                        && (atFirst || entry.isExported())) {
                    addClasspathEntries(proj, already, false);
                }
                break;
            default:
                break;
            }
        }
    } catch (Exception e) {
        DiiguPlugin.log(e);
    }
}

From source file:org.seasar.kijimuna.core.loader.ProjectClassLoader.java

License:Apache License

private void getClassPaths(IJavaProject project) {
    if (project == null) {
        throw new NullPointerException();
    }/*from   www .ja v a  2  s .c  om*/
    List errorList = new ArrayList();
    try {
        IClasspathEntry[] entries = project.getResolvedClasspath(true);
        for (int i = 0; i < entries.length; i++) {
            int kind = entries[i].getEntryKind();
            String url = null;
            if (kind == IClasspathEntry.CPE_SOURCE) {
                try {
                    url = "file:///" + entries[i].getOutputLocation().toString();
                } catch (RuntimeException e1) {
                    continue;
                }
            } else {
                url = "jar:file:///" + entries[i].getPath().toString() + "!/";
            }
            try {
                addURL(new URL(url));
            } catch (MalformedURLException e) {
                errorList.add(url);
            }
        }
    } catch (JavaModelException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.seasar.kijimuna.core.util.JavaProjectClassLoader.java

License:Apache License

protected void addClasspathEntries(IJavaProject project, Set already, boolean atFirst) {
    already.add(project);// w  w w.j a  va  2 s .  c  om

    try {
        IContainer workspaceroot = project.getProject().getParent();
        IPath path = project.getOutputLocation();
        addURL(toURL(workspaceroot.getFolder(path).getLocation()));

        IClasspathEntry[] entries = project.getResolvedClasspath(true);
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_SOURCE:
                IPath dist = entry.getOutputLocation();
                if (dist != null) {
                    addURL(toURL(workspaceroot.getFolder(dist).getLocation()));
                }
                break;
            case IClasspathEntry.CPE_LIBRARY:
            case IClasspathEntry.CPE_CONTAINER:
            case IClasspathEntry.CPE_VARIABLE:
                IPath p = entry.getPath();
                if (p.toFile().exists()) {
                    addURL(toURL(p));
                } else {
                    addURL(toURL(workspaceroot.getFile(p).getLocation()));
                }
                break;
            case IClasspathEntry.CPE_PROJECT:
                IJavaProject proj = JavaCore.create(ProjectUtils.getProject(entry.getPath().segment(0)));
                if (proj != null && proj.exists() && already.contains(proj) == false
                        && (atFirst || entry.isExported())) {
                    addClasspathEntries(proj, already, false);
                }
                break;
            default:
                break;
            }
        }
    } catch (Exception e) {
        KijimunaCore.reportException(e);
    }
}

From source file:org.seasar.kvasir.plust.ProjectClassLoader.java

License:Apache License

private void getClassPaths(IJavaProject project) {
    if (project == null) {
        throw new NullPointerException();
    }//from w  w  w  . j  ava  2  s  . com
    try {
        IClasspathEntry[] entries = project.getResolvedClasspath(true);
        for (int i = 0; i < entries.length; i++) {
            int kind = entries[i].getEntryKind();
            URL url = null;
            if (kind == IClasspathEntry.CPE_SOURCE) {
                try {
                    IPath outputLocation = entries[i].getOutputLocation();
                    if (outputLocation == null) {
                        outputLocation = project.getOutputLocation();
                        if (outputLocation == null) {
                            // TODO ????????
                            KvasirPlugin.getDefault().log(
                                    "Can't construct URL for classLoader because default output location is null: entry="
                                            + entries[i].getPath(),
                                    null);
                            continue;
                        }
                    }

                    url = ClassUtils.getURLForURLClassLoader(
                            project.getProject().getParent().getFolder(outputLocation).getLocation().toFile());
                } catch (RuntimeException e1) {
                    KvasirPlugin.getDefault()
                            .log("Can't construct URL for classLoader: entry=" + entries[i].getPath(), e1);
                    continue;
                }
            } else {
                url = ClassUtils.getURLForURLClassLoader(entries[i].getPath().toFile());
                //                    "jar:file:///" + entries[i].getPath().toString()
                //                        + "!/";
            }
            this.monitor.subTask(Messages.getString("KvasirProject.3") + ":" + url);
            addURL(url);
        }
    } catch (JavaModelException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.seasar.s2junit4plugin.util.JDTUtil.java

License:Apache License

/**
 * ????/*w w w  . ja v  a 2 s.com*/
 * @param project Java
 * @return 
 * @throws JavaModelException JDT????
 */
public static List<IPath> getSourceFolders(IJavaProject project) throws JavaModelException {
    List<IPath> sourceFolders = new java.util.ArrayList<IPath>();
    for (IClasspathEntry entry : project.getResolvedClasspath(true)) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            sourceFolders.add(entry.getPath());
        }
    }
    return sourceFolders;
}

From source file:org.sonar.ide.eclipse.internal.jdt.JavaProjectConfigurator.java

License:Open Source License

private void configureJavaProject(IJavaProject javaProject, ProjectDefinition sonarProject) {
    Properties properties = sonarProject.getProperties();
    String javaSource = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
    String javaTarget = javaProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true);

    properties.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, Java.KEY);
    properties.setProperty("sonar.java.source", javaSource);
    LOG.info("Source Java version: {}", javaSource);
    properties.setProperty("sonar.java.target", javaTarget);
    LOG.info("Target Java version: {}", javaTarget);

    try {//www. j a v a  2 s  . c om
        IClasspathEntry[] classPath = javaProject.getResolvedClasspath(true);
        for (IClasspathEntry entry : classPath) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_SOURCE:
                if (isSourceExcluded(entry)) {
                    break;
                }
                String srcDir = getAbsolutePath(javaProject, entry.getPath());
                LOG.debug("Source directory: {}", srcDir);
                sonarProject.addSourceDir(srcDir);
                if (entry.getOutputLocation() != null) {
                    String binDir = getAbsolutePath(javaProject, entry.getOutputLocation());
                    LOG.debug("Binary directory: {}", binDir);
                    sonarProject.addBinaryDir(binDir);
                }
                break;

            case IClasspathEntry.CPE_LIBRARY:
                String libDir = entry.getPath().toOSString();
                LOG.debug("Library: {}", libDir);
                sonarProject.addLibrary(libDir);
                break;

            default:
                LOG.warn("Unhandled ClassPathEntry : {}", entry);
                break;
            }
        }

        String binDir = getAbsolutePath(javaProject, javaProject.getOutputLocation());
        LOG.debug("Default binary directory: {}", binDir);
        sonarProject.addBinaryDir(binDir);
    } catch (JavaModelException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:org.sonar.ide.eclipse.jdt.internal.JavaProjectConfigurator.java

License:Open Source License

/**
 * Adds the classpath of an eclipse project to the sonarProject recursively, i.e
 * it iterates all dependent projects. Libraries and output folders of dependent projects
 * are added, but no source folders./*from  ww w . j  a va  2 s.c  o m*/
 * @param javaProject the eclipse project to get the classpath from
 * @param sonarProjectProperties the sonar project properties to add the classpath to
 * @param context
 * @param topProject indicate we are working on the project to be analysed and not on a dependent project
 * @throws JavaModelException see {@link IJavaProject#getResolvedClasspath(boolean)}
 */
private void addClassPathToSonarProject(IJavaProject javaProject, JavaProjectConfiguration context,
        boolean topProject) throws JavaModelException {
    IClasspathEntry[] classPath = javaProject.getResolvedClasspath(true);
    for (IClasspathEntry entry : classPath) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (!isSourceExcluded(entry)) {
                processSourceEntry(entry, javaProject, context, topProject);
            }
            break;
        case IClasspathEntry.CPE_LIBRARY:
            if (topProject || entry.isExported()) {
                final String libDir = resolveLibrary(javaProject, entry);
                if (libDir != null) {
                    LOG.debug("Library: {}", libDir);
                    context.libraries().add(libDir);
                }
            }
            break;
        case IClasspathEntry.CPE_PROJECT:
            IJavaModel javaModel = javaProject.getJavaModel();
            IJavaProject referredProject = javaModel.getJavaProject(entry.getPath().segment(0));
            if (!context.dependentProjects().contains(referredProject)) {
                LOG.debug("Adding project: {}", referredProject.getProject().getName());
                addClassPathToSonarProject(referredProject, context, false);
                context.dependentProjects().add(referredProject);
            }
            break;
        default:
            LOG.warn("Unhandled ClassPathEntry : {}", entry);
            break;
        }
    }

    processOutputDir(javaProject.getOutputLocation(), context, topProject);
}

From source file:org.sonar.ide.eclipse.jdt.internal.JavaProjectConfiguratorTest.java

License:Open Source License

@Test
public void shouldConfigureJavaSourceAndTarget() throws JavaModelException, IOException {
    IJavaProject project = mock(IJavaProject.class);
    Properties sonarProperties = new Properties();

    when(project.getOption(JavaCore.COMPILER_SOURCE, true)).thenReturn("1.6");
    when(project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).thenReturn("1.6");
    when(project.getResolvedClasspath(true)).thenReturn(new IClasspathEntry[] {});
    when(project.getOutputLocation()).thenReturn(new Path(temp.newFolder("output").getAbsolutePath()));

    configurator.configureJavaProject(project, sonarProperties, true);

    assertTrue(sonarProperties.containsKey("sonar.java.source"));
    assertThat(sonarProperties.getProperty("sonar.java.source"), is("1.6"));
    assertTrue(sonarProperties.containsKey("sonar.java.target"));
    assertThat(sonarProperties.getProperty("sonar.java.target"), is("1.6"));
}