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.eatop.eaadapter.ea2ecore.postprocessings.CreateReleaseZipFile.java

License:Open Source License

private void removeFromJavaClasspath(IProject project, IPath path) throws CoreException, JavaModelException {

    IJavaProject jPrj = JavaCore.create(project);

    IClasspathEntry[] clsPathEntries = jPrj.getRawClasspath();
    List<IClasspathEntry> newClsPathEntries = new ArrayList<IClasspathEntry>();

    for (IClasspathEntry clsPathEntry : clsPathEntries) {
        if (!clsPathEntry.getPath().equals(path)) {
            newClsPathEntries.add(clsPathEntry);
        }/*from  w  w  w .  j  a v  a2 s . com*/
    }

    jPrj.setRawClasspath(newClsPathEntries.toArray(new IClasspathEntry[newClsPathEntries.size()]), monitor);
}

From source file:org.eclipse.edt.ide.core.utils.EclipseUtilities.java

License:Open Source License

/**
 * Adds the outputFolder as a Java source folder if the project is a Java project.
 * /*from  w  w w.  ja  va  2  s  . c o  m*/
 * @param project       The project containing the folder (used when outputFolder is a relative path)
 * @param outputFolder  The path of the folder. It may be project-relative, or workspace-relative. If workspace-relative
 *                      it should start with 'F/' for a folder or 'P/' for a project.
 * @param forceClasspathRefresh A classpath needs to be refreshed if an entry already exists for the output folder, but the folder has yet to be
 *                         created.  This can occur when a project is exported without a generation directory.
 * @throws CoreException
 */
public static void addToJavaBuildPathIfNecessary(IProject project, String outputFolder,
        boolean forceClasspathRefresh) throws CoreException {
    if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject.exists()) {
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            IPath outputFolderPath = new Path(convertFromInternalPath(outputFolder));
            boolean needToAdd = true;

            IPath fullPath = outputFolderPath.isAbsolute() ? outputFolderPath
                    : outputFolderPath.segmentCount() == 0 ? project.getFullPath()
                            : project.getFolder(outputFolderPath).getFullPath();

            for (int i = 0; i < entries.length; i++) {
                if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    IPath nextPath = entries[i].getPath();
                    // JDT throws an error if you have a source folder within a source folder. We could add exclusions to support this, but
                    // for now we just won't add the folder.
                    if (nextPath.isPrefixOf(fullPath) || fullPath.isPrefixOf(nextPath)) {
                        needToAdd = false;
                        break;
                    }
                }
            }

            if (needToAdd) {
                IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
                System.arraycopy(entries, 0, newEntries, 0, entries.length);
                newEntries[newEntries.length - 1] = JavaCore.newSourceEntry(fullPath);
                javaProject.setRawClasspath(newEntries, null);
            }

            if (!needToAdd && forceClasspathRefresh) {
                javaProject.setRawClasspath(javaProject.readRawClasspath(), javaProject.readOutputLocation(),
                        null);
            }
        }
    }
}

From source file:org.eclipse.edt.ide.core.utils.EclipseUtilities.java

License:Open Source License

/**
 * Adds the runtime containers to the project if necessary. This does nothing if the project is
 * not a Java project.//from  ww  w  . j  a v a  2 s .co m
 * 
 * @param project   The Java project.
 * @param generator  The generator provider.
 * @param ctx  The generation context.
 */
public static void addRuntimesToProject(IProject project, IGenerator generator, EglContext ctx) {
    EDTRuntimeContainer[] baseRuntimes = generator instanceof AbstractGenerator
            ? ((AbstractGenerator) generator).resolveBaseRuntimeContainers()
            : null;

    EDTRuntimeContainer[] containersToAdd;
    Set<String> requiredContainers = ctx.getRequiredRuntimeContainers();
    if (requiredContainers.size() == 0) {
        if (baseRuntimes == null || baseRuntimes.length == 0) {
            return;
        }
        containersToAdd = baseRuntimes;
    } else {
        Set<EDTRuntimeContainer> containers = new HashSet<EDTRuntimeContainer>(10);
        if (baseRuntimes != null && baseRuntimes.length > 0) {
            containers.addAll(Arrays.asList(baseRuntimes));
        }
        for (EDTRuntimeContainer container : generator.getRuntimeContainers()) {
            if (requiredContainers.contains(container.getId())) {
                containers.add(container);
            }
        }
        containersToAdd = containers.toArray(new EDTRuntimeContainer[containers.size()]);
    }

    if (containersToAdd == null || containersToAdd.length == 0) {
        return;
    }

    try {
        if (project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(project);
            IClasspathEntry[] classpath = javaProject.getRawClasspath();

            List<IClasspathEntry> additions = new ArrayList<IClasspathEntry>();

            for (int i = 0; i < containersToAdd.length; i++) {
                IPath path = containersToAdd[i].getPath();
                boolean found = false;
                for (int j = 0; j < classpath.length; j++) {
                    if (classpath[j].getEntryKind() == IClasspathEntry.CPE_CONTAINER
                            && classpath[j].getPath().equals(path)) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    additions.add(JavaCore.newContainerEntry(path));
                }
            }

            if (additions.size() > 0) {
                IClasspathEntry[] newEntries = new IClasspathEntry[classpath.length + additions.size()];
                System.arraycopy(classpath, 0, newEntries, 0, classpath.length);
                for (int i = 0; i < additions.size(); i++) {
                    newEntries[classpath.length + i] = additions.get(i);
                }
                javaProject.setRawClasspath(newEntries, null);
            }
        }
    } catch (CoreException e) {
        EDTCoreIDEPlugin.log(e);
    }
}

From source file:org.eclipse.edt.ide.eunit.internal.actions.GenTestDriverAction.java

License:Open Source License

protected WorkspaceModifyOperation getSetJavaBuildPathOperation(final IProject javaDriverProject,
        final IProject dependentProj) {
    WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
        @Override//from w  w  w. j  a  va 2  s.c  o m
        protected void execute(IProgressMonitor monitor)
                throws CoreException, InvocationTargetException, InterruptedException {
            if (javaDriverProject.hasNature(JavaCore.NATURE_ID)) {
                monitor.subTask("Set java build path depends on project " + dependentProj.getName());
                IJavaProject javaProject = JavaCore.create(javaDriverProject);

                IClasspathEntry[] classpath = javaProject.getRawClasspath();

                boolean javaProjBuildPathAlreadySet = false;
                //check to see if the java build path already set for the same dependent project
                for (int p = 0; p < classpath.length && !javaProjBuildPathAlreadySet; p++) {
                    if (classpath[p].getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                        IPath dependentProjPath = classpath[p].getPath();
                        if (dependentProj.getFullPath().equals(dependentProjPath))
                            javaProjBuildPathAlreadySet = true;
                    }
                }

                //if not set, set it
                if (!javaProjBuildPathAlreadySet) {
                    List<IClasspathEntry> additions = new ArrayList<IClasspathEntry>();

                    IClasspathEntry newClsPathEntry = JavaCore.newProjectEntry(dependentProj.getFullPath());
                    additions.add(newClsPathEntry);

                    if (additions.size() > 0) {
                        IClasspathEntry[] newEntries = new IClasspathEntry[classpath.length + additions.size()];
                        System.arraycopy(classpath, 0, newEntries, 0, classpath.length);
                        for (int i = 0; i < additions.size(); i++) {
                            newEntries[classpath.length + i] = additions.get(i);
                        }
                        javaProject.setRawClasspath(newEntries, null);
                    }
                }
                monitor.worked(1);
            }
        }
    };

    return op;
}

From source file:org.eclipse.edt.ide.ui.wizards.EGLProjectUtility.java

License:Open Source License

public static void addClasspathLibraryEntries(IJavaProject javaProject, IClasspathEntry[] classpathEntries)
        throws CoreException {
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + classpathEntries.length];

    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    for (int i = 0; i < classpathEntries.length; i++) {
        newEntries[oldEntries.length + i] = classpathEntries[i];
    }/*from   w w  w .j ava 2s.  c om*/
    javaProject.setRawClasspath(newEntries, null);
}

From source file:org.eclipse.edt.ide.ui.wizards.EGLProjectUtility.java

License:Open Source License

public static void removeClasspathLibraryEntries(IJavaProject javaProject, IPath[] jarPaths)
        throws CoreException {
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    if (oldEntries.length < jarPaths.length)
        return; //there are more paths to be deleted than the existing paths, should not reach here
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length - jarPaths.length];
    int k = 0;//ww w  . ja v  a 2 s  .  c o m

    for (int i = 0; i < oldEntries.length; i++) {
        boolean isEntryKept = true;
        for (int j = 0; j < jarPaths.length; j++) {
            if (jarPaths[j].toString().equalsIgnoreCase(oldEntries[i].getPath().toString())) {
                isEntryKept = false;
                break;
            }
        }
        if (isEntryKept) {
            newEntries[k++] = oldEntries[i];
        }
    }
    javaProject.setRawClasspath(newEntries, null);
}

From source file:org.eclipse.edt.ide.ui.wizards.EGLProjectUtility.java

License:Open Source License

public static void modifyClasspathLibraryEntry(IProject project, IClasspathEntry[] modifiedEntries)
        throws CoreException {
    IJavaProject javaProject; //The Java "view" of the project.
    if (project.hasNature(JavaCore.NATURE_ID)) {
        javaProject = JavaCore.create(project);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = entries;

        for (int j = 0; j < modifiedEntries.length; j++) {
            for (int i = 0; i < entries.length; i++) {
                if (modifiedEntries[j].getPath().toOSString()
                        .equalsIgnoreCase(entries[i].getPath().toOSString())) {
                    newEntries[i] = modifiedEntries[j];
                    break;
                }/*from  w  ww.j  a v a 2 s.  c o  m*/
            }
        }
        javaProject.setRawClasspath(newEntries, null);
    }
}

From source file:org.eclipse.edt.ide.ui.wizards.ProjectConfigurationOperation.java

License:Open Source License

private void postAddingJavaBuildPathEntry(IEGLPathEntry[] selectedEntries, IProject curProject,
        IProgressMonitor monitor) {/*from  w  ww  .  jav  a 2  s.  co  m*/
    try {
        if (curProject.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject javaProject = JavaCore.create(curProject);
            IClasspathEntry[] javaClassPathEntries = javaProject.getRawClasspath();
            List<IClasspathEntry> afterChangeEntries = new ArrayList<IClasspathEntry>();

            Set ipathSet = new HashSet<IPath>();
            for (IClasspathEntry icpEntry : javaClassPathEntries) {
                ipathSet.add(icpEntry.getPath());
                afterChangeEntries.add(icpEntry);
            }

            for (IEGLPathEntry iEGLpathEntry : selectedEntries) {
                if (iEGLpathEntry.getEntryKind() == IEGLPathEntry.CPE_PROJECT) {
                    IPath eglProjectPath = iEGLpathEntry.getPath();
                    IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(eglProjectPath);
                    if (resource != null && resource.exists()) {
                        if (resource.getProject().hasNature(JavaCore.NATURE_ID)
                                && !ipathSet.contains(eglProjectPath)) {
                            ipathSet.add(eglProjectPath);
                            afterChangeEntries.add(JavaCore.newProjectEntry(eglProjectPath));
                        }
                    }
                }
            }

            javaProject.setRawClasspath(
                    afterChangeEntries.toArray(new IClasspathEntry[afterChangeEntries.size()]), monitor);
        }
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

From source file:org.eclipse.emf.cdo.dawn.codegen.util.ProjectCreationHelper.java

License:Open Source License

/**
 * @param javaProject/*  w ww.  j  a  va2  s .  c o  m*/
 * @param newEntry
 * @throws JavaModelException
 */
public final static void addToClasspath(IJavaProject javaProject, IClasspathEntry newEntry)
        throws JavaModelException {
    if (newEntry == null) {
        return;
    }
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);

    newEntries[oldEntries.length] = newEntry;
    javaProject.setRawClasspath(newEntries, null);
}

From source file:org.eclipse.emf.cdo.dawn.codegen.util.ProjectCreationHelper.java

License:Open Source License

/**
 * @param javaProject//  ww w.  j a  va2  s.  c o  m
 * @param toBeRemoved
 * @throws JavaModelException
 */
public final static void removeFromClasspath(IJavaProject javaProject, IPath toBeRemoved)
        throws JavaModelException {
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();

    for (IClasspathEntry classpathEntry : oldEntries) {
        if (!classpathEntry.getPath().equals(toBeRemoved)) {
            newEntries.add(classpathEntry);
        }
    }

    IClasspathEntry[] newEntriesArray = new IClasspathEntry[newEntries.size()];
    javaProject.setRawClasspath(newEntries.toArray(newEntriesArray), null);
}