Example usage for org.eclipse.jdt.core IClasspathEntry CPE_PROJECT

List of usage examples for org.eclipse.jdt.core IClasspathEntry CPE_PROJECT

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry CPE_PROJECT.

Prototype

int CPE_PROJECT

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_PROJECT.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a required project.

Usage

From source file:org.jetbrains.kotlin.core.utils.ProjectUtils.java

License:Apache License

@NotNull
private static List<File> expandClasspath(@NotNull IJavaProject javaProject, boolean includeDependencies,
        boolean includeBinFolders, @NotNull Predicate<IClasspathEntry> entryPredicate)
        throws JavaModelException {
    Set<File> orderedFiles = Sets.newLinkedHashSet();

    for (IClasspathEntry classpathEntry : javaProject.getResolvedClasspath(true)) {
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT && includeDependencies) {
            orderedFiles/*from  ww  w . j ava2s . co m*/
                    .addAll(expandDependentProjectClasspath(classpathEntry, includeBinFolders, entryPredicate));
        } else { // Source folder or library
            if (entryPredicate.apply(classpathEntry)) {
                orderedFiles.addAll(getFileByEntry(classpathEntry, javaProject));
            }
        }
    }

    return Lists.newArrayList(orderedFiles);
}

From source file:org.jetbrains.kotlin.core.utils.ProjectUtils.java

License:Apache License

@NotNull
private static List<File> expandDependentProjectClasspath(@NotNull IClasspathEntry projectEntry,
        boolean includeBinFolders, @NotNull Predicate<IClasspathEntry> entryPredicate)
        throws JavaModelException {
    IPath projectPath = projectEntry.getPath();
    IProject dependentProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectPath.toString());
    IJavaProject javaProject = JavaCore.create(dependentProject);

    Set<File> orderedFiles = Sets.newLinkedHashSet();

    for (IClasspathEntry classpathEntry : javaProject.getResolvedClasspath(true)) {
        if (!(classpathEntry.isExported() || classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE)) {
            continue;
        }//from   w  w w .j  a  va 2  s . c  o  m

        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            orderedFiles
                    .addAll(expandDependentProjectClasspath(classpathEntry, includeBinFolders, entryPredicate));
        } else {
            if (entryPredicate.apply(classpathEntry)) {
                orderedFiles.addAll(getFileByEntry(classpathEntry, javaProject));
            }
        }
    }

    if (includeBinFolders) {
        IFolder outputFolder = ProjectUtils.getOutputFolder(javaProject);
        if (outputFolder != null && outputFolder.exists()) {
            orderedFiles.add(outputFolder.getLocation().toFile());
        }
    }

    return Lists.newArrayList(orderedFiles);
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Returns the {@link IResource}s of the given {@link IClasspathEntry}.
 * @param javaProject The actual {@link IJavaProject} that provides the {@link IClasspathEntry}.
 * @param entry The given {@link IClasspathEntry}.
 * @param alreadyHandledProjects The already handled {@link IProject} that don't need to be analysed again.
 * @return The found {@link IResource}s.
 * @throws JavaModelException //from   w  w w.  jav  a  2 s . co  m
 */
