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

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

Introduction

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

Prototype

void setRawClasspath(IClasspathEntry[] entries, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Sets the classpath of this project using a list of classpath entries.

Usage

From source file:org.eclipse.jst.j2ee.web.project.facet.WebFacetInstallDelegateForWebFragmentProjects.java

License:Open Source License

protected static void addToClasspath(final IJavaProject jproj, final IClasspathEntry entry)
        throws CoreException {
    final IClasspathEntry[] existingEntries = jproj.getRawClasspath();
    for (IClasspathEntry existingEntry : existingEntries) {
        if (existingEntry.equals(entry)) {
            return;
        }//from   ww  w. jav  a 2  s  .c  om
    }
    final IClasspathEntry[] updated = new IClasspathEntry[existingEntries.length + 1];
    System.arraycopy(existingEntries, 0, updated, 0, existingEntries.length);
    updated[existingEntries.length] = entry;
    jproj.setRawClasspath(updated, null);
}

From source file:org.eclipse.jst.jsf.core.internal.jsflibraryconfig.JSFLibraryRegistryUtil.java

License:Open Source License

/**
 * Sets the raw classpath on a project and logs an error if it when a JavaModelException occurs
 * @param project/*from ww w. j av a  2  s  .  c o  m*/
 * @param cpEntries
 * @param monitor
 */
public static void setRawClasspath(IJavaProject project, List cpEntries, IProgressMonitor monitor) {
    IClasspathEntry[] entries = (IClasspathEntry[]) cpEntries.toArray(new IClasspathEntry[0]);
    try {
        project.setRawClasspath(entries, monitor);
    } catch (JavaModelException e) {
        JSFCorePlugin.log(e, "Unable to set classpath for: " + project.getProject().getName()); //$NON-NLS-1$
    }
}

From source file:org.eclipse.jst.jsf.core.internal.project.facet.LegacyJSFLibraryProviderUninstallOperation.java

License:Open Source License

public void execute(final LibraryProviderOperationConfig config, final IProgressMonitor monitor)

        throws CoreException

{
    monitor.beginTask("", 1); //$NON-NLS-1$

    try {//w ww.j  a  va 2  s. co  m
        final IProject project = config.getFacetedProject().getProject();
        final IJavaProject jproj = JavaCore.create(project);
        final List<IClasspathEntry> newcp = new ArrayList<IClasspathEntry>();

        for (IClasspathEntry cpe : jproj.getRawClasspath()) {
            if (!LegacyJSFLibraryProviderDetector.detect(cpe)) {
                newcp.add(cpe);
            }
        }

        final IClasspathEntry[] array = newcp.toArray(new IClasspathEntry[newcp.size()]);
        jproj.setRawClasspath(array, null);

        monitor.worked(1);
    } finally {
        monitor.done();
    }
}

From source file:org.eclipse.jst.jsp.core.tests.taglibindex.BundleResourceUtil.java

License:Open Source License

/**
 * Add a library entry (like a jar) to the classpath of a project. The jar
 * must be in your poject. You can copy the jar into your workspace using
 * copyBundleEntryIntoWorkspace(String entryname, String fullPath)
 * /*from w ww.  j  ava  2s  .c  o  m*/
 * @param proj
 *            assumed it has java nature
 * @param pathToJar
 *            project relative, no leading slash
 */
public static void addLibraryEntry(IProject proj, String pathToJar) {

    IPath projLocation = proj.getLocation();
    IPath absJarPath = projLocation.append(pathToJar);

    IJavaProject jProj = JavaCore.create(proj);

    IClasspathEntry strutsJarEntry = JavaCore.newLibraryEntry(absJarPath, null, null);
    try {
        IClasspathEntry[] currentEntries = jProj.getRawClasspath();

        List l = new ArrayList();
        l.addAll(Arrays.asList(currentEntries));
        l.add(strutsJarEntry);

        IClasspathEntry[] newEntries = (IClasspathEntry[]) l.toArray(new IClasspathEntry[l.size()]);
        jProj.setRawClasspath(newEntries, new NullProgressMonitor());
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:org.eclipse.jst.jsp.core.tests.taglibindex.BundleResourceUtil.java

License:Open Source License

public static void addWebContainer(IProject proj) {
    IJavaProject jProj = JavaCore.create(proj);
    try {/*from   w  ww . j a v a2s  .  c  o m*/
        IClasspathEntry[] entries = jProj.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && "org.eclipse.jst.jsp.core.tests.webContainerInitializer"
                            .equals(entries[i].getPath().segment(0))) {
                return;
            }
        }
        List newEntries = new ArrayList();
        newEntries.addAll(Arrays.asList(entries));
        newEntries.add(
                JavaCore.newContainerEntry(new Path("org.eclipse.jst.jsp.core.tests.webContainerInitializer")));
        jProj.setRawClasspath((IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]),
                null);
    } catch (JavaModelException e) {

    }
}

