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.common.project.facet.core.internal.ClasspathUtil.java

License:Open Source License

public static void addClasspathEntry(final IJavaProject project, final IClasspathEntry cpe)

        throws CoreException

{
    validateClasspathEdit(project);/*from  w  ww.  ja  v a2 s  . c om*/

    final IClasspathEntry[] cpOld = project.getRawClasspath();
    final IClasspathEntry[] cpNew = new IClasspathEntry[cpOld.length + 1];
    System.arraycopy(cpOld, 0, cpNew, 0, cpOld.length);
    cpNew[cpOld.length] = cpe;
    project.setRawClasspath(cpNew, null);
}

From source file:org.eclipse.jst.common.project.facet.core.internal.JavaFacetInstallDelegate.java

License:Open Source License

public void execute(final IProject project, final IProjectFacetVersion fv, final Object cfg,
        final IProgressMonitor monitor)

        throws CoreException

{
    validateEdit(project);/*from  w w  w  . j  a  v  a 2 s  .  c  o  m*/

    final JavaFacetInstallConfig config = castToConfig(cfg);

    // Create the source and the output directories.

    IJavaProject jproject = null;

    if (project.exists()) {
        jproject = JavaCore.create(project);
    }

    if (!jproject.exists()) {
        final List<IClasspathEntry> cp = new ArrayList<IClasspathEntry>();

        for (IPath srcFolderPath : config.getSourceFolders()) {
            final IFolder folder = createFolder(project, srcFolderPath, false);
            cp.add(JavaCore.newSourceEntry(folder.getFullPath()));
        }

        final IPath defOutputPath = config.getDefaultOutputFolder();
        IFolder defOutputFolder = null;

        if (defOutputPath != null) {
            defOutputFolder = createFolder(project, config.getDefaultOutputFolder(), true);
        }

        // Add the java nature. This will automatically add the builder.

        final IProjectDescription desc = project.getDescription();
        final String[] current = desc.getNatureIds();
        final String[] replacement = new String[current.length + 1];
        System.arraycopy(current, 0, replacement, 0, current.length);
        replacement[current.length] = JavaCore.NATURE_ID;
        desc.setNatureIds(replacement);
        project.setDescription(desc, null);

        // Setup the classpath.

        if (defOutputFolder == null) {
            jproject.setRawClasspath(cp.toArray(new IClasspathEntry[cp.size()]), null);
        } else {
            jproject.setRawClasspath(cp.toArray(new IClasspathEntry[cp.size()]), defOutputFolder.getFullPath(),
                    null);
        }

        JavaFacetUtil.resetClasspath(project, null, fv);
        JavaFacetUtil.setCompilerLevel(project, fv);
    } else {
        // Set the compiler compliance level for the project. Ignore whether
        // this might already be set so at the workspace level in case
        // workspace settings change later or the project is included in a
        // different workspace.

        String oldCompilerLevel = JavaFacetUtil.getCompilerLevel(project);
        JavaFacetUtil.setCompilerLevel(project, fv);

        String newCompilerLevel = JavaFacetUtil.getCompilerLevel(project);

        // Schedule a full build of the project if the compiler level changed
        // because we want classes in the project to be recompiled.

        if (newCompilerLevel != null && !newCompilerLevel.equals(oldCompilerLevel)) {
            JavaFacetUtil.scheduleFullBuild(project);
        }
    }
}

From source file:org.eclipse.jst.common.project.facet.core.internal.JavaFacetUtil.java

License:Open Source License

private static void removeJreContainer(final IProject proj)

        throws CoreException

{
    final IJavaProject jproj = JavaCore.create(proj);
    final IClasspathEntry[] cp = jproj.getRawClasspath();

    int pos = -1;

    for (int i = 0; i < cp.length; i++) {
        final IClasspathEntry cpe = cp[i];

        if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                && cpe.getPath().segment(0).equals(JavaRuntime.JRE_CONTAINER)) {
            pos = i;/*from w ww  .  j av  a  2s. c  o m*/
            break;
        }
    }

    if (pos == -1) {
        return;
    }

    final IClasspathEntry[] newcp = new IClasspathEntry[cp.length - 1];

    System.arraycopy(cp, 0, newcp, 0, pos);
    System.arraycopy(cp, pos + 1, newcp, pos, newcp.length - pos);

    jproj.setRawClasspath(newcp, null);
}

From source file:org.eclipse.jst.common.project.facet.core.libprov.osgi.OsgiBundlesContainer.java

License:Open Source License

public static void addToClasspath(final IJavaProject project, final IProjectFacet facet,
        final IClasspathAttribute[] attributes)

        throws CoreException

{
    if (!isOnClasspath(project, facet)) {
        final IClasspathEntry[] oldcp = project.getRawClasspath();
        final IClasspathEntry[] newcp = new IClasspathEntry[oldcp.length + 1];
        System.arraycopy(oldcp, 0, newcp, 0, oldcp.length);
        final IPath path = CONTAINER_PATH.append(facet.getId());
        final IClasspathAttribute[] attrs = (attributes != null ? attributes : new IClasspathAttribute[0]);
        newcp[newcp.length - 1] = JavaCore.newContainerEntry(path, null, attrs, false);

        project.setRawClasspath(newcp, null);
    }/*from  w ww .  j a va  2s .c  o m*/
}

From source file:org.eclipse.jst.common.project.facet.core.libprov.osgi.OsgiBundlesContainer.java

License:Open Source License

public static void removeFromClasspath(final IJavaProject project, final IProjectFacet facet)

        throws CoreException

{
    final IClasspathEntry[] oldcp = project.getRawClasspath();

    for (int i = 0; i < oldcp.length; i++) {
        final IClasspathEntry cpe = oldcp[i];

        if (isOsgiBundlesContainer(cpe)) {
            final String fid = cpe.getPath().segment(1);

            if (facet.getId().equals(fid)) {
                final IClasspathEntry[] newcp = new IClasspathEntry[oldcp.length - 1];
                System.arraycopy(oldcp, 0, newcp, 0, i);
                System.arraycopy(oldcp, i + 1, newcp, i, oldcp.length - i - 1);

                project.setRawClasspath(newcp, null);

                return;
            }//from w w w  . ja  v a 2 s .  co  m
        }
    }
}

From source file:org.eclipse.jst.j2ee.application.internal.operations.BinaryProjectHelper.java

License:Open Source License

public void importArchiveAsBinary(Archive archive, IProject project, IProgressMonitor monitor) {
    try {//from   ww  w.ja v a 2  s.  co m

        IJavaProject javaProject = JavaCore.create(project);

        IFile savedModuleFile = saveFile(archive, project);
        saveEnclosedFile(archive, project, ProjectUtilities.DOT_CLASSPATH);
        saveEnclosedFile(archive, project, ProjectUtilities.DOT_PROJECT);
        removeImportedClassesFromClasspathIfNecessary(project);

        ensureBinary(javaProject, monitor);
        IPath path = savedModuleFile.getFullPath();

        IClasspathEntry newEntry = JavaCore.newLibraryEntry(path, path, null, true);

        IClasspathEntry[] entries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 1, entries.length);
        newEntries[0] = newEntry;
        javaProject.setRawClasspath(newEntries, monitor);

    } catch (FileNotFoundException e) {
        J2EEPlugin.logError(e);
    } catch (IOException e) {
        J2EEPlugin.logError(e);
    } catch (JavaModelException e) {
        J2EEPlugin.logError(e);
    }
}

From source file:org.eclipse.jst.j2ee.application.internal.operations.BinaryProjectHelper.java

License:Open Source License

protected void ensureBinary(IJavaProject javaProject, IProgressMonitor monitor) {

    if (javaProject == null)
        return;/*  ww  w  .j  a v  a2 s  . c  o  m*/
    List newCp = new ArrayList();
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            if (entries[i].getEntryKind() != IClasspathEntry.CPE_SOURCE)
                newCp.add(entries[i]);
        }
        entries = (IClasspathEntry[]) newCp.toArray(new IClasspathEntry[newCp.size()]);
        javaProject.setRawClasspath(entries, monitor);
    } catch (JavaModelException ex) {
        J2EEPlugin.logError(ex);
    }
}

From source file:org.eclipse.jst.j2ee.application.internal.operations.J2EEUtilityJarImportAssistantOperation.java

License:Open Source License

protected final void addLibraryToClasspath(IProject associatedEARProject, IFile copiedJarFile,
        IProgressMonitor monitor) throws CoreException, JavaModelException {
    if (associatedEARProject.hasNature(JavaCore.NATURE_ID)) {

        IClasspathEntry entry = JavaCore.newLibraryEntry(copiedJarFile.getFullPath().makeAbsolute(), null, // source
                // attachment
                null, // source attachment root
                new IAccessRule[0], // accessRules
                new IClasspathAttribute[0], // extraAttributes
                false); // isExported

        IJavaProject earJavaProject = JavaCore.create(associatedEARProject);
        IClasspathEntry[] rawClasspath = earJavaProject.getRawClasspath();
        IClasspathEntry[] newClasspath = new IClasspathEntry[rawClasspath.length + 1];
        System.arraycopy(rawClasspath, 0, newClasspath, 0, rawClasspath.length);
        newClasspath[rawClasspath.length] = entry;
        earJavaProject.setRawClasspath(newClasspath, monitor);

    }// ww  w .java  2s .  c om
}

From source file:org.eclipse.jst.j2ee.internal.archive.J2EEComponentArchiveSaveAdapter.java

License:Open Source License

protected void linkImportedClassesFolderIfNecessary() {
    if (importedClassesFolder != null) {
        try {/* ww w  .  j  a va  2s  . c o  m*/
            vComponent.getRootFolder().getFolder(getImportedClassesRuntimePath())
                    .createLink(importedClassesFolder.getProjectRelativePath(), 0, null);
            if (shouldAddImportedClassesToClasspath()) {
                if (JemProjectUtilities.getJavaProject(vComponent.getProject()) != null) {
                    IJavaProject javaProject = JavaCore.create(vComponent.getProject());
                    IClasspathEntry[] javaClasspath = javaProject.getRawClasspath();
                    IClasspathEntry[] newJavaClasspath = new IClasspathEntry[javaClasspath.length + 1];
                    System.arraycopy(javaClasspath, 0, newJavaClasspath, 0, javaClasspath.length);
                    newJavaClasspath[newJavaClasspath.length - 1] = JavaCore
                            .newLibraryEntry(importedClassesFolder.getFullPath(), null, null, true);
                    javaProject.setRawClasspath(newJavaClasspath, new NullProgressMonitor());
                }
            }
        } catch (CoreException e) {
            J2EEPlugin.logError(e);
        }
    }
}

From source file:org.eclipse.jst.j2ee.internal.classpathdep.UpdateClasspathAttributesOperation.java

License:Open Source License

/**
 * Updates the specified Java project so that only the specified classpath entries have
 * the WTP component dependency attribute.
 * @param javaProject Target Java project.
 * @param entries Classpath entries that should have the component dependency attribute. Map from IClasspathEntry
 * to the IClasspathAttribute for the WTP classpath component dependency.
 * @param modifyComponentDep True if modifying the dependency attribute, false if modifying the non-dependency attribute.
 * @throws CoreException Thrown if an error is encountered.
 *//* w  w w .  jav  a  2s .  c  o m*/
private void updateDependencyAttributes(final IJavaProject javaProject, final Map entriesWithAttrib,
        final boolean modifyComponentDep, final boolean isLegacyJ2EE) throws CoreException {
    if (javaProject == null || !javaProject.getProject().isAccessible()) {
        return;
    }

    final List updatedClasspath = new ArrayList();
    final IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    boolean needToUpdateClasspath = false;
    IClasspathAttribute attrib = UpdateClasspathAttributeUtil.createDependencyAttribute();
    if (!modifyComponentDep) {
        attrib = UpdateClasspathAttributeUtil.createNonDependencyAttribute();
    }
    for (int i = 0; i < rawClasspath.length; i++) {
        IClasspathEntry entry = rawClasspath[i];
        final int kind = entry.getEntryKind();
        boolean hasAttribute = ClasspathDependencyUtil
                .checkForComponentDependencyAttribute(entry,
                        modifyComponentDep ? DependencyAttributeType.CLASSPATH_COMPONENT_DEPENDENCY
                                : DependencyAttributeType.CLASSPATH_COMPONENT_NONDEPENDENCY,
                        isLegacyJ2EE) != null;
        boolean shouldHaveAttribute = entriesWithAttrib.containsKey(entry);
        boolean updateAttributes = false;
        IClasspathAttribute[] updatedAttributes = null;
        if (shouldHaveAttribute) {
            if (!hasAttribute) {
                // should have the attribute and currently missing it
                attrib = (IClasspathAttribute) entriesWithAttrib.get(entry);
                updatedAttributes = updateAttributes(entry.getExtraAttributes(), attrib, true);
                needToUpdateClasspath = true;
                updateAttributes = true;
            }
        } else if (hasAttribute) {
            // should not have the attribute and currently has it
            updatedAttributes = updateAttributes(entry.getExtraAttributes(), attrib, false);
            needToUpdateClasspath = true;
            updateAttributes = true;
        }

        if (updateAttributes) {
            switch (kind) {
            case IClasspathEntry.CPE_CONTAINER:
                entry = JavaCore.newContainerEntry(entry.getPath(), entry.getAccessRules(), updatedAttributes,
                        entry.isExported());
                break;
            case IClasspathEntry.CPE_LIBRARY:
                entry = JavaCore.newLibraryEntry(entry.getPath(), entry.getSourceAttachmentPath(),
                        entry.getSourceAttachmentRootPath(), entry.getAccessRules(), updatedAttributes,
                        entry.isExported());
                break;
            case IClasspathEntry.CPE_VARIABLE:
                entry = JavaCore.newVariableEntry(entry.getPath(), entry.getSourceAttachmentPath(),
                        entry.getSourceAttachmentRootPath(), entry.getAccessRules(), updatedAttributes,
                        entry.isExported());
                break;
            case IClasspathEntry.CPE_PROJECT: // although project entries are not yet supported, allow the attribute here and let the validator flag as an error
                entry = JavaCore.newProjectEntry(entry.getPath(), entry.getAccessRules(),
                        entry.combineAccessRules(), updatedAttributes, entry.isExported());
                break;
            case IClasspathEntry.CPE_SOURCE: // although source entries are not supported, allow the attribute here and let the validator flag as an error
                entry = JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(),
                        entry.getExclusionPatterns(), entry.getOutputLocation(), updatedAttributes);
                break;
            }
        }

        updatedClasspath.add(entry);
    }
    if (needToUpdateClasspath) {
        final IClasspathEntry[] updatedCPArray = (IClasspathEntry[]) updatedClasspath
                .toArray(new IClasspathEntry[updatedClasspath.size()]);
        javaProject.setRawClasspath(updatedCPArray, null);
    }
}