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

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

Introduction

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

Prototype

int CPE_LIBRARY

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

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a library.

Usage

From source file:org.eclipse.xtext.junit4.ui.util.JavaProjectSetupUtil.java

License:Open Source License

public static void removeJarFromClasspath(IJavaProject from, IFile to) throws CoreException {
    removeFromClasspath(from, IClasspathEntry.CPE_LIBRARY, to.getFullPath());
}

From source file:org.eclipsejc.builder.CapBuilder.java

License:Open Source License

private IPath getConverterPath(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(false);

    for (IClasspathEntry entry : resolvedClasspath) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY)
            continue;

        IPath path = entry.getPath();//  w  w  w.  j  a va 2  s .c  o m
        if (!path.lastSegment().equals("api.jar"))
            continue;

        IPath libPath = path.removeLastSegments(1);
        return libPath.append("converter.jar");
    }
    return null;
}

From source file:org.eclipselabs.jar2uml.JarToUML.java

License:Open Source License

/**
 * Adds all relevant class file paths for javaProject
 * @param javaProject//from w  w w .  j a  va2s  .c o  m
 * @param includeWorkspaceReferences Include referenced projects and jar files in workspace
 * @throws JavaModelException 
 * @throws IOException 
 */
public void addPaths(IJavaProject javaProject, boolean includeWorkspaceReferences)
        throws JavaModelException, IOException {
    for (IClasspathEntry cpe : javaProject.getResolvedClasspath(true)) {
        IPath cpePath;
        switch (cpe.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            cpePath = cpe.getOutputLocation();
            if (cpePath == null) {
                cpePath = javaProject.getOutputLocation();
            }
            IContainer container = (IContainer) ResourcesPlugin.getWorkspace().getRoot().findMember(cpePath);
            addPath(container);
            break;
        case IClasspathEntry.CPE_LIBRARY:
            cpePath = cpe.getPath();
            IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(cpePath);
            if ((resource != null)
                    && (includeWorkspaceReferences || javaProject.getProject().equals(resource.getProject()))) {
                if (resource instanceof IFile) {
                    addCpJar(new JarFile(resource.getLocation().toFile()));
                } else if (resource instanceof IContainer) {
                    addCpPath((IContainer) resource);
                } else {
                    throw new IOException(String
                            .format(JarToUMLResources.getString("JarToUML.unexpectedResourceKind"), resource)); //$NON-NLS-1$
                }
            }
            break;
        }
    }
    if (includeWorkspaceReferences) {
        Set<IJavaProject> refs = new HashSet<IJavaProject>();
        findJavaProjectReferences(javaProject, refs);
        for (IJavaProject ref : refs) {
            addPaths(ref, includeWorkspaceReferences);
        }
    }
}

From source file:org.entirej.ide.core.project.EJPluginEntireJClassLoader.java

License:Apache License

private static void processEntry(IJavaProject javaProject, List<URL> urlList, IClasspathEntry entry,
        boolean ignoreSource) throws MalformedURLException {
    // This source output ... always included & exported
    if (!ignoreSource && entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
        IPath outputLocation = entry.getOutputLocation();

        if (outputLocation != null) {
            URL url = new URL("file", null, outputLocation.toString() + "/");
            urlList.add(url);//from  ww  w .  j  ava2s . c  o  m
        }
    }

    // Referenced project classpath. If this project is exported,
    // Then all *exported* entries are exported with respect to this
    // project,
    else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
        IProject ijproject = ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().segment(0));
        IJavaProject ref = JavaCore.create(ijproject);
        Collection<URL> cpEntries = getClasspathEntries(ref, false);
        urlList.addAll(cpEntries);
    }

    // This is the Directories classpath
    else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
        IPath entryPath = entry.getPath();
        URL url = new URL("file", null, entryPath.toString());
        IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath);

        if (res != null && res.exists()) {
            url = new URL("file", null, res.getLocation().toString());
        }
        urlList.add(url);
    }
    // This is Library classpath
    else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
        IPath entryPath = entry.getPath();
        URL url = new URL("file", null, entryPath.toString());

        IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath);
        if (res != null && res.exists()) {
            url = new URL("file", null, res.getLocation().toString());
        }
        urlList.add(url);
    }
    // This is Variables classpath
    else if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
        String variableName = entry.getPath().segment(0);
        IPath variablePath = JavaCore.getClasspathVariable(variableName);
        if (variablePath != null) {
            URL url = new URL("file", null, variablePath.toString());
            urlList.add(url);
        }
    }
}

From source file:org.evosuite.eclipse.popup.actions.TestGenerationAction.java

