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.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Updates the class path of a Java project with libraries embedded by the studio.
 * @param jp the Java project/*from  w  ww  . ja va 2 s.com*/
 * @param folders the folder names
 * @throws IOException
 * @throws JavaModelException
 */
public static void updateClasspathWithProjectLibraries(IJavaProject jp, IProgressMonitor monitor,
        String... folders) throws IOException, JavaModelException {

    if (folders == null)
        return;

    // Keep the current entries
    ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    entries.addAll(Arrays.asList(jp.getRawClasspath()));
    FilenameFilter filter = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".jar") || name.endsWith(".zip");
        }
    };

    for (String folder : folders) {
        File pojoLibPath = ResourceUtils.getPluginBinaryPath("com.ebmwebsourcing.petals.libs.esb", folder); //$NON-NLS-1$
        if (pojoLibPath == null) {
            PetalsCommonPlugin.log("Could not find the Petals libraries in the distribution.", IStatus.ERROR);
            throw new IOException("Petals libraries could not be located.");
        }

        // Add the libraries in the project class path
        File[] jarFiles = pojoLibPath.listFiles(filter);
        if (jarFiles != null) {
            for (File jarFile : jarFiles) {
                IPath path = new Path(jarFile.getAbsolutePath());
                IClasspathEntry entry = JavaCore.newLibraryEntry(path, null, null);
                entries.add(entry);
            }
        }
    }

    IClasspathEntry[] newEntries = CollectionUtils.convertToArray(entries, IClasspathEntry.class);
    if (!jp.hasClasspathCycle(newEntries))
        jp.setRawClasspath(newEntries, monitor);
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Makes an Eclipse project a Java project.
 * <p>//from  w w  w  .  j  av  a  2 s  . co  m
 * If the project does not exist, or is not accessible, then nothing is done.<br />
 * If the project does not have the Java nature, this nature is added.
 * </p>
 * <p>
 * The default output directory is "bin". If this directory does not exist, it is created.
 * If the creation fails, then no output directory is set.
 * </p>
 * <p>
 * The default source directory is "src/main/java". If this directory does not exist, it is created.
 * If the creation fails, then no source directory is set.
 * </p>
 *
 * @param project the project to transform into a Java project
 * @return the created Java project
 * @throws CoreException if something went wrong
 */
public static IJavaProject createJavaProject(IProject project) throws CoreException {

    IJavaProject jp = null;
    if (project.isAccessible()) {

        // Add the Java nature?
        if (!project.hasNature(JavaCore.NATURE_ID)) {

            IProjectDescription description = project.getDescription();
            String[] natures = description.getNatureIds();
            String[] newNatures = new String[natures.length + 1];

            System.arraycopy(natures, 0, newNatures, 0, natures.length);
            newNatures[natures.length] = JavaCore.NATURE_ID;
            description.setNatureIds(newNatures);
            project.setDescription(description, null);
        }

        // Set the default class path
        jp = JavaCore.create(project);
        IProgressMonitor monitor = new NullProgressMonitor();

        //
        // Output location
        IPath ppath = project.getFullPath();
        IPath binPath = ppath.append(PetalsConstants.LOC_BIN_FOLDER);
        File binFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(binPath).toFile();
        if (binFile.exists() && binFile.isDirectory() || !binFile.exists() && binFile.mkdirs()) {
            jp.setOutputLocation(binPath, monitor);
        }

        Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();
        entries.addAll(Arrays.asList(jp.getRawClasspath()));
        entries.add(JavaRuntime.getDefaultJREContainerEntry());

        //
        // Remove the "src" entry
        IClasspathEntry srcEntry = null;
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                srcEntry = entry;
                break;
            }
        }

        //
        // Specify a new source entry
        if (srcEntry != null)
            entries.remove(srcEntry);

        String[] srcPaths = new String[] { PetalsConstants.LOC_SRC_FOLDER,
                PetalsConstants.LOC_JAVA_RES_FOLDER };
        for (String s : srcPaths) {
            IPath srcPath = ppath.append(s);
            File srcFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(srcPath).toFile();
            if (srcFile.exists() && srcFile.isDirectory() || !srcFile.exists() && srcFile.mkdirs()) {

                project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
                srcEntry = JavaCore.newSourceEntry(srcPath);
                entries.add(srcEntry);
            } else {
                PetalsCommonPlugin.log("Could not set '" + s + "' as a source folder.", IStatus.ERROR);
            }
        }

        jp.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);
    }

    return jp;
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Creates a new source folder./*from w  w  w .j ava 2 s. c om*/
 * <p>
 * If the folder does not exist, it is created.<br />
 * If the directory is already in the class path, then this method does nothing.
 * </p>
 *
 * @param jp the Java project
 * @param folderName the folder name
 * @param monitor the progress monitor
 * @return the created folder
 * @throws CoreException if something went wrong
 */
public static IFolder createSourceFolder(IJavaProject jp, String folderName, IProgressMonitor monitor)
        throws CoreException {

    IFolder newSourceFolder = jp.getProject().getFolder(folderName);
    if (!newSourceFolder.exists())
        newSourceFolder.create(true, true, monitor);

    IClasspathEntry srcEntry = JavaCore.newSourceEntry(newSourceFolder.getFullPath());
    Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();
    entries.addAll(Arrays.asList(jp.getRawClasspath()));
    entries.add(srcEntry);
    jp.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);

    return newSourceFolder;
}

From source file:com.ecfeed.ui.common.utils.EclipsePackageFragmentGetter.java

License:Open Source License

private static IPackageFragmentRoot createNewSourceFolder(String name, IFileInfoProvider fFileInfoProvider)
        throws CoreException {

    IProject project = fFileInfoProvider.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    IFolder srcFolder = project.getFolder(name);

    int i = 0;/*from w  w w.j  a  v  a  2  s. co  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.feup.contribution.druid.DruidPlugin.java

License:Open Source License

public void addDruidNature(IProject project) throws CoreException {
    if (project.hasNature(DRUID_NATURE))
        return;//from  w w  w .j  a  v a2 s  .  com

    IProjectDescription description = project.getDescription();
    String[] ids = description.getNatureIds();
    String[] newIds = new String[ids.length + 1];
    System.arraycopy(ids, 0, newIds, 0, ids.length);
    newIds[ids.length] = DRUID_NATURE;
    description.setNatureIds(newIds);
    project.setDescription(description, null);

    IJavaProject javaProject = (IJavaProject) JavaCore.create((IProject) project);

    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();

    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(rawClasspath.length + 1);
    for (IClasspathEntry e : rawClasspath) {
        newEntries.add(e);
    }
    newEntries.add(JavaCore.newContainerEntry(DruidClasspathContainer.CONTAINER_ID));

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

    javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
}

From source file:com.github.caofangkun.bazelipse.wizard.BazelProjectSupport.java

License:Open Source License

private static void createClasspath(IPath root, List<String> paths, IJavaProject javaProject)
        throws CoreException {
    String name = root.lastSegment();
    IFolder base = javaProject.getProject().getFolder(name);
    if (!base.isLinked()) {
        base.createLink(root, IResource.NONE, null);
    }/*from   ww w  . j ava 2s  .c o m*/
    List<IClasspathEntry> list = new LinkedList<>();
    for (String path : paths) {
        IPath workspacePath = base.getFullPath().append(path);
        list.add(JavaCore.newSourceEntry(workspacePath));
    }
    list.add(JavaCore.newContainerEntry(new Path(BazelClasspathContainer.CONTAINER_NAME)));
    // TODO(dmarting): we should add otherwise. Best way is to get the
    // bootclasspath from Bazel.
    list.add(JavaCore.newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER/"
            + "org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8")));
    IClasspathEntry[] newClasspath = (IClasspathEntry[]) list.toArray(new IClasspathEntry[0]);
    javaProject.setRawClasspath(newClasspath, null);
}

From source file:com.google.appengine.eclipse.core.validators.java.PluginTestUtils.java

License:Open Source License

/**
 * Creates a Java project with the given name and the specified raw classpath.
 *//*from ww w  .j  a  v  a  2 s .  co  m*/
public static IJavaProject createProject(String projectName, IClasspathEntry[] rawClasspaths)
        throws CoreException {
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = workspaceRoot.getProject(projectName);
    if (project.exists()) {
        throw new IllegalStateException("Project " + projectName + " already exists in this workspace");
    }

    NullProgressMonitor monitor = new NullProgressMonitor();

    BuildPathsBlock.createProject(project, project.getLocationURI(), monitor);
    BuildPathsBlock.addJavaNature(project, monitor);

    IJavaProject javaProject = JavaCore.create(project);
    javaProject.setRawClasspath(rawClasspaths, monitor);

    return javaProject;
}

From source file:com.google.cloud.tools.eclipse.appengine.compat.GpeMigrator.java

License:Apache License

@VisibleForTesting
static void removeGpeClasspathEntries(IProject project) {
    try {/*from  www  . ja  v  a2  s . com*/
        IJavaProject javaProject = JavaCore.create(project);
        List<IClasspathEntry> newEntries = new ArrayList<>();
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            String path = entry.getPath().toString(); // note: '/' is a path separator.
            if (!GPE_CLASSPATH_ENTRIES_PATH.contains(path)) {
                newEntries.add(entry);
            }
        }

        IClasspathEntry[] rawEntries = newEntries.toArray(new IClasspathEntry[0]);
        javaProject.setRawClasspath(rawEntries, new NullProgressMonitor());
        javaProject.save(new NullProgressMonitor(), true);

    } catch (JavaModelException ex) {
        logger.log(Level.WARNING, "Failed to remove GPE classpath entries.", ex);
    }
}

From source file:com.google.cloud.tools.eclipse.appengine.facets.FacetInstallDelegate.java

License:Open Source License

/**
 * Adds jars associated with the App Engine facet if they don't already exist in
 * <code>project</code>/*from  w w  w. ja  va 2s . co  m*/
 */
private void addAppEngineJarsToClasspath(IProject project, IProgressMonitor monitor) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    IClasspathEntry appEngineContainer = JavaCore.newContainerEntry(
            new Path(AppEngineSdkClasspathContainer.CONTAINER_ID), new IAccessRule[0],
            new IClasspathAttribute[] {
                    UpdateClasspathAttributeUtil.createDependencyAttribute(true /*isWebApp */) },
            true /* isExported */);

    // Check if App Engine container entry already exists
    for (int i = 0; i < rawClasspath.length; i++) {
        if (rawClasspath[i].equals(appEngineContainer)) {
            return;
        }
    }

    IClasspathEntry[] newClasspath = new IClasspathEntry[rawClasspath.length + 1];
    System.arraycopy(rawClasspath, 0, newClasspath, 0, rawClasspath.length);
    newClasspath[newClasspath.length - 1] = appEngineContainer;
    javaProject.setRawClasspath(newClasspath, monitor);
}

From source file:com.google.cloud.tools.eclipse.appengine.facets.FacetUninstallDelegate.java

License:Open Source License

private void removeAppEngineJarsFromClasspath(IProject project, IProgressMonitor monitor) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    IClasspathEntry[] newClasspath = new IClasspathEntry[rawClasspath.length - 1];

    int appEngineContainerIndex = 0;
    boolean isAppEngineSdkPresent = false;
    for (IClasspathEntry entry : rawClasspath) {
        if (AppEngineSdkClasspathContainer.CONTAINER_ID.equals(entry.getPath().toString())) {
            isAppEngineSdkPresent = true;
        } else {//  w  ww.  j  av  a2  s .  c o m
            newClasspath[appEngineContainerIndex++] = entry;
        }
    }

    if (isAppEngineSdkPresent) {
        javaProject.setRawClasspath(newClasspath, monitor);
    }
}