Example usage for org.eclipse.jdt.core IJavaProject getRawClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject getRawClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getRawClasspath.

Prototype

IClasspathEntry[] getRawClasspath() throws JavaModelException;

Source Link

Document

Returns the raw classpath for the project, as a list of classpath entries.

Usage

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  av a 2 s  .  c o  m
 * 
 * @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   w w  w . j  a v  a 2 s.  c  o 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/*w  w w. j  a  va2s . 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  www . j  a  v  a 2s .co m*/

    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

/**
 * Appends target project build path with source project build path.
 * //from   w w  w .  j a  va 2 s .  co m
 * @param targetProject the target project
 * @param sourceProject the source project
 * @throws CoreException
 */
public static void appendProjectBuildPath(IJavaProject targetProject, IJavaProject sourceProject)
        throws CoreException {
    try {
        // copy required entries to target
        IClasspathEntry[] srcEntries = sourceProject.getRawClasspath();
        for (int i = 0; i < srcEntries.length; i++) {
            IClasspathEntry entry = srcEntries[i];
            if (entry.isExported() || entry.getEntryKind() == IClasspathEntry.CPE_SOURCE)
                continue;
            addToClasspath(targetProject, entry);
        }
        // add the source project as a project entry
        IClasspathEntry srcPrjEntry = JavaCore.newProjectEntry(sourceProject.getPath());
        addToClasspath(targetProject, srcPrjEntry);
    } catch (JavaModelException e) {
        // we interested only in core exceptions
        if (e.getCause() instanceof CoreException) {
            throw (CoreException) e.getCause();
        }
    }
}

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  w w  .  j  a v a 2s. c o m*/
    }
    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.convert.WTAPIUsage.java

License:Open Source License

private Collection<String> collectReferencedPlugins(PrintWriter writer) {
    Collection<String> pluginIds = new HashSet<String>();
    try {/*  w  w w .j  av  a  2s . co m*/
        for (IJavaProject proj : projects)
            for (IClasspathEntry entry : proj.getRawClasspath())
                collectPluginsReferencedByClasspathEntry(writer, pluginIds, proj, entry);
    } catch (JavaModelException e) {
        writer.println();
        e.printStackTrace(writer);
    } catch (IOException e) {
        writer.println();
        e.printStackTrace(writer);
    }
    return pluginIds;
}

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 w  w  . ja  va 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 {//ww  w  .j a  va 2  s . 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:com.wt.studio.plugin.platform.actions.MavenClasspathUpdateJob.java

License:Open Source License

private IPath getJREContainerPath(IContainer basedir) throws CoreException {
    IProject project = basedir.getProject();
    if (project != null && project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (JavaRuntime.JRE_CONTAINER.equals(entry.getPath().segment(0))) {
                return entry.getPath();
            }//  w ww. j ava  2s. c  o m
        }
    }
    return null;
}