License:Open Source License

/**
 * If we generate JUnit tests, we need to make sure that JUnit is on the
 * classpath of the project, otherwise we will see compile errors
 * //  w  w w .  j a va 2s. co  m
 * @param project
 */
public void fixJUnitClassPath(IJavaProject project) {
    IPath junitPath = JUnitCore.JUNIT4_CONTAINER_PATH;

    boolean hasJUnit = false;
    boolean hasEvoSuite = false;
    boolean hasOldEvoSuite = false;

    try {
        Path containerPath = new Path("org.evosuite.eclipse.classpathContainerInitializer");
        IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, project);
        System.out.println("EvoSuite JAR at: " + container.getPath().toOSString());

        IClasspathEntry[] oldEntries = project.getRawClasspath();
        ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(oldEntries.length + 1);

        IClasspathEntry cpentry = JavaCore.newContainerEntry(junitPath);

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

            // Check if JUnit is already in the build path
            if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath path = curr.getPath();
                if (path.equals(cpentry.getPath())) {
                    hasJUnit = true;
                }
                if (path.equals(container.getPath())) {
                    hasEvoSuite = true;
                }
            } else if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                // Check for older EvoSuite entries
                IPath path = curr.getPath();
                if (path.toFile().getName().equals(Activator.EVOSUITE_JAR)) {
                    if (path.equals(container.getPath())) {
                        System.out.println("Is current evosuite!");
                        hasEvoSuite = true;
                    } else {
                        System.out.println("Is NOT current evosuite!");
                        hasOldEvoSuite = true;
                        continue;
                    }
                }
                if (path.equals(cpentry.getPath())) {
                    hasJUnit = true;
                }
                if (path.equals(container.getPath())) {
                    hasEvoSuite = true;
                }
            }

            if (curr != null) {
                newEntries.add(curr);
            }
        }

        if (hasJUnit && hasEvoSuite && !hasOldEvoSuite) {
            return;
        }

        // add the entry
        if (!hasJUnit) {
            newEntries.add(cpentry);
        }

        if (!hasEvoSuite && container != null) {
            for (IClasspathEntry entry : container.getClasspathEntries()) {
                newEntries.add(entry);
            }
        }

        System.out.println("New classpath: " + newEntries);

        // newEntries.add(JavaCore.newContainerEntry(EvoSuiteClasspathContainer.ID));

        // Convert newEntries to an array
        IClasspathEntry[] newCPEntries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
        project.setRawClasspath(newCPEntries, null);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }

}

From source file:org.evosuite.eclipse.popup.actions.TestGenerationJob.java

License:Open Source License

private String buildProjectCP() throws JavaModelException {
    IJavaProject jProject = JavaCore.create(target.getProject());
    IClasspathEntry[] oldEntries = jProject.getRawClasspath();
    String classPath = "";
    boolean first = true;

    for (int i = 0; i < oldEntries.length; i++) {
        IClasspathEntry curr = oldEntries[i];
        System.out.println("Current entry: " + curr.getPath());

        if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
            IPath path = curr.getPath();
            if (path.toFile().getName().startsWith("evosuite")) {
                System.out.println("Skipping evosuite.jar");
                continue;
            }/*from  w ww .  j  a  v a  2  s. co m*/
            if (!first)
                classPath += File.pathSeparator;
            else
                first = false;

            if (path.toFile().exists()) {
                classPath += path.toOSString();
                System.out.println("Adding CPE_LIBRARY to classpath: " + path.toOSString());
            } else {
                classPath += target.getWorkspace().getRoot().getLocation().toOSString() + path.toOSString();
                System.out.println("Adding CPE_LIBRARY to classpath: "
                        + target.getWorkspace().getRoot().getLocation().toOSString() + path.toOSString());
            }
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            if (curr.isExported()) {
                if (curr.toString().equals("org.eclipse.jdt.launching.JRE_CONTAINER")) {
                    System.out.println("Found JRE container");
                } else if (curr.toString().startsWith("org.eclipse.jdt.junit.JUNIT_CONTAINER")) {
                    System.out.println("Found JUnit container");
                } else {
                    System.out.println("Found unknown container: " + curr);
                }
            } else {
                System.out.println("Container not exported: " + curr);
            }
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            // Add binary dirs of this project to classpath
            System.out.println("Don't handle CPE_PROJECT yet");
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
            System.out.println("Path: " + curr.getPath());
            System.out.println("Resolved Path: " + JavaCore.getResolvedVariablePath(curr.getPath()));
            if (!first)
                classPath += File.pathSeparator;
            else
                first = false;

            classPath += JavaCore.getResolvedVariablePath(curr.getPath());
        } else if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            System.out.println("Don't handle CPE_SOURCE yet");
        } else {
            System.out.println("CP type: " + curr.getEntryKind());
        }
    }
    ResourceList.resetAllCaches();
    if (!first)
        classPath += File.pathSeparator;

    classPath += target.getWorkspace().getRoot().findMember(jProject.getOutputLocation()).getLocation()
            .toOSString();
    return classPath;
}

