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.pde.api.tools.tests.util.ProjectUtils.java

License:Open Source License

/**
 * Removes the specified entry from the classpath of the specified project
 * // w w w  .ja va  2 s . co  m
 * @param project
 * @param entry
 * @throws JavaModelException
 */
public static void removeFromClasspath(IJavaProject project, IClasspathEntry entry) throws JavaModelException {
    IClasspathEntry[] oldEntries = project.getRawClasspath();
    ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    for (int i = 0; i < oldEntries.length; i++) {
        if (!oldEntries[i].equals(entry)) {
            entries.add(oldEntries[i]);
        }
    }
    if (entries.size() != oldEntries.length) {
        project.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]),
                new NullProgressMonitor());
    }
}

From source file:org.eclipse.pde.internal.core.project.ProjectModifyOperation.java

License:Open Source License

/**
 * Configures the build path and output location of the described Java project.
 * If the Java project existed before this operation, new build path entries are
 * added for the bundle class path, if required, but we don't change the exiting
 * build path./*from  w w  w .  jav  a  2  s . co  m*/
 * 
 * @param description desired project description
 * @param before state before the operation
 * @param existed whether the Java project existed before the operation
 */
private void configureJavaProject(IBundleProjectDescription description, IBundleProjectDescription before,
        boolean existed) throws CoreException {
    IProject project = description.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    // create source folders as required
    IBundleClasspathEntry[] bces = description.getBundleClasspath();
    if (bces != null && bces.length > 0) {
        for (int i = 0; i < bces.length; i++) {
            IPath folder = bces[i].getSourcePath();
            if (folder != null) {
                CoreUtility.createFolder(project.getFolder(folder));
            }
        }
    }
    // Set default output folder
    if (description.getDefaultOutputFolder() != null) {
        IPath path = project.getFullPath().append(description.getDefaultOutputFolder());
        javaProject.setOutputLocation(path, null);
    }

    // merge the class path if the project existed before
    IBundleClasspathEntry[] prev = before.getBundleClasspath();
    if (!isEqual(bces, prev)) {
        if (existed) {
            // add entries not already present
            IClasspathEntry[] entries = getSourceFolderEntries(javaProject, description);
            IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
            List<IClasspathEntry> add = new ArrayList<IClasspathEntry>();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                boolean present = false;
                for (int j = 0; j < rawClasspath.length; j++) {
                    IClasspathEntry existingEntry = rawClasspath[j];
                    if (existingEntry.getEntryKind() == entry.getEntryKind()) {
                        if (existingEntry.getPath().equals(entry.getPath())) {
                            present = true;
                            break;
                        }
                    }
                }
                if (!present) {
                    add.add(entry);
                }
            }
            // check if the 'required plug-ins' container is present
            boolean addRequired = false;
            if (description.hasNature(IBundleProjectDescription.PLUGIN_NATURE)) {
                addRequired = true;
                for (int i = 0; i < rawClasspath.length; i++) {
                    IClasspathEntry cpe = rawClasspath[i];
                    if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        if (PDECore.REQUIRED_PLUGINS_CONTAINER_PATH.equals(cpe.getPath())) {
                            addRequired = false;
                            break;
                        }
                    }
                }
            }
            if (addRequired) {
                add.add(ClasspathComputer.createContainerEntry());
            }
            if (!add.isEmpty()) {
                List<IClasspathEntry> all = new ArrayList<IClasspathEntry>();
                for (int i = 0; i < rawClasspath.length; i++) {
                    all.add(rawClasspath[i]);
                }
                all.addAll(add);
                javaProject.setRawClasspath(all.toArray(new IClasspathEntry[all.size()]), null);
            }
        } else {
            IClasspathEntry[] entries = getClassPathEntries(javaProject, description);
            javaProject.setRawClasspath(entries, null);
        }
    }
}

From source file:org.eclipse.pde.internal.core.SearchablePluginsManager.java

License:Open Source License