From source file:org.eclipse.jst.jsp.core.tests.taglibindex.TestIndex.java

License:Open Source License

public void testAvailableFromExportedOnBuildpathFromAnotherProject() throws Exception {
    TaglibIndex.shutdown();// ww  w .  java2  s.c  o m

    // Create project 1
    IProject project = BundleResourceUtil.createSimpleProject("testavailable1", null, null);
    assertTrue(project.isAccessible());
    BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/testavailable1", "/testavailable1");

    // Create project 2
    IProject project2 = BundleResourceUtil.createSimpleProject("testavailable2", null, null);
    assertTrue(project2.isAccessible());
    BundleResourceUtil.copyBundleEntriesIntoWorkspace("/testfiles/testavailable2", "/testavailable2");
    BundleResourceUtil.copyBundleEntryIntoWorkspace("/testfiles/bug_118251-sample/sample_tld.jar",
            "/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar");

    TaglibIndex.startup();

    // make sure project 1 sees no taglibs
    ITaglibRecord[] records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable1/WebContent"));
    assertEquals("ITaglibRecords were found", 0, records.length);
    // make sure project 2 sees two taglibs
    ITaglibRecord[] records2 = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable2/WebContent"));
    if (records2.length != 2) {
        for (int i = 0; i < records2.length; i++) {
            System.err.println(records2[i]);
        }
    }
    assertEquals("total ITaglibRecord count doesn't match", 2, records2.length);

    TaglibIndex.shutdown();
    TaglibIndex.startup();

    records2 = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable2/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match after restart", 2, records2.length);

    IJavaProject created = JavaCore.create(project2);
    assertTrue("/availabletest2 not a Java project", created.exists());

    // export the jar from project 2
    IClasspathEntry[] entries = created.getRawClasspath();
    boolean found = false;
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(new Path("/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar"))) {
            found = true;
            assertFalse("was exported", entry.isExported());
            ((ClasspathEntry) entry).isExported = true;
        }
    }
    assertTrue("/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar was not found in build path", found);
    IClasspathEntry[] entries2 = new IClasspathEntry[entries.length];
    System.arraycopy(entries, 1, entries2, 0, entries.length - 1);
    entries2[entries.length - 1] = entries[0];
    created.setRawClasspath(entries2, new NullProgressMonitor());

    entries = created.getRawClasspath();
    found = false;
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(new Path("/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar"))) {
            found = true;
            assertTrue("/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar was not exported",
                    ((ClasspathEntry) entry).isExported);
        }
    }
    assertTrue(
            "/testavailable2/WebContent/WEB-INF/lib/sample_tld.jar was not found (and exported) in build path",
            found);

    // project 2 should still have just two taglibs
    records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable2/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match (after exporting jar)", 2, records.length);

    // now one taglib should be visible from project 1
    records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable1/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match (after exporting jar), classpath provider problem?",
            1, records.length);

    TaglibIndex.shutdown();
    TaglibIndex.startup();

    // project 2 should still have just two taglibs
    records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable2/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match (after exporting jar and restarting)", 2,
            records.length);

    // and one taglib should still be visible from project 1
    records = TaglibIndex.getAvailableTaglibRecords(new Path("/testavailable1/WebContent"));
    assertEquals("total ITaglibRecord count doesn't match (after exporting jar and restarting)", 1,
            records.length);
}

From source file:org.eclipse.jst.ws.axis.consumption.core.tests.util.Util.java

License:Open Source License

/**
 * Adds Axis jars and the JRE to the classpath of the given project.
 * @param javaProject The project to add axis.jar to.
 * @throws JavaModelException If the jar could not be added.
 */// w w  w.  ja v a2  s.c om
public static void addRequiredJarsToJavaProject(IJavaProject javaProject) throws CoreException {
    // Get the current classpath as a list.
    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    List list = new LinkedList(java.util.Arrays.asList(classpath));

    // Add the JRE to the list.
    IClasspathEntry con = JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER"));
    list.add(con);

    // Add the Axis jars to the list.
    String[] jars = new String[] { "org.apache.axis", "axis.jar", "org.apache.axis",
            "commons-discovery-0.2.jar", "org.apache.commons_logging", "commons-logging-1.0.4.jar",
            "org.apache.axis", "jaxrpc.jar", "org.apache.jakarta_log4j", "log4j-1.2.8.jar", "org.apache.axis",
            "saaj.jar", "org.apache.axis", "wsdl4j-1.5.1.jar" };

    for (int i = 0; i < jars.length; i += 2) {
        IPath jar = getAxisPluginJarPath(jars[i], jars[i + 1]);
        System.out.println("Adding jar [" + jar.toString() + "]");
        IClasspathEntry jarEntry = JavaCore.newLibraryEntry(jar, null, null);
        list.add(jarEntry);
    }

    // Update the current classpath from the list.
    IClasspathEntry[] newClasspath = (IClasspathEntry[]) list.toArray(new IClasspathEntry[0]);
    javaProject.setRawClasspath(newClasspath, null);
}