From source file:org.flowerplatform.editor.java.propertypage.remote.JavaProjectPropertyPageService.java

License:Open Source License

public Object getClasspathEntries(ServiceInvocationContext context, List<PathFragment> path) {
    @SuppressWarnings("unchecked")
    Pair<File, String> node = (Pair<File, String>) GenericTreeStatefulService.getNodeByPathFor(path, null);
    File projectFile = node.a;//from ww w .  ja  v  a 2 s .co  m
    File wd = ProjectsService.getInstance().getProjectToWorkingDirectoryAndIProjectMap().get(projectFile).a;

    IProject project = ProjectsService.getInstance().getProjectToWorkingDirectoryAndIProjectMap()
            .get(projectFile).b;
    IJavaProject javaProject = JavaCore.create(project);

    List<String> srcFolders = new ArrayList<String>();
    List<String> projects = new ArrayList<String>();
    List<String> libraries = new ArrayList<String>();

    try {
        if (!project.getFile(IJavaProject.CLASSPATH_FILE_NAME).exists()) {
            return null;
        }
        @SuppressWarnings("restriction")
        IClasspathEntry[][] entries = ((JavaProject) javaProject).readFileEntriesWithException(null);
        for (IClasspathEntry entry : entries[0]) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                srcFolders.add(entry.getPath()
                        .makeRelativeTo(project.getFolder(ProjectsService.LINK_TO_PROJECT).getFullPath())
                        .toFile().getPath());
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                File file = ProjectsService.getInstance().getFileFromProjectWrapperResource(
                        ResourcesPlugin.getWorkspace().getRoot().getProject(entry.getPath().lastSegment()));
                projects.add(CommonPlugin.getInstance().getPathRelativeToFile(file, wd));
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY
                    && entry.getContentKind() == IPackageFragmentRoot.K_BINARY) {
                IFile resource = ResourcesPlugin.getWorkspace().getRoot().getFile(entry.getPath());
                File file = ProjectsService.getInstance().getFileFromProjectWrapperResource(resource);
                libraries.add(CommonPlugin.getInstance().getPathRelativeToFile(file, wd));
            }
        }

    } catch (CoreException | IOException e) {
        logger.error("Exception thrown while getting java classpath entries for {}", project.getName(), e);
        return null;
    }
    return new Object[] { srcFolders, projects, libraries };
}

From source file:org.fusesource.tools.core.Classpath.java

License:Open Source License

public Collection<IPath> getLibs() {
    return getPaths(IClasspathEntry.CPE_LIBRARY);
}

From source file:org.grails.ide.eclipse.commands.test.GrailsCommandUtilTest.java

License:Open Source License

/**
 * This test isn't actually testing stuff from {@link GrailsCommandUtils}, but it is in here because
 * it uses some similar checks that a project is properly setup after doing a "raw" import of project
 * created outside eclipse using grails commands.
 * <p>/*w w w .ja va2  s  .com*/
 * A "raw" import means simply pulling in a project from files from somewhere outside eclipse and doing 
 * nothing more than adding them as a new project to the workspace. As of Grails 1.3.5 this should produce
 * a correctly configured grails project (grails 1.3.5 produces correct .project etc. files). So there
 * should be no need to call 'eclipsify' on such projects.
 */