private void computeClasspath(IJavaProject project, IProgressMonitor monitor) {
    IClasspathEntry[] classpath = new IClasspathEntry[2];
    classpath[0] = JavaCore.newContainerEntry(JavaRuntime.newDefaultJREContainerPath());
    classpath[1] = JavaCore.newContainerEntry(PDECore.JAVA_SEARCH_CONTAINER_PATH);
    try {/*from   w  w w. ja v a2s  .  co  m*/
        project.setRawClasspath(classpath, monitor);
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.pde.internal.ui.editor.plugin.LibrarySection.java

License:Open Source License

private void updateJavaClasspathLibs(String[] oldPaths, String[] newPaths) {
    IProject project = ((IModel) getPage().getModel()).getUnderlyingResource().getProject();
    IJavaProject jproject = JavaCore.create(project);
    try {//from   ww  w  .  j  a  va2  s .c o  m
        IClasspathEntry[] entries = jproject.getRawClasspath();
        ArrayList<IClasspathEntry> toBeAdded = new ArrayList<IClasspathEntry>();
        int index = -1;
        entryLoop: for (int i = 0; i < entries.length; i++) {
            if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                if (index == -1)
                    index = i;
                // do not add the old paths (handling deletion/renaming)
                IPath path = entries[i].getPath().removeFirstSegments(1).removeTrailingSeparator();
                for (int j = 0; j < oldPaths.length; j++)
                    if (oldPaths[j] != null && path.equals(new Path(oldPaths[j]).removeTrailingSeparator()))
                        continue entryLoop;
            } else if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                if (index == -1)
                    index = i;
            toBeAdded.add(entries[i]);
        }
        if (index == -1)
            index = entries.length;

        // add paths
        for (int i = 0; i < newPaths.length; i++) {
            if (newPaths[i] == null)
                continue;
            IClasspathEntry entry = JavaCore.newLibraryEntry(project.getFullPath().append(newPaths[i]), null,
                    null, true);
            if (!toBeAdded.contains(entry))
                toBeAdded.add(index++, entry);
        }

        if (toBeAdded.size() == entries.length)
            return;

        IClasspathEntry[] updated = toBeAdded.toArray(new IClasspathEntry[toBeAdded.size()]);
        jproject.setRawClasspath(updated, null);
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.pde.internal.ui.wizards.feature.AbstractCreateFeatureOperation.java

License:Open Source License

private void createProject(IProgressMonitor monitor) throws CoreException {
    CoreUtility.createProject(fProject, fLocation, monitor);
    fProject.open(monitor);/*  ww w.  j  a v  a 2 s  .c om*/
    IProjectDescription desc = fProject.getWorkspace().newProjectDescription(fProject.getName());
    desc.setLocation(fLocation);
    if (!fProject.hasNature(PDE.FEATURE_NATURE))
        CoreUtility.addNatureToProject(fProject, PDE.FEATURE_NATURE, monitor);

    if (fFeatureData.hasCustomHandler()) {
        if (!fProject.hasNature(JavaCore.NATURE_ID))
            CoreUtility.addNatureToProject(fProject, JavaCore.NATURE_ID, monitor);

        if (fFeatureData.getSourceFolderName() != null
                && fFeatureData.getSourceFolderName().trim().length() > 0) {
            IFolder folder = fProject.getFolder(fFeatureData.getSourceFolderName());
            if (!folder.exists())
                CoreUtility.createFolder(folder);
        }

        IJavaProject jproject = JavaCore.create(fProject);
        jproject.setOutputLocation(fProject.getFullPath().append(fFeatureData.getJavaBuildFolderName()),
                monitor);
        jproject.setRawClasspath(new IClasspathEntry[] {
                JavaCore.newSourceEntry(fProject.getFullPath().append(fFeatureData.getSourceFolderName())),
                JavaCore.newContainerEntry(new Path(JavaRuntime.JRE_CONTAINER)) }, monitor);
    }
}

From source file:org.eclipse.pde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

protected void updateReferences(IProgressMonitor monitor, IProject project) throws JavaModelException {
    IJavaProject currentProject = JavaCore.create(project);
    IPluginModelBase[] pluginstoUpdate = fData.getPluginsToUpdate();
    for (int i = 0; i < pluginstoUpdate.length; ++i) {
        IProject proj = pluginstoUpdate[i].getUnderlyingResource().getProject();
        if (currentProject.getProject().equals(proj))
            continue;
        IJavaProject javaProject = JavaCore.create(proj);
        IClasspathEntry[] cp = javaProject.getRawClasspath();
        IClasspathEntry[] updated = getUpdatedClasspath(cp, currentProject);
        if (updated != null) {
            javaProject.setRawClasspath(updated, monitor);
        }/*  ww w . j av a2  s .  co m*/
        try {
            updateRequiredPlugins(javaProject, monitor, pluginstoUpdate[i]);
        } catch (CoreException e) {
            PDEPlugin.log(e);
        }
    }
}

From source file:org.eclipse.pde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

private static void updateRequiredPlugins(IJavaProject javaProject, IProgressMonitor monitor,
        IPluginModelBase model) throws CoreException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List<IClasspathEntry> classpath = new ArrayList<IClasspathEntry>();
    List<IClasspathEntry> requiredProjects = new ArrayList<IClasspathEntry>();
    for (int i = 0; i < entries.length; i++) {
        if (isPluginProjectEntry(entries[i])) {
            requiredProjects.add(entries[i]);
        } else {/*from  w w  w .  ja v a 2 s  . c o  m*/
            classpath.add(entries[i]);
        }
    }
    if (requiredProjects.size() <= 0)
        return;
    IFile file = PDEProject.getManifest(javaProject.getProject());
    try {
        // TODO format manifest
        Manifest manifest = new Manifest(file.getContents());
        String value = manifest.getMainAttributes().getValue(Constants.REQUIRE_BUNDLE);
        StringBuffer sb = value != null ? new StringBuffer(value) : new StringBuffer();
        if (sb.length() > 0)
            sb.append(","); //$NON-NLS-1$
        for (int i = 0; i < requiredProjects.size(); i++) {
            IClasspathEntry entry = requiredProjects.get(i);
            if (i > 0)
                sb.append(","); //$NON-NLS-1$
            sb.append(entry.getPath().segment(0));
            if (entry.isExported())
                sb.append(";visibility:=reexport"); // TODO is there a //$NON-NLS-1$
            // constant?
        }
        manifest.getMainAttributes().putValue(Constants.REQUIRE_BUNDLE, sb.toString());
        ByteArrayOutputStream content = new ByteArrayOutputStream();
        manifest.write(content);
        file.setContents(new ByteArrayInputStream(content.toByteArray()), true, false, monitor);
        // now update .classpath
        javaProject.setRawClasspath(classpath.toArray(new IClasspathEntry[classpath.size()]), monitor);
        //         ClasspathComputer.setClasspath(javaProject.getProject(), model);
    } catch (IOException e) {
    } catch (CoreException e) {
    }
}

From source file:org.eclipse.pde.internal.ui.wizards.tools.ConvertProjectToPluginOperation.java

License:Open Source License

private void loadClasspathEntries(IProject project, IProgressMonitor monitor) {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] currentClassPath = new IClasspathEntry[0];
    ArrayList<String> sources = new ArrayList<String>();
    ArrayList<String> libraries = new ArrayList<String>();
    try {/* w  w w .  j  a v a 2s .  c om*/
        currentClassPath = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
    }
    for (int i = 0; i < currentClassPath.length; i++) {
        int contentType = currentClassPath[i].getEntryKind();
        if (contentType == IClasspathEntry.CPE_SOURCE) {
            String relativePath = getRelativePath(currentClassPath[i], project);
            if (relativePath.equals("")) { //$NON-NLS-1$
                sources.add("."); //$NON-NLS-1$
            } else {
                sources.add(relativePath + "/"); //$NON-NLS-1$
            }
        } else if (contentType == IClasspathEntry.CPE_LIBRARY) {
            String path = getRelativePath(currentClassPath[i], project);
            if (path.length() > 0)
                libraries.add(path);
            else
                libraries.add("."); //$NON-NLS-1$
        }
    }
    fSrcEntries = sources.toArray(new String[sources.size()]);
    fLibEntries = libraries.toArray(new String[libraries.size()]);

    IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1];
    System.arraycopy(currentClassPath, 0, classPath, 0, currentClassPath.length);
    classPath[classPath.length - 1] = ClasspathComputer.createContainerEntry();
    try {
        javaProject.setRawClasspath(classPath, monitor);
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.pde.ui.tests.project.ProjectCreationTests.java

License:Open Source License

/**
 * Convert an existing Java project into a bundle project. Ensure it's build path
 * doesn't get toasted in the process./*w w  w.  ja  v a 2s . com*/
 * 
 * @throws CoreException
 */
public void testJavaToBundle() throws CoreException {
    // create a Java project
    String name = getName().toLowerCase().substring(4);
    name = "test." + name;
    IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
    assertFalse("Project should not exist", proj.exists());
    proj.create(null);
    proj.open(null);
    IProjectDescription pd = proj.getDescription();
    pd.setNatureIds(new String[] { JavaCore.NATURE_ID });
    proj.setDescription(pd, null);
    IFolder src = proj.getFolder("someSrc");
    src.create(false, true, null);
    IFolder output = proj.getFolder("someBin");
    output.create(false, true, null);
    IJavaProject javaProject = JavaCore.create(proj);
    javaProject.setOutputLocation(output.getFullPath(), null);
    IClasspathEntry entry1 = JavaCore.newSourceEntry(src.getFullPath());
    IClasspathEntry entry2 = JavaCore.newContainerEntry(JavaRuntime
            .newJREContainerPath(JavaRuntime.getExecutionEnvironmentsManager().getEnvironment("J2SE-1.4")));
    IClasspathEntry entry3 = JavaCore.newContainerEntry(ClasspathContainerInitializer.PATH);
    javaProject.setRawClasspath(new IClasspathEntry[] { entry1, entry2, entry3 }, null);

    // convert to a bundle
    IBundleProjectDescription description = getBundleProjectService().getDescription(proj);
    assertTrue("Missing Java Nature", description.hasNature(JavaCore.NATURE_ID));
    description.setSymbolicName(proj.getName());
    description.setNatureIds(new String[] { IBundleProjectDescription.PLUGIN_NATURE, JavaCore.NATURE_ID });
    IBundleClasspathEntry entry = getBundleProjectService()
            .newBundleClasspathEntry(src.getProjectRelativePath(), null, null);
    description.setBundleClasspath(new IBundleClasspathEntry[] { entry });
    description.apply(null);

    // validate
    IBundleProjectDescription d2 = getBundleProjectService().getDescription(proj);
    assertEquals("Wrong symbolic name", proj.getName(), d2.getSymbolicName());
    String[] natureIds = d2.getNatureIds();
    assertEquals("Wrong number of natures", 2, natureIds.length);
    assertEquals("Wrong nature", IBundleProjectDescription.PLUGIN_NATURE, natureIds[0]);
    assertTrue("Nature should be present", d2.hasNature(IBundleProjectDescription.PLUGIN_NATURE));
    assertEquals("Wrong nature", JavaCore.NATURE_ID, natureIds[1]);
    // execution environment should be that on the Java build path
    String[] ees = d2.getExecutionEnvironments();
    assertNotNull("Missing EEs", ees);
    assertEquals("Wrong number of EEs", 1, ees.length);
    assertEquals("Wrong EE", "J2SE-1.4", ees[0]);
    // version
    assertEquals("Wrong version", "1.0.0.qualifier", d2.getBundleVersion().toString());

    IBundleClasspathEntry[] classpath = d2.getBundleClasspath();
    assertEquals("Wrong number of Bundle-Classpath entries", 1, classpath.length);
    assertEquals("Wrong Bundle-Classpath entry", getBundleProjectService()
            .newBundleClasspathEntry(src.getProjectRelativePath(), null, new Path(".")), classpath[0]);

    // raw class path should still be intact
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    assertEquals("Wrong number of entries", 4, rawClasspath.length);
    assertEquals("Wrong entry", entry1, rawClasspath[0]);
    assertEquals("Wrong entry", entry2, rawClasspath[1]);
    assertEquals("Wrong entry", entry3, rawClasspath[2]);
    assertEquals("Missing Required Plug-ins Container", ClasspathComputer.createContainerEntry(),
            rawClasspath[3]);
}

From source file:org.eclipse.recommenders.internal.coordinates.rcp.EclipseDependencyListenerTest.java

License:Open Source License

private static void appendJreToClasspath(IJavaProject javaProject) throws Exception {
    IClasspathEntry jreEntry = JavaRuntime.getDefaultJREContainerEntry();
    javaProject.setRawClasspath(concat(javaProject.getRawClasspath(), jreEntry), new NullProgressMonitor());
}