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:x10dt.ui.launch.core.utils.ProjectUtils.java

License:Open Source License

/**
 * Returns the set of projects on this project's classpath.
 *//*www .j a v  a  2s  . co  m*/
public static Collection<IProject> getDependentProjects(IJavaProject project) throws JavaModelException {
    Collection<IProject> result = new ArrayList<IProject>();
    final IWorkspaceRoot root = project.getResource().getWorkspace().getRoot();
    for (final IClasspathEntry cpEntry : project.getResolvedClasspath(true)) {
        if (cpEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            final IResource resource = root.findMember(cpEntry.getPath());
            if (resource == null) {
                LaunchCore.log(IStatus.WARNING, NLS.bind(Messages.JPU_ResourceErrorMsg, cpEntry.getPath()));
            } else {
                result.add((IProject) resource);
            }
        }
    }
    return result;
}

From source file:x10dt.ui.launch.core.utils.ProjectUtils.java

License:Open Source License

/**
 * Returns a filtered set of class path entries for a given Java project.
 * //ww  w.  jav a  2s .  c  o m
 * @param <T> The type of the class path entries once transformed via the functor provided.
 * @param jProject The Java project to consider.
 * @param cpEntryFunctor The functor to use to transform an {@link IPath} related to a class path
 * entry into another type of interest.
 * @param libFilter The filter to user in order to filter the library entries.
 * @return A non-null, possibly empty, set of class path entries.
 * @throws JavaModelException Occurs if we could not resolve the class path entries.
 * @throws IllegalArgumentException Occurs if a class path entry kind is not one of the expected
 * list. More precisely, CPE_VARIABLE and CPE_CONTAINER should not be encountered.
 */
public static <T> Set<T> getFilteredCpEntries(final IJavaProject jProject,
        final IFunctor<IPath, T> cpEntryFunctor, final IFilter<IPath> libFilter, StringBuffer pathBuffer)
        throws JavaModelException {
    final Set<T> container = new HashSet<T>();
    final IWorkspaceRoot root = jProject.getResource().getWorkspace().getRoot();
    for (final IClasspathEntry cpEntry : jProject.getResolvedClasspath(true)) {
        collectCpEntries(container, cpEntry, root, libFilter, cpEntryFunctor, jProject.getProject(),
                pathBuffer);
    }
    return container;
}

From source file:x10dt.ui.launch.core.utils.ProjectUtils.java

License:Open Source License

/**
 * Returns the collection of source folders for a given project.
 * /*w w  w .j a v  a2s . c  o m*/
 * @param project The project of interest.
 * @return A non-null collection of workspace-relative strings representing the src folders of the project.
 * @throws JavaModelException Occurs if we could not resolve the project class path.
 */
public static Collection<String> collectSourceFolders(final IJavaProject project) throws JavaModelException {
    final Collection<String> result = new ArrayList<String>();
    for (final IClasspathEntry cpEntry : project.getResolvedClasspath(true)) {
        if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            final IPath entryPath = cpEntry.getPath();
            if (!entryPath.segment(0).equals(project.getElementName())) {
                continue;
            }
            result.add(entryPath.toOSString());
        }
    }
    return result;
}

From source file:x10dt.ui.launch.core.utils.ProjectUtils.java

License:Open Source License

private static <T> void collectCpEntries(final Set<T> container, final IClasspathEntry cpEntry,
        final IWorkspaceRoot root, final IFilter<IPath> libFilter, final IFunctor<IPath, T> functor,
        final IProject project, StringBuffer pathBuffer) throws JavaModelException {
    final IJavaProject javaProject = JavaCore.create(project);
    switch (cpEntry.getEntryKind()) {
    case IClasspathEntry.CPE_SOURCE:
        if (isX10Project(javaProject)) {
            IPath outputPath = getOutputLocation(cpEntry, javaProject);
            if (outputPath.lastSegment().equals("bin-java")) {
                appendNew(functor, container, pathBuffer, makeAbsolutePath(root, outputPath));

            } else {
                appendNew(functor, container, pathBuffer, makeAbsolutePath(root, cpEntry.getPath()));
            }//w  ww  .j  a  v  a2s .c  o m
        } else {
            appendNew(functor, container, pathBuffer,
                    makeAbsolutePath(root, getOutputLocation(cpEntry, javaProject)));
        }
        break;

    case IClasspathEntry.CPE_LIBRARY:
        IPath path = cpEntry.getPath();
        if (libFilter.accepts(path)) {
            IPath absolutePath = makeAbsolutePath(root, path);
            appendNew(functor, container, pathBuffer, absolutePath);
        }
        break;

    case IClasspathEntry.CPE_PROJECT:
        final IResource resource = root.findMember(cpEntry.getPath());
        if (resource == null) {
            LaunchCore.log(IStatus.WARNING, NLS.bind(Messages.JPU_ResourceErrorMsg, cpEntry.getPath()));
        } else {
            final IJavaProject refProject = JavaCore.create((IProject) resource);
            for (final IClasspathEntry newCPEntry : refProject.getResolvedClasspath(true)) {
                collectCpEntries(container, newCPEntry, root, libFilter, functor, (IProject) resource,
                        pathBuffer);
            }
        }
        break;

    default:
        throw new IllegalArgumentException(
                NLS.bind(Messages.JPU_UnexpectedEntryKindMsg, cpEntry.getEntryKind()));
    }
}

From source file:x10dt.ui.parser.CompilerDelegate.java

License:Open Source License

/**
 * @return a list of all project-relative CPE_SOURCE-type classpath entries.
 * @throws JavaModelException//from   w w  w.j ava 2s. c om
 */