public void testImportExistingProject() throws Throwable {
    GrailsProjectVersionFixer.globalAskToConvertToGrailsProjectAnswer = true;
    System.out.println(">>>>>>>>>>>>>>>>>>>testImportExistingProject");
    GroovyCompilerVersionCheck.testMode();
    ensureDefaultGrailsVersion(GrailsVersion.V_1_3_5); // GrailsVersion of test project!
    StsTestUtil.setAutoBuilding(true);
    System.out.println("Waiting for autobuild...");
    StsTestUtil.waitForAutoBuild();
    System.out.println("Done autobuild");

    URL bareBonesURL = this.getClass().getClassLoader().getResource("external-project.zip");

    String projectName = "External";
    // Create bare bones project from zip file
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

    IPath unzipLoc = workspaceRoot.getLocation();
    ZipFileUtil.unzip(bareBonesURL, unzipLoc.toFile(), new NullProgressMonitor());

    project = workspaceRoot.getProject(projectName);
    project.create(null);

    if (!project.isOpen()) {
        project.open(null);
    }

    assertTrue(project.isAccessible());
    assertTrue(project.getFolder("grails-app").exists());

    /////////////////////////////////////////////////////////////////////////////////////////////////
    //Check a few things about this test project

    assertTrue(project.hasNature(JavaCore.NATURE_ID)); // Should have Java Nature at this point
    assertTrue(GroovyNature.hasGroovyNature(project)); // Should also have Groovy nature

    final IJavaProject javaProject = JavaCore.create(project);

    //Next we check the classpath related stuff. 
    // The classpath may not be right initially... but should eventually become correct as a background
    // refresh dependency job should get scheduled. 
    // ACondition
    new ACondition() {
        @Override
        public boolean test() throws Exception {
            System.out.println("Checking project config...");
            assertEquals("/" + project.getName() + "/target-eclipse/classes",
                    javaProject.getOutputLocation().toString());

            ///////////////////////////////////
            // Check resolved classpath stuff
            IClasspathEntry[] classPath = javaProject.getResolvedClasspath(false);

            // A whole bunch of libraries should be there, check for just a few of those

            // this one will fail on macs
            //            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "jre/lib/rt.jar", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "grails-core", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "grails-bootstrap", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "groovy-all", classPath);
            //assertClassPathEntry(IClasspathEntry.CPE_LIBRARY, "servlet-api", classPath);

            //            System.out.println(">>>Resolved classpath");
            //            for (IClasspathEntry entry : classPath) {
            //               System.out.println(kindString(entry.getEntryKind())+": "+entry.getPath());
            //            }
            //            System.out.println("<<<Resolved classpath");

            ///////////////////////////////////
            // Check raw classpath stuff
            classPath = javaProject.getRawClasspath();
            //            for (IClasspathEntry classpathEntry : classPath) {
            //               System.out.println(kindString(classpathEntry.getEntryKind())+": "+classpathEntry.getPath());
            //            }

            //The usual source folders:
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "src/java", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "src/groovy", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/conf", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/controllers", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/domain", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/services", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "grails-app/taglib", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "test/integration", classPath);
            assertClassPathEntry(IClasspathEntry.CPE_SOURCE, "test/unit", classPath);

            //The classpath containers for Java and Grails
            assertClassPathEntry(IClasspathEntry.CPE_CONTAINER, "org.eclipse.jdt.launching.JRE_CONTAINER",
                    classPath);
            assertClassPathEntry(IClasspathEntry.CPE_CONTAINER,
                    "org.grails.ide.eclipse.core.CLASSPATH_CONTAINER", classPath);

            //Installed plugins source folders
            assertDefaultPluginSourceFolders(project);

            assertCharset(project);

            System.out.println("Checking project config => OK!");
            return true;
        }
    }.waitFor(60000);

    System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<testImportExistingProject");
}

From source file:org.grails.ide.eclipse.commands.test.PluginInstallerTests.java

License:Open Source License

/**
 * Regression test for./*from ww w  .java 2 s .  co  m*/
 * https://issuetracker.springsource.com/browse/STS-1502
 */
public void testSTS1502PluginWithJarDependency() throws Exception {
    assertPluginsInstalled(GrailsTest.defaultPlugins());
    IStatus status = GrailsPluginInstaller.performPluginChanges(null,
            getLatestPluginVersions("spring-security-core"), installable, null);
    GrailsTest.assertStatusOK(status);
    new ACondition() {
        @Override
        public boolean test() throws Exception {
            IClasspathEntry[] classpath = JavaCore.create(installable).getResolvedClasspath(true);
            assertClassPathEntry(IClasspathEntry.CPE_LIBRARY,
                    Pattern.compile("spring-security-core|org.springframework.security.core"), classpath);
            return true;
        }

    }.waitFor(500000);

    boolean webxml = isPluginInstalled("webxml");
    if (webxml) {
        //in later versions, spring-security-core plugin pulls in the "webxml" plugin?
        status = GrailsPluginInstaller.performPluginChanges(
                getLatestPluginVersions("spring-security-core", "webxml"), null, installable, null);
    } else {
        status = GrailsPluginInstaller.performPluginChanges(getLatestPluginVersions("spring-security-core"),
                null, installable, null);
    }
    GrailsTest.assertStatusOK(status);
    assertPluginsInstalled(GrailsTest.defaultPlugins());
}