From source file:org.eclipse.jst.ws.internal.axis.consumption.ui.task.CopyAxisJarCommand.java

License:Open Source License

/**
 * Update the Java project classpath adding classpath from newJarNamesList
 * to oldClasspath// w  w  w.  jav  a 2  s. c  om
 * 
 * @param monitor
 * @return
 * @throws JavaModelException
 */
private IStatus updateClasspath(IProgressMonitor monitor) throws JavaModelException {

    IStatus status = Status.OK_STATUS;

    if (newJarNamesList.size() > 0) {
        JarEntry[] newJarEntries = (JarEntry[]) newJarNamesList.toArray(new JarEntry[] {});

        IClasspathEntry[] newClasspath = new IClasspathEntry[oldClasspath.length + newJarEntries.length];
        int i = 0;
        // Add oldClasspath entries
        while (i < oldClasspath.length) {
            newClasspath[i] = oldClasspath[i];
            i++;
        }

        int m = 0;
        while (i < newClasspath.length) {
            newClasspath[i] = JavaCore.newLibraryEntry(newJarEntries[m].getJarPath(), null, null);
            m++;
            i++;
        }

        //
        // Then update the project classpath.
        //

        IJavaProject javaProject = JavaCore.create(project);
        javaProject.setRawClasspath(newClasspath, monitor);

    }

    return status;

}

From source file:org.eclipse.jst.ws.internal.consumption.command.common.CreateJavaProjectCommand.java

License:Open Source License

public IStatus execute(IProgressMonitor monitor, IAdaptable adaptable) {
    IEnvironment env = getEnvironment();
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName_);

    if (project != null && !project.exists()) {
        try {//from w  w  w .  j  a v  a  2 s.com
            project.create(ResourcesPlugin.getWorkspace().newProjectDescription(project.getName()), monitor);
            project.open(monitor);
            IProjectDescription desc = project.getDescription();
            desc.setNatureIds(new String[] { JavaCore.NATURE_ID });
            ICommand cmd = desc.newCommand();
            cmd.setBuilderName(JavaCore.BUILDER_ID);
            desc.setBuildSpec(new ICommand[] { cmd });
            project.setDescription(desc, monitor);
            IJavaProject javaProject = JavaCore.create(project);
            Path projectRoot = new Path(Path.ROOT.append(new Path(project.getName())).toString());
            javaProject.setRawClasspath(new IClasspathEntry[] { JavaCore.newSourceEntry(projectRoot),
                    JavaCore.newContainerEntry(new Path(JavaRuntime.JRE_CONTAINER)) }, monitor);
            javaProject.setOutputLocation(projectRoot, monitor);
        } catch (CoreException ce) {
            IStatus status = StatusUtils
                    .errorStatus(NLS.bind(ConsumptionMessages.MSG_ERROR_CANNOT_CREATE_JAVA_PROJECT,
                            new String[] { projectName_ }), ce);
            env.getStatusHandler().reportError(status);
            return status;
        }
    }
    return Status.OK_STATUS;
}

From source file:org.eclipse.jst.ws.internal.consumption.ui.common.FacetOperationDelegate.java

License:Open Source License

private void fixEJBClassPath(IFacetedProject project) {
    if (!J2EEUtils.isEJBComponent(project.getProject()))
        return;//from  w  w w. ja va  2  s  .c om
    IProject ejbProject = project.getProject();
    IJavaProject javaProject = JavaCore.create(ejbProject);
    Path projectRoot = new Path(Path.ROOT.append(new Path(ejbProject.getName())).toString());
    IPath ejbModulePath = projectRoot.append("ejbModule");
    try {
        IClasspathEntry[] originalSet = javaProject.getRawClasspath();
        boolean foundEJBModulEntry = false;
        for (IClasspathEntry entry : originalSet) {
            if (entry.getPath().equals(ejbModulePath))
                foundEJBModulEntry = true;
        }
        if (!foundEJBModulEntry) {
            IClasspathEntry[] newSet = new IClasspathEntry[originalSet.length + 1];
            int i = 0;

            for (IClasspathEntry entry : originalSet) {
                newSet[i++] = entry;
            }
            newSet[i] = JavaCore.newSourceEntry(ejbModulePath);
            javaProject.setRawClasspath(newSet, null);
        }
    } catch (Exception e) {
        // TODO: handle exception
    }

}