private List<IPath> getProjectSrcPath() throws CoreException {
    List<IPath> srcPath = new ArrayList<IPath>();

    // Produce a search path heuristically for files living outside the workspace,
    // and for workspace files living in non-X10-natured projects.
    if (fX10Project == null || !isX10Project()) {
        IPath pkgRootPath = determinePkgRootPath();

        if (pkgRootPath != null) {
            if (fX10Project != null && fX10Project.getProject().getName().equals("x10.runtime")) {
                // If the containing project happens to be x10.runtime,
                // don't add the runtime bound into the X10DT to the search path
                return Arrays.asList(pkgRootPath);
            } else {
                return Arrays.asList(pkgRootPath, new Path(getRuntimePath()));
            }
        } else {
            return Arrays.asList((IPath) new Path(getRuntimePath()));
        }
    }

    IClasspathEntry[] classPath = fX10Project.getResolvedClasspath(true);

    for (int i = 0; i < classPath.length; i++) {
        IClasspathEntry e = classPath[i];

        if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            srcPath.add(e.getPath());
        } else if (e.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            //PORT1.7 Compiler needs to see X10 source for all referenced compilation units,
            // so add source path entries of referenced projects to this project's sourcepath.
            // Assume that goal dependencies are such that Polyglot will not be compelled to
            // compile referenced X10 source down to Java source (causing duplication; see below).
            //
            // RMF 6/4/2008 - Don't add referenced projects to the source path:
            // 1) doing so should be unnecessary, since the classpath will include
            //    the project, and the class files should satisfy all references,
            // 2) doing so will cause Polyglot to compile the source files found in
            //    the other project to Java source files located in the *referencing*
            //    project, causing duplication, which is not what we want.
            //
            IProject refProject = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(e.getPath().toPortableString());
            IJavaProject refJavaProject = JavaCore.create(refProject);
            IClasspathEntry[] refJavaCPEntries = refJavaProject.getResolvedClasspath(true);
            for (int j = 0; j < refJavaCPEntries.length; j++) {
                if (refJavaCPEntries[j].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    srcPath.add(refJavaCPEntries[j].getPath());
                }
            }
        } else if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            // PORT1.7 Add the X10 runtime jar to the source path, since the compiler
            // needs to see the X10 source for the user-visible runtime classes (like
            // x10.lang.Region) to get the extra type information (for deptypes) that
            // can't be stored in Java class files, and for now, these source files
            // actually live in the X10 runtime jar.
            IPath path = e.getPath();
            if (path.toPortableString().contains(X10BundleUtils.X10_RUNTIME_BUNDLE_ID)) {
                srcPath.add(path);
            }
        }
    }
    if (srcPath.size() == 0)
        srcPath.add(fX10Project.getProject().getLocation());
    return srcPath;
}

From source file:x10dt.ui.parser.CompilerDelegate.java

License:Open Source License

/**
 * Build a class path string for a project
 * @param project The root project to start with
 * @param buff Accumulates the classpath into this buffer
 * @throws JavaModelException/*from w  w w  . j  a  v a  2 s.co  m*/
 */
private static void buildClassPathSpec(final IWorkspaceRoot root, IJavaProject rootProject,
        IJavaProject project, Set<IPath> container) throws JavaModelException {
    if (project == null) {
        return;
    }

    IClasspathEntry[] classPath = project.getResolvedClasspath(true);
    for (IClasspathEntry cpEntry : classPath) {
        switch (cpEntry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (isX10Project(project)) {
                IPath outputPath = getOutputLocation(cpEntry, project);
                if (outputPath.lastSegment().equals("bin-java")) {
                    container.add(makeAbsolutePath(root, outputPath));
                } else {
                    container.add(makeAbsolutePath(root, cpEntry.getPath()));
                }
            } else {
                container.add(makeAbsolutePath(root, getOutputLocation(cpEntry, project)));
            }
            break;

        case IClasspathEntry.CPE_LIBRARY:
            IPath path = makeAbsolutePath(root, cpEntry.getPath());
            container.add(path);
            break;

        case IClasspathEntry.CPE_PROJECT:
            final IResource resource = root.findMember(cpEntry.getPath());
            if (resource == null) {
                X10DTCorePlugin.getInstance().writeErrorMsg("Error resolving class path: " + cpEntry.getPath());
            } else {
                final IJavaProject refProject = JavaCore.create((IProject) resource);
                for (final IClasspathEntry newCPEntry : refProject.getResolvedClasspath(true)) {
                    buildClassPathSpec(root, rootProject, refProject, container);
                }
            }
            break;

        default:
            X10DTCorePlugin.getInstance()
                    .writeErrorMsg("Error resolving class path kind: " + cpEntry.getEntryKind());
        }
    }
}

From source file:x10dt.ui.parser.PolyglotNodeLocator.java

License:Open Source License

private IClassFile resolveClassFile(final String path) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(fSrcProject.getRawProject());
    IClasspathEntry[] cpEntries = javaProject.getResolvedClasspath(true);

    for (int i = 0; i < cpEntries.length; i++) {
        IClasspathEntry entry = cpEntries[i];

        if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IPath entryPath = entry.getPath();
            IPackageFragmentRoot pkgRoot = javaProject.findPackageFragmentRoot(entryPath);
            final int pkgEnd = path.lastIndexOf('/');
            String pkgName = path.substring(0, pkgEnd).replace('/', '.');
            IPackageFragment pkgFrag = pkgRoot.getPackageFragment(pkgName);

            return pkgFrag.getClassFile(path.substring(pkgEnd + 1));
        }//w ww  .j  a  v  a 2 s  . c  o m
    }
    return null;
}