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.andmore.internal.project.LibraryClasspathContainerInitializer.java

License:Open Source License

private static IClasspathContainer allocateContainer(IJavaProject javaProject, List<IClasspathEntry> entries,
        IPath id, String description) {

    if (AndmoreAndroidPlugin.getDefault() == null) { // This is totally weird, but I've seen it happen!
        return null;
    }//from  w  ww .  j a v  a  2 s .c o m

    // First check that the project has a library-type container.
    try {
        IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
        final IClasspathEntry[] oldRawClasspath = rawClasspath;

        boolean foundContainer = false;
        for (IClasspathEntry entry : rawClasspath) {
            // get the entry and kind
            final int kind = entry.getEntryKind();

            if (kind == IClasspathEntry.CPE_CONTAINER) {
                String path = entry.getPath().toString();
                String idString = id.toString();
                if (idString.equals(path)) {
                    foundContainer = true;
                    break;
                }
            }
        }

        // if there isn't any, add it.
        if (foundContainer == false) {
            // add the android container to the array
            rawClasspath = ProjectHelper.addEntryToClasspath(rawClasspath,
                    JavaCore.newContainerEntry(id, true /*isExported*/));
        }

        // set the new list of entries to the project
        if (rawClasspath != oldRawClasspath) {
            javaProject.setRawClasspath(rawClasspath, new NullProgressMonitor());
        }
    } catch (JavaModelException e) {
        // This really shouldn't happen, but if it does, simply return null (the calling
        // method will fails as well)
        return null;
    }

    return new AndroidClasspathContainer(entries.toArray(new IClasspathEntry[entries.size()]), id, description,
            IClasspathContainer.K_APPLICATION);
}

From source file:org.eclipse.andmore.internal.project.ProjectHelper.java

License:Open Source License

/**
 * Fix the project classpath entries. The method ensures that:
 * <ul>/* ww  w.jav a 2 s  .c om*/
 * <li>The project does not reference any old android.zip/android.jar archive.</li>
 * <li>The project does not use its output folder as a sourc folder.</li>
 * <li>The project does not reference a desktop JRE</li>
 * <li>The project references the AndroidClasspathContainer.
 * </ul>
 * @param javaProject The project to fix.
 * @throws JavaModelException
 */
public static void fixProjectClasspathEntries(IJavaProject javaProject) throws JavaModelException {

    // get the project classpath
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IClasspathEntry[] oldEntries = entries;
    boolean forceRewriteOfCPE = false;

    // check if the JRE is set as library
    int jreIndex = ProjectHelper.findClasspathEntryByPath(entries, JavaRuntime.JRE_CONTAINER,
            IClasspathEntry.CPE_CONTAINER);
    if (jreIndex != -1) {
        // the project has a JRE included, we remove it
        entries = ProjectHelper.removeEntryFromClasspath(entries, jreIndex);
    }

    // get the output folder
    IPath outputFolder = javaProject.getOutputLocation();

    boolean foundFrameworkContainer = false;
    IClasspathEntry foundLibrariesContainer = null;
    IClasspathEntry foundDependenciesContainer = null;

    for (int i = 0; i < entries.length;) {
        // get the entry and kind
        IClasspathEntry entry = entries[i];
        int kind = entry.getEntryKind();

        if (kind == IClasspathEntry.CPE_SOURCE) {
            IPath path = entry.getPath();

            if (path.equals(outputFolder)) {
                entries = ProjectHelper.removeEntryFromClasspath(entries, i);

                // continue, to skip the i++;
                continue;
            }
        } else if (kind == IClasspathEntry.CPE_CONTAINER) {
            String path = entry.getPath().toString();
            if (AndmoreAndroidConstants.CONTAINER_FRAMEWORK.equals(path)) {
                foundFrameworkContainer = true;
            } else if (AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES.equals(path)) {
                foundLibrariesContainer = entry;
            } else if (AndmoreAndroidConstants.CONTAINER_DEPENDENCIES.equals(path)) {
                foundDependenciesContainer = entry;
            }
        }

        i++;
    }

    // look to see if we have the m2eclipse nature
    boolean m2eNature = false;
    try {
        m2eNature = javaProject.getProject().hasNature("org.eclipse.m2e.core.maven2Nature");
    } catch (CoreException e) {
        AndmoreAndroidPlugin.log(e, "Failed to query project %s for m2e nature",
                javaProject.getProject().getName());
    }

    // if the framework container is not there, we add it
    if (!foundFrameworkContainer) {
        // add the android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_FRAMEWORK)));
    }

    // same thing for the library container
    if (foundLibrariesContainer == null) {
        // add the exported libraries android container to the array
        entries = ProjectHelper.addEntryToClasspath(entries, JavaCore
                .newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES), true));
    } else if (!m2eNature && !foundLibrariesContainer.isExported()) {
        // the container is present but it's not exported and since there's no m2e nature
        // we do want it to be exported.
        // keep all the other parameters the same.
        entries = ProjectHelper.replaceEntryInClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_PRIVATE_LIBRARIES),
                        foundLibrariesContainer.getAccessRules(), foundLibrariesContainer.getExtraAttributes(),
                        true));
        forceRewriteOfCPE = true;
    }

    // same thing for the dependencies container
    if (foundDependenciesContainer == null) {
        // add the android dependencies container to the array
        entries = ProjectHelper.addEntryToClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES), true));
    } else if (!m2eNature && !foundDependenciesContainer.isExported()) {
        // the container is present but it's not exported and since there's no m2e nature
        // we do want it to be exported.
        // keep all the other parameters the same.
        entries = ProjectHelper.replaceEntryInClasspath(entries,
                JavaCore.newContainerEntry(new Path(AndmoreAndroidConstants.CONTAINER_DEPENDENCIES),
                        foundDependenciesContainer.getAccessRules(),
                        foundDependenciesContainer.getExtraAttributes(), true));
        forceRewriteOfCPE = true;
    }

    // set the new list of entries to the project
    if (entries != oldEntries || forceRewriteOfCPE) {
        javaProject.setRawClasspath(entries, new NullProgressMonitor());
    }

    // If needed, check and fix compiler compliance and source compatibility
    ProjectHelper.checkAndFixCompilerCompliance(javaProject);
}

From source file:org.eclipse.ant.internal.ui.datatransfer.ProjectCreator.java

License:Open Source License

private IJavaProject createJavaProject(String projectName, IProgressMonitor monitor) throws CoreException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(projectName);
    if (!project.exists()) {
        project.create(monitor);/*from w ww . ja  v a2 s  .c  o  m*/
    } else {
        project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
    }

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

    if (!project.hasNature(JavaCore.NATURE_ID)) {
        addNatureToProject(project, JavaCore.NATURE_ID, monitor);
    }

    IJavaProject jproject = JavaCore.create(project);

    jproject.setRawClasspath(new IClasspathEntry[0], monitor);

    return jproject;
}

From source file:org.eclipse.ant.internal.ui.datatransfer.ProjectCreator.java

License:Open Source License

private void addToClasspath(IJavaProject jproject, IClasspathEntry cpe, IProgressMonitor monitor)
        throws JavaModelException {
    IClasspathEntry[] oldEntries = jproject.getRawClasspath();
    for (int i = 0; i < oldEntries.length; i++) {
        if (oldEntries[i].equals(cpe)) {
            return;
        }/*w  ww. j a v a2s  . c  om*/
    }
    int nEntries = oldEntries.length;
    IClasspathEntry[] newEntries = new IClasspathEntry[nEntries + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, nEntries);
    newEntries[nEntries] = cpe;
    jproject.setRawClasspath(newEntries, monitor);
}

From source file:org.eclipse.birt.report.designer.ui.ide.wizards.NewReportProjectWizard.java

License:Open Source License

private void setClasspath(IProject project) throws JavaModelException, CoreException {
    IJavaProject javaProject = JavaCore.create(project);

    if (outputText.getText() != null && outputText.getText().trim().length() > 0) {
        IPath path = project.getFullPath().append(outputText.getText());
        javaProject.setOutputLocation(path, null);
    }/*  w ww. j  av a 2 s  .  com*/

    IClasspathEntry[] entries = getClassPathEntries(project);
    javaProject.setRawClasspath(entries, null);
}

From source file:org.eclipse.birt.report.designer.ui.samples.ide.action.IDEOpenSampleReportAction.java

License:Open Source License

private void setClasspath(IProject project) throws JavaModelException, CoreException {
    IJavaProject javaProject = JavaCore.create(project);

    IPath path = project.getFullPath().append("bin"); //$NON-NLS-1$
    javaProject.setOutputLocation(path, null);

    IClasspathEntry[] entries = getClassPathEntries(project);
    javaProject.setRawClasspath(entries, null);
}

From source file:org.eclipse.buildship.core.workspace.internal.DefaultWorkspaceOperations.java

License:Open Source License

private void setSourcesAndClasspathOnProject(IJavaProject javaProject, ClasspathDefinition classpath,
        IProgressMonitor monitor) {/*  ww  w.  j a v  a 2s. com*/
    monitor.beginTask(String.format("Configure sources and classpath for Eclipse project %s",
            javaProject.getProject().getName()), 12);
    try {
        // create a new holder for all classpath entries
        Builder<IClasspathEntry> entries = ImmutableList.builder();

        // add the library with the JRE dependencies
        entries.add(JavaCore.newContainerEntry(classpath.getJrePath()));
        monitor.worked(1);

        // add classpath definition of where to store the external dependencies, the classpath
        // will be populated lazily by the org.eclipse.jdt.core.classpathContainerInitializer
        // extension point (see GradleClasspathContainerInitializer)
        entries.add(createClasspathContainerForExternalDependencies());
        monitor.worked(1);

        // add project dependencies
        entries.addAll(collectProjectDependencies(classpath));
        monitor.worked(1);

        // add source directories; create the directory if it doesn't exist
        entries.addAll(collectSourceDirectories(classpath, javaProject));
        monitor.worked(1);

        // assign the whole classpath at once to the project
        List<IClasspathEntry> entriesArray = entries.build();
        javaProject.setRawClasspath(entriesArray.toArray(new IClasspathEntry[entriesArray.size()]),
                new SubProgressMonitor(monitor, 8));
    } catch (Exception e) {
        String message = String.format("Cannot configure sources and classpath for Eclipse project %s.",
                javaProject.getProject().getName());
        CorePlugin.logger().error(message, e);
        throw new GradlePluginsRuntimeException(message, e);
    } finally {
        monitor.done();
    }
}

From source file:org.eclipse.buildship.core.workspace.internal.LibraryFilter.java

License:Open Source License

public static void update(IJavaProject eclipseProject, OmniEclipseProject modelProject,
        IProgressMonitor monitor) throws JavaModelException {
    if (supportsClasspathCustomization(modelProject)) {
        IClasspathEntry[] newClasspath = filterLibraries(eclipseProject.getRawClasspath());
        eclipseProject.setRawClasspath(newClasspath, monitor);
    }//from w ww.j av  a  2  s .  co m
}

From source file:org.eclipse.buildship.core.workspace.internal.WtpClasspathUpdater.java

License:Open Source License

private static void replaceGradleClasspathContainerAttribute(IJavaProject project, String plusKey,
        String plusValue, String minusKey, SubMonitor progress) throws JavaModelException {
    IClasspathEntry[] oldClasspath = project.getRawClasspath();
    IClasspathEntry[] newClasspath = new IClasspathEntry[oldClasspath.length];
    for (int i = 0; i < oldClasspath.length; i++) {
        IClasspathEntry entry = oldClasspath[i];
        if (isGradleClasspathContainer(entry)) {
            IClasspathAttribute[] attributes = replaceClasspathAttribute(entry.getExtraAttributes(), plusKey,
                    plusValue, minusKey);
            newClasspath[i] = JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), attributes,
                    entry.isExported());
        } else {/*from w w  w .java  2 s  .  co m*/
            newClasspath[i] = entry;
        }
    }
    project.setRawClasspath(newClasspath, progress);
}

From source file:org.eclipse.buildship.wtp.core.configurator.WebApplicationConfigurator.java

License:Open Source License

/**
 * Makes the Gradle container deployable by modifying its classpath entry.
 *//* ww  w .j  a  va 2 s .  c o m*/
private void makeGradleContainerDeployable(ProjectConfigurationRequest projectConfigurationRequest,
        IProgressMonitor monitor) throws JavaModelException {
    IProject workspaceProject = projectConfigurationRequest.getWorkspaceProject();
    IJavaProject javaProject = JavaCore.create(workspaceProject);

    List<IClasspathEntry> classpathEntries = Arrays.asList(javaProject.getRawClasspath());
    List<IClasspathEntry> newEntries = FluentIterable.from(classpathEntries)
            .transform(new Function<IClasspathEntry, IClasspathEntry>() {

                @Override
                public IClasspathEntry apply(IClasspathEntry entry) {
                    String path = entry.getPath().toString();
                    if (path.equals(GRADLE_CLASSPATH_CONTAINER_PATH)) {
                        IClasspathEntry newGradleContainerEntry = markAsDeployable(entry);
                        return newGradleContainerEntry;
                    } else {
                        return entry;
                    }
                }
            }).toList();

    javaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[newEntries.size()]), monitor);
}