private static List<IResource> getResourceFor(IJavaProject javaProject, IClasspathEntry entry, int expectedKind,
        Set<IProject> alreadyHandledProjects) throws JavaModelException {
    if (entry != null) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                || entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                || entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                || entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            List<IResource> result = new LinkedList<IResource>();
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            for (IPackageFragmentRoot root : roots) {
                if (root.getKind() == expectedKind) {
                    if (root.getResource() != null) {
                        if (root.getResource().getLocationURI() != null) {
                            result.add(root.getResource());
                        }
                    } else if (root.getPath() != null) {
                        IResource resource = ResourcesPlugin.getWorkspace().getRoot()
                                .findMember(root.getPath());
                        if (resource != null && resource.exists()) {
                            result.add(resource);
                        }
                    }
                }
            }
            return result; // Ignore containers
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            Assert.isNotNull(entry.getPath());
            IResource project = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            Assert.isTrue(project instanceof IProject);
            if (!alreadyHandledProjects.contains(project)) {
                return getSourceResources((IProject) project, alreadyHandledProjects);
            } else {
                return null; // Project was already analyzed, no need to do it again.
            }
        } else {
            Assert.isTrue(false, "Unknown content kind \"" + entry.getContentKind()
                    + "\" of class path entry \"" + entry + "\".");
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.key_project.util.jdt.JDTUtil.java

License:Open Source License

/**
 * Returns the locations of the given {@link IClasspathEntry}.
 * @param javaProject The actual {@link IJavaProject} that provides the {@link IClasspathEntry}.
 * @param entry The given {@link IClasspathEntry}.
 * @param alreadyHandledProjects The already handled {@link IProject} that don't need to be analysed again.
 * @return The found locations.//from  w  ww.  j  a va 2  s . c o m
 * @throws JavaModelException 
 */
private static List<File> getLocationFor(IJavaProject javaProject, IClasspathEntry entry, int expectedKind,
        Set<IProject> alreadyHandledProjects) throws JavaModelException {
    if (entry != null) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                || entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                || entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                || entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            List<File> result = new LinkedList<File>();
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            for (IPackageFragmentRoot root : roots) {
                if (root.getKind() == expectedKind) {
                    if (root.getResource() != null) {
                        if (root.getResource().getLocationURI() != null) {
                            result.add(ResourceUtil.getLocation(root.getResource()));
                        }
                    } else if (root.getPath() != null) {
                        File location = new File(root.getPath().toString());
                        if (location.exists()) {
                            result.add(location);
                        }
                    }
                }
            }
            return result; // Ignore containers
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            Assert.isNotNull(entry.getPath());
            IResource project = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            Assert.isTrue(project instanceof IProject);
            if (!alreadyHandledProjects.contains(project)) {
                return getSourceLocations((IProject) project, alreadyHandledProjects);
            } else {
                return null; // Project was already analyzed, no need to do it again.
            }
        } else {
            Assert.isTrue(false, "Unknown content kind \"" + entry.getContentKind()
                    + "\" of class path entry \"" + entry + "\".");
            return null;
        }
    } else {
        return null;
    }
}

From source file:org.maven.ide.eclipse.tests.BuildPathManagerTest.java

License:Apache License

public void testEnableMavenNature() throws Exception {
    deleteProject("MNGECLIPSE-248parent");
    deleteProject("MNGECLIPSE-248child");

    final IProject project1 = createProject("MNGECLIPSE-248parent", "projects/MNGECLIPSE-248parent/pom.xml");
    final IProject project2 = createProject("MNGECLIPSE-248child", "projects/MNGECLIPSE-248child/pom.xml");

    NullProgressMonitor monitor = new NullProgressMonitor();
    BuildPathManager buildpathManager = Maven2Plugin.getDefault().getBuildpathManager();

    ResolverConfiguration configuration = new ResolverConfiguration();
    buildpathManager.enableMavenNature(project1, configuration, monitor);
    //    buildpathManager.updateSourceFolders(project1, monitor);

    buildpathManager.enableMavenNature(project2, configuration, monitor);
    //    buildpathManager.updateSourceFolders(project2, monitor);

    //    waitForJob("Initializing " + project1.getProject().getName());
    //    waitForJob("Initializing " + project2.getProject().getName());
    waitForJobsToComplete();/*from w ww.j a va  2s.  c o m*/

    IClasspathEntry[] project1entries = getMavenContainerEntries(project1);
    assertEquals(1, project1entries.length);
    assertEquals(IClasspathEntry.CPE_LIBRARY, project1entries[0].getEntryKind());
    assertTrue(project1entries[0].getPath().lastSegment().equals("junit-4.1.jar"));

    IClasspathEntry[] project2entries = getMavenContainerEntries(project2);
    assertEquals(2, project2entries.length);
    assertEquals(IClasspathEntry.CPE_PROJECT, project2entries[0].getEntryKind());
    assertTrue(project2entries[0].getPath().segment(0).equals("MNGECLIPSE-248parent"));
    assertEquals(IClasspathEntry.CPE_LIBRARY, project2entries[1].getEntryKind());
    assertTrue(project2entries[1].getPath().lastSegment().equals("junit-4.1.jar"));
}

From source file:org.neuro4j.studio.core.util.ClassloaderHelper.java

License:Apache License

private static void collectClasspathURLs(IJavaProject javaProject, List<URL> urls, Set<IJavaProject> visited,
        boolean isFirstProject) {
    if (visited.contains(javaProject))
        return;/*from  w  w w. j  a  v a2  s.  com*/
    visited.add(javaProject);
    IPath outPath = getJavaProjectOutputAbsoluteLocation(javaProject.getProject());
    if (outPath != null) {
        outPath = outPath.addTrailingSeparator();
        URL out = createFileURL(outPath);
        urls.add(out);

    }

    IClasspathEntry[] entries = null;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e) {
        return;
    }
    IClasspathEntry entry;
    for (int i = 0; i < entries.length; i++) {
        entry = entries[i];
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            collectClasspathEntryURL(entry, urls);
            break;
        case IClasspathEntry.CPE_CONTAINER:
        case IClasspathEntry.CPE_VARIABLE:
            collectClasspathEntryURL(entry, urls);
            break;
        case IClasspathEntry.CPE_PROJECT: {
            if (isFirstProject || entry.isExported())
                collectClasspathURLs(getJavaProject(entry), urls, visited, false);
            break;
        }
        }
    }
}

From source file:org.neuro4j.studio.core.util.ClassloaderHelper.java

License:Apache License

private static void collectClasspathIPath(IJavaProject javaProject, List<IPath> urls, Set<IJavaProject> visited,
        boolean isFirstProject) {
    if (visited.contains(javaProject))
        return;/*from www.j  a v  a2 s .c om*/
    visited.add(javaProject);

    IClasspathEntry[] entries = null;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e) {
        return;
    }
    IClasspathEntry entry;
    for (int i = 0; i < entries.length; i++) {
        entry = entries[i];
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
            addJarToList(entry.getPath(), urls);
            break;
        case IClasspathEntry.CPE_CONTAINER:
        case IClasspathEntry.CPE_SOURCE:
            break;
        case IClasspathEntry.CPE_VARIABLE:

            addJarToList(entry.getPath(), urls);
            break;
        case IClasspathEntry.CPE_PROJECT: {
            if (isFirstProject || entry.isExported())
                collectClasspathIPath(getJavaProject(entry), urls, visited, false);
            break;
        }
        }
    }
}

From source file:org.objectstyle.wolips.eomodeler.eclipse.EclipseEOModelGroupFactory.java

License:Open Source License

protected void addModelsFromProject(EOModelGroup modelGroup, IProject project, Set<Object> searchedResources,
        Set<IProject> searchedProjects, Set<EOModelVerificationFailure> failures, boolean skipOnDuplicates,
        IProgressMonitor progressMonitor, int depth) throws IOException, EOModelException, CoreException {
    if (!searchedProjects.contains(project)) {
        progressMonitor.setTaskName("Adding models from " + project.getName() + " ...");
        searchedProjects.add(project);/* ww w . ja  v a 2s .c om*/
        if (!project.exists()) {
            failures.add(new EOModelVerificationFailure(null,
                    "The dependent project '" + project.getName() + "' does not exist.", false));
        } else if (!project.isOpen()) {
            failures.add(new EOModelVerificationFailure(null,
                    "The dependent project '" + project.getName() + "' exists but is not open.", false));
        } else {
            boolean visitedProject = false;
            boolean isJavaProject = project.getNature(JavaCore.NATURE_ID) != null;
            IClasspathEntry[] classpathEntries = null;
            if (isJavaProject) {
                IJavaProject javaProject = JavaCore.create(project);
                classpathEntries = javaProject.getResolvedClasspath(true);
            } else {
                classpathEntries = new IClasspathEntry[0];
            }
            boolean showProgress = (depth == 0);
            if (showProgress) {
                progressMonitor.beginTask("Scanning " + project.getName() + " classpath ...",
                        classpathEntries.length + 1);
            }
            for (int classpathEntryNum = 0; classpathEntryNum < classpathEntries.length; classpathEntryNum++) {
                IClasspathEntry entry = classpathEntries[classpathEntryNum];
                int entryKind = entry.getEntryKind();
                if (entryKind == IClasspathEntry.CPE_LIBRARY) {
                    List<IPath> jarPaths = new LinkedList<IPath>();
                    IPath path = entry.getPath();
                    IPath frameworkPath = null;
                    while (frameworkPath == null && path.lastSegment() != null) {
                        String lastSegment = path.lastSegment();
                        if (lastSegment != null && lastSegment.endsWith(".framework")) {
                            frameworkPath = path;
                        } else {
                            if (lastSegment != null && lastSegment.endsWith(".jar")) {
                                // MS: This is really annoying, but it appears that a jar in your project looks the
                                // same as an absolute jar path reference outside your project.  I don't know
                                // how to tell them apart, so I check to see if the jar is in your project 
                                // before we fallback to the old way.
                                IFile jarInProject = project.getWorkspace().getRoot().getFile(path);
                                if (jarInProject.exists()) {
                                    jarPaths.add(jarInProject.getLocation());
                                } else {
                                    jarPaths.add(path);
                                }
                            }
                            path = path.removeLastSegments(1);
                        }
                    }

                    if (frameworkPath != null) {
                        File resourcesFolder = frameworkPath.append("Resources").toFile();
                        if (!searchedResources.contains(resourcesFolder) && resourcesFolder.exists()) {
                            searchedResources.add(resourcesFolder);
                            modelGroup.loadModelsFromURL(resourcesFolder.toURL(), 1, failures, skipOnDuplicates,
                                    progressMonitor);
                        }
                    }

                    for (IPath jarPath : jarPaths) {
                        URL jarResourcesURL = new URL("jar:" + jarPath.toFile().toURL() + "!/Resources");
                        if (!searchedResources.contains(jarResourcesURL) && URLUtils.exists(jarResourcesURL)) {
                            modelGroup.loadModelsFromURL(jarResourcesURL, 1, failures, skipOnDuplicates,
                                    progressMonitor);
                        }
                    }
                } else if (entryKind == IClasspathEntry.CPE_PROJECT) {
                    IPath path = entry.getPath();
                    IProject dependsOnProject = ResourcesPlugin.getWorkspace().getRoot()
                            .getProject(path.lastSegment());
                    addModelsFromProject(modelGroup, dependsOnProject, searchedResources, searchedProjects,
                            failures, skipOnDuplicates, progressMonitor, depth + 1);
                } else if (entryKind == IClasspathEntry.CPE_SOURCE) {
                    visitedProject = true;
                    project.accept(new ModelVisitor(project, modelGroup, searchedResources, failures,
                            skipOnDuplicates, progressMonitor), IResource.DEPTH_INFINITE,
                            IContainer.EXCLUDE_DERIVED);
                }
                if (showProgress) {
                    progressMonitor.worked(1);
                }
            }

            if (!visitedProject) {
                project.accept(new ModelVisitor(project, modelGroup, searchedResources, failures,
                        skipOnDuplicates, progressMonitor), IResource.DEPTH_INFINITE,
                        IContainer.EXCLUDE_DERIVED);
                if (showProgress) {
                    progressMonitor.worked(1);
                }
            }
        }
    }
}

From source file:org.objectstyle.wolips.jrebel.utils.WOProjectClassLoader.java

License:BSD License

private void addURLs(IJavaProject javaProject, boolean exportsOnly) {
    if (!javaProjects.contains(javaProject)) {
        javaProjects.add(javaProject);/*w w w  .  jav a2 s .  c  om*/

        try {
            // Add default output location
            addURL(javaProject.getOutputLocation());

            // Add each classpath entry
            IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
            for (IClasspathEntry classpathEntry : classpathEntries) {
                if (classpathEntry.isExported() || !exportsOnly) {
                    switch (classpathEntry.getEntryKind()) {

                    // Recurse on projects
                    case IClasspathEntry.CPE_PROJECT:
                        IProject project = javaProject.getProject().getWorkspace().getRoot()
                                .getProject(classpathEntry.getPath().toString());
                        IJavaProject javaProj = JavaCore.create(project);
                        if (javaProj != null) {
                            addURLs(javaProj, true);
                        }
                        break;

                    // Library
                    case IClasspathEntry.CPE_LIBRARY:
                        addURL(classpathEntry);
                        break;

                    // Only Source entries with custom output location need to be added
                    case IClasspathEntry.CPE_SOURCE:
                        IPath outputLocation = classpathEntry.getOutputLocation();
                        if (outputLocation != null) {
                            addURL(outputLocation);
                        }
                        break;

                    // Variable and Container entries should not be happening, because
                    // we've asked for resolved entries.
                    case IClasspathEntry.CPE_VARIABLE:
                    case IClasspathEntry.CPE_CONTAINER:
                        break;
                    }
                }
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
            // log.debug("MalformedURLException occurred: " + e.getLocalizedMessage(),e);
        }
    }
}

From source file:org.opendaylight.yangide.core.indexing.IndexAllProject.java

License:Open Source License

@Override
public boolean execute(IProgressMonitor progressMonitor) {
    log.info("[I] Project: {}", project.getName());

    if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled()) {
        return true;
    }/*w  w w . j  a va  2s . c o  m*/

    if (!this.project.isAccessible()) {
        return true;
    }
    final Set<IPath> ignoredPath = new HashSet<>();
    final Set<IPath> externalJarsPath = new HashSet<>();
    try {
        JavaProject proj = (JavaProject) JavaCore.create(project);
        final Set<String> projectScope = new HashSet<>();
        projectScope.add(project.getName());

        if (proj != null) {
            IClasspathEntry[] classpath = proj.getResolvedClasspath();
            for (IClasspathEntry entry : classpath) {
                IPath entryPath = entry.getPath();
                IPath output = entry.getOutputLocation();
                if (output != null && !entryPath.equals(output)) {
                    ignoredPath.add(output);
                }

                // index dependencies projects
                if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                    IProject prj = ResourcesPlugin.getWorkspace().getRoot()
                            .getProject(entry.getPath().lastSegment());
                    if (prj != null && prj.exists()) {
                        this.manager.indexAll(prj);
                        projectScope.add(prj.getName());
                    }
                }
            }
            IPackageFragmentRoot[] roots = proj.getAllPackageFragmentRoots();
            for (IPackageFragmentRoot root : roots) {
                IPath entryPath = root.getPath();
                if (entryPath != null && entryPath.toFile().exists()
                        && entryPath.lastSegment().toLowerCase().endsWith(".jar")) {
                    externalJarsPath.add(entryPath);
                }
            }
            // Update project information with set of project dependencies
            YangProjectInfo yangProjectInfo = (YangProjectInfo) YangCorePlugin.create(project)
                    .getElementInfo(null);
            yangProjectInfo.setProjectScope(projectScope);
            // fill indirect scope
            Set<String> indirectScope = new HashSet<>();
            indirectScope.add(project.getName());
            for (IJavaProject jproj : JavaCore.create(ResourcesPlugin.getWorkspace().getRoot())
                    .getJavaProjects()) {
                if (jproj != proj) {
                    for (String name : jproj.getRequiredProjectNames()) {
                        if (name.equals(project.getName())) {
                            indirectScope.add(jproj.getProject().getName());
                        }
                    }
                }
            }
            yangProjectInfo.setIndirectScope(indirectScope);
        }
    } catch (JavaModelException | YangModelException e) {
        // java project doesn't exist: ignore
    }

    for (IPath path : externalJarsPath) {
        try (JarFile jarFile = new JarFile(path.toFile())) {
            ZipEntry entry = jarFile.getEntry("META-INF/yang/");
            if (entry != null) {
                this.manager.addJarFile(project, path);
            }
        } catch (IOException e) {
            YangCorePlugin.log(e);
        }
    }
    try {
        final HashSet<IFile> indexedFiles = new HashSet<>();
        project.accept(proxy -> {
            if (IndexAllProject.this.isCancelled) {
                return false;
            }
            if (!ignoredPath.isEmpty() && ignoredPath.contains(proxy.requestFullPath())) {
                return false;
            }
            if (proxy.getType() == IResource.FILE) {
                if (CoreUtil.isYangLikeFileName(proxy.getName())) {
                    IFile file = (IFile) proxy.requestResource();
                    indexedFiles.add(file);
                }
                return false;
            }
            return true;
        }, IResource.NONE);

        for (IFile file : indexedFiles) {
            this.manager.addSource(file);
        }
    } catch (CoreException e) {
        this.manager.removeIndexFamily(project);
        return false;
    }
    return true;
}