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:com.technophobia.substeps.junit.launcher.SubstepsLaunchConfigurationDelegate.java

License:Open Source License

private Callback1<IJavaProject> removeSubstepsFromClasspath() {
    return new Callback1<IJavaProject>() {

        @Override//from w  ww  .ja  va2 s  . c  o m
        public void doCallback(final IJavaProject project) {
            try {
                final List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>(
                        Arrays.asList(project.getRawClasspath()));

                final List<String> jarFiles = new SubstepJarProvider().junitRunnerJars();
                for (final String jarFile : jarFiles) {
                    newClasspath.remove(JavaCore.newLibraryEntry(new Path(jarFile), null, null));
                }
                project.setRawClasspath(newClasspath.toArray(new IClasspathEntry[newClasspath.size()]), null);
            } catch (final JavaModelException ex) {
                FeatureRunnerPlugin.error("Could not remove substeps jars from classpath", ex);
            }
        }
    };
}

From source file:com.testify.ecfeed.ui.common.EclipseModelImplementer.java

License:Open Source License

private IPackageFragmentRoot createNewSourceFolder(String name) throws CoreException {
    IProject project = fFileInfoProvider.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    IFolder srcFolder = project.getFolder(name);
    int i = 0;/*from   www . java2  s.c o m*/
    while (srcFolder.exists()) {
        String newName = name + i++;
        srcFolder = project.getFolder(newName);
    }
    srcFolder.create(false, true, null);
    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(srcFolder);

    IClasspathEntry[] entries = javaProject.getRawClasspath();
    IClasspathEntry[] updated = new IClasspathEntry[entries.length + 1];
    System.arraycopy(entries, 0, updated, 0, entries.length);
    updated[entries.length] = JavaCore.newSourceEntry(root.getPath());
    javaProject.setRawClasspath(updated, null);
    return root;
}

From source file:com.threecrickets.creel.eclipse.internal.EclipseUtil.java

License:LGPL

/**
 * Sets a classpath container in a project. If the classpath container is
 * already there, will recreate it. (Assumes that the path is only used
 * once.)//from ww  w.  j a  v a2  s  .c  om
 * 
 * @param project
 *        The project
 * @param container
 *        The classpath container
 * @throws JavaModelException
 *         In case of an Eclipse JDT error
 */
public static void setClasspathContainer(IJavaProject project, IClasspathContainer container)
        throws JavaModelException {
    IPath path = container.getPath();
    IClasspathEntry[] entries = project.getRawClasspath();

    int found = -1;
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        if ((entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) && (entry.getPath().equals(path))) {
            found = i;
            break;
        }
    }

    if (found == -1) {
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);
        newEntries[entries.length] = JavaCore.newContainerEntry(container.getPath());
        project.setRawClasspath(newEntries, null);
    } else {
        entries[found] = JavaCore.newContainerEntry(path);
        project.setRawClasspath(entries, null);
    }

    JavaCore.setClasspathContainer(Classpath.PATH, new IJavaProject[] { project },
            new IClasspathContainer[] { container }, null);
}

From source file:com.threecrickets.creel.eclipse.internal.EclipseUtil.java

License:LGPL

/**
 * Removes a classpath container from project if it is there. (Assumes that
 * the path is only used once.)//from ww w. ja v a  2 s  .co  m
 * 
 * @param project
 *        The project
 * @param path
 *        The path
 * @throws JavaModelException
 *         In case of an Eclipse JDT error
 */
public static void removeClasspathContainer(IJavaProject project, IPath path) throws JavaModelException {
    IClasspathEntry[] entries = project.getRawClasspath();

    int found = -1;
    for (int i = 0, length = entries.length; i < length; i++) {
        IClasspathEntry entry = entries[i];
        if ((entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) && (entry.getPath().equals(path))) {
            found = i;
            break;
        }
    }

    if (found != -1) {
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length - 1];
        System.arraycopy(entries, 0, newEntries, 0, found);
        if (found != entries.length)
            System.arraycopy(entries, found + 1, newEntries, found, entries.length - found - 1);
        project.setRawClasspath(newEntries, null);
    }
}

From source file:com.versant.core.jdo.tools.eclipse.Utils.java

License:Open Source License

/**
 * Add the 'JDO_CONTAINER' container variable to the project's build path.
 * This variable is resolved from {@link JDOClasspathContainerInitializer}.
 *
 * @see #addJDOGenieCPContainer(org.eclipse.jdt.core.IJavaProject)
 * @see JDOClasspathContainerInitializer
 * @see JDOClasspathContainer/*from   ww w. ja v  a  2  s  .c o  m*/
 *
 * @param jProject
 * @throws JavaModelException
 */
public static void addCPVarTo(IJavaProject jProject) throws JavaModelException {
    //create the container variable
    IClasspathEntry varEntry = JavaCore.newContainerEntry(new Path("JDO_CONTAINER"));

    IClasspathEntry[] oldclasspath = jProject.getRawClasspath();
    IClasspathEntry[] newclasspath = new IClasspathEntry[oldclasspath.length + 1];

    for (int i = 0; i < oldclasspath.length; i++) {
        IClasspathEntry iClasspathEntry = oldclasspath[i];
        if (iClasspathEntry.equals(varEntry)) {
            if (ECLIPSE_DEBUG)
                System.out.println("-- JDO_CONTAINER already added to cp");
            return;
        }
    }

    for (int i = 0; i < oldclasspath.length; i++) {
        newclasspath[i] = oldclasspath[i];
    }

    newclasspath[newclasspath.length - 1] = varEntry;
    jProject.setRawClasspath(newclasspath, null);
}

From source file:com.versant.core.jdo.tools.eclipse.Utils.java

License:Open Source License

public static void addJDOGenieJars(IProject project) {
    String path = getJDOGenieHomePath();
    if (path == null)
        return;/*from w w w .  jav a 2s  .  com*/

    try {
        IClasspathEntry libEntryGenie = JavaCore.newLibraryEntry(new Path(path + "lib/openaccess.jar"), null,
                null, false);
        IClasspathEntry libEntryJTA = JavaCore.newLibraryEntry(new Path(path + "lib/jta.jar"), null, null,
                false);
        IJavaProject jProject = JavaCore.create(project);

        IClasspathEntry[] currentCPs = jProject.getRawClasspath();
        java.util.List l = new ArrayList();
        for (int i = 0; i < currentCPs.length; i++) {
            IClasspathEntry currentCP = currentCPs[i];
            l.add(currentCP);
        }

        addIfNotContains(l, libEntryGenie);
        addIfNotContains(l, libEntryJTA);

        IClasspathEntry[] newCPs = new IClasspathEntry[l.size()];
        l.toArray(newCPs);

        jProject.setRawClasspath(newCPs, null);
    } catch (JavaModelException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.windowtester.codegen.util.BuildPathUtil.java

License:Open Source License

public static void addToClasspath(final IJavaProject project, IClasspathEntry entry) throws CoreException {
    IClasspathEntry[] oldEntries = project.getRawClasspath();
    for (int i = 0; i < oldEntries.length; i++) {
        if (oldEntries[i].equals(entry)) {
            return;
        }/*from w ww  . j a v a 2  s .  c  om*/
    }
    int nEntries = oldEntries.length;
    final IClasspathEntry[] newEntries = new IClasspathEntry[nEntries + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, nEntries);
    newEntries[nEntries] = entry;
    try {
        try {
            project.setRawClasspath(newEntries, null);
        } catch (JavaModelException e) {
            throw new InvocationTargetException(e);
        }
    } catch (InvocationTargetException e) {
        Throwable t = e.getTargetException();
        if (t instanceof CoreException) {
            throw (CoreException) t;
        }
    }
}

From source file:com.windowtester.eclipse.ui.wizard.NewExampleProjectWizard.java

License:Open Source License

private void importProject(String projectName, IProgressMonitor monitor) throws CoreException {
    final IWorkspace workspace = ResourcesPlugin.getWorkspace();
    final IProject project = workspace.getRoot().getProject(projectName);
    IProjectDescription description = workspace.newProjectDescription(projectName);
    description.setLocation(null);//from  w ww.j a  v a 2  s .  c o  m
    project.create(description, new SubProgressMonitor(monitor, 1));
    project.open(new SubProgressMonitor(monitor, 1));

    // Direct ECLIPSE_HOME references are different each Eclipse installation
    // so adjust the classpath accordingly

    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    boolean modified = false;
    for (int i = 0; i < classpath.length; i++) {
        IClasspathEntry entry = classpath[i];
        if (entry.getEntryKind() != IClasspathEntry.CPE_VARIABLE)
            continue;
        IPath path = entry.getPath();
        if (path.segmentCount() != 3)
            continue;
        if (!path.segment(0).equals("ECLIPSE_HOME"))
            continue;
        if (!path.segment(1).equals("plugins"))
            continue;
        String jarName = path.segment(2);
        path = path.removeLastSegments(1);
        IPath pluginsPath = JavaCore.getResolvedVariablePath(path);
        if (pluginsPath == null) {
            Logger.log("Failed to resolve " + path);
            continue;
        }
        File pluginsDir = pluginsPath.toFile();
        String jarPrefix = jarName.substring(0, jarName.indexOf('_') + 1);
        String[] childNames = pluginsDir.list();
        if (childNames == null) {
            Logger.log("Failed to obtain children for " + pluginsDir.getPath());
            continue;
        }
        for (int j = 0; j < childNames.length; j++) {
            String name = childNames[j];
            if (name.startsWith(jarPrefix)) {
                modified = true;
                classpath[i] = JavaCore.newVariableEntry(path.append(name), null, null);
                break;
            }
        }
    }
    if (modified)
        javaProject.setRawClasspath(classpath, new NullProgressMonitor());
}

From source file:com.windowtester.swt.codegen.wizards.NewTestTypeWizard.java

License:Open Source License

private void updateClasspath(IResource resource) {
    try {//from w  ww  .ja va2s .c o  m
        if (_wizardPage.isRcpApplication()) {
            IPath requiredPluginsPath = new Path("org.eclipse.pde.core.requiredPlugins");
            IJavaProject javaProject = JavaCore.create(resource.getProject());
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                    if (entry.getPath().equals(requiredPluginsPath)) {
                        return; // no need to process
                    }
                }
            }
            IClasspathEntry requiredPlugins = JavaCore.newContainerEntry(requiredPluginsPath);
            IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
            System.arraycopy(entries, 0, newEntries, 0, entries.length);
            newEntries[entries.length] = requiredPlugins;
            javaProject.setRawClasspath(newEntries, null);
        }
    } catch (CoreException e) {
        Logger.log(e);
    }
}

From source file:de.akra.idocit.java.utils.JavaTestUtils.java

License:Apache License

/**
 * Creates and initializes a IJavaProject within the currently running test workspace.
 * /*from w  w  w.  j  av a  2 s . com*/
 * @destination The current test workspace.
 * @param projectName
 *            [PRIMARY_KEY]
 * @param filesToAdd
 *            [ATTRIBUTE] files to add to the test workspace
 * @return [OBJECT] the created project.
 * @throws CoreException
 * @throws FileNotFoundException
 * @thematicgrid Putting Operations
 */
public static IProject initProjectInWorkspace(final String projectName, final Collection<File> filesToAdd)
        throws CoreException, FileNotFoundException {
    /*
     * The implementation of the initialization of the Test-Java Project has been
     * guided by http://sdqweb.ipd.kit.edu/wiki/JDT_Tutorial:
     * _Creating_Eclipse_Java_Projects_Programmatically.
     * 
     * Thanks to the authors.
     */

    // Create Java Project
    final IProgressMonitor progressMonitor = new NullProgressMonitor();
    final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    final IProject project = root.getProject(projectName);
    project.create(progressMonitor);
    project.open(progressMonitor);

    final IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);
    final IJavaProject javaProject = JavaCore.create(project);
    javaProject.open(progressMonitor);

    // Add Java Runtime to the project
    final List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    final IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    final LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

    // Add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // Create source folder
    final IFolder srcFolder = project.getFolder("src");
    srcFolder.create(true, true, progressMonitor);

    final IPackageFragmentRoot fragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    final IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    final IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(fragmentRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    // Create the package
    final IFolder packageFolder = srcFolder.getFolder("source");
    packageFolder.create(true, true, progressMonitor);

    // Create Java files
    for (final File file : filesToAdd) {
        final IFile customerWorkspaceFile = packageFolder.getFile(file.getName());
        customerWorkspaceFile.create(new FileInputStream(file), true, progressMonitor);
    }

    project.refreshLocal(IProject.DEPTH_INFINITE, progressMonitor);
    return project;
}