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.redhat.ceylon.eclipse.ui.test.CeylonEditorTest.java

License:Open Source License

private IProject createJavaProject(String name) throws CoreException, JavaModelException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(name);
    project.create(null);//w w w . j  av  a 2  s. c  om
    project.open(null);

    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);

    IJavaProject javaProject = JavaCore.create(project);

    IFolder binFolder = project.getFolder("bin");
    binFolder.create(false, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    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);

    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    IPackageFragmentRoot packageroot = javaProject.getPackageFragmentRoot(sourceFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(packageroot.getPath());
    javaProject.setRawClasspath(newEntries, null);
    return project;
}

From source file:com.siteview.mde.internal.core.project.ProjectModifyOperation.java

License:Open Source License

/**
 * Configures the build path and output location of the described Java project.
 * If the Java project existed before this operation, new build path entries are
 * added for the bundle class path, if required, but we don't change the exiting
 * build path.//from ww  w.j a  v  a 2  s .c om
 * 
 * @param description desired project description
 * @param before state before the operation
 * @param existed whether the Java project existed before the operation
 */
private void configureJavaProject(IBundleProjectDescription description, IBundleProjectDescription before,
        boolean existed) throws CoreException {
    IProject project = description.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    // create source folders as required
    IBundleClasspathEntry[] bces = description.getBundleClasspath();
    if (bces != null && bces.length > 0) {
        for (int i = 0; i < bces.length; i++) {
            IPath folder = bces[i].getSourcePath();
            if (folder != null) {
                CoreUtility.createFolder(project.getFolder(folder));
            }
        }
    }
    // Set default output folder
    if (description.getDefaultOutputFolder() != null) {
        IPath path = project.getFullPath().append(description.getDefaultOutputFolder());
        javaProject.setOutputLocation(path, null);
    }

    // merge the class path if the project existed before
    IBundleClasspathEntry[] prev = before.getBundleClasspath();
    if (!isEqual(bces, prev)) {
        if (existed) {
            // add entries not already present
            IClasspathEntry[] entries = getSourceFolderEntries(javaProject, description);
            IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
            List add = new ArrayList();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                boolean present = false;
                for (int j = 0; j < rawClasspath.length; j++) {
                    IClasspathEntry existingEntry = rawClasspath[j];
                    if (existingEntry.getEntryKind() == entry.getEntryKind()) {
                        if (existingEntry.getPath().equals(entry.getPath())) {
                            present = true;
                            break;
                        }
                    }
                }
                if (!present) {
                    add.add(entry);
                }
            }
            // check if the 'required plug-ins' container is present
            boolean addRequired = false;
            if (description.hasNature(IBundleProjectDescription.PLUGIN_NATURE)) {
                addRequired = true;
                for (int i = 0; i < rawClasspath.length; i++) {
                    IClasspathEntry cpe = rawClasspath[i];
                    if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        if (MDECore.REQUIRED_PLUGINS_CONTAINER_PATH.equals(cpe.getPath())) {
                            addRequired = false;
                            break;
                        }
                    }
                }
            }
            if (addRequired) {
                add.add(ClasspathComputer.createContainerEntry());
            }
            if (!add.isEmpty()) {
                List all = new ArrayList();
                for (int i = 0; i < rawClasspath.length; i++) {
                    all.add(rawClasspath[i]);
                }
                all.addAll(add);
                javaProject.setRawClasspath((IClasspathEntry[]) all.toArray(new IClasspathEntry[all.size()]),
                        null);
            }
        } else {
            IClasspathEntry[] entries = getClassPathEntries(javaProject, description);
            javaProject.setRawClasspath(entries, null);
        }
    }
}

From source file:com.siteview.mde.internal.core.SearchablePluginsManager.java

License:Open Source License

private void computeClasspath(IJavaProject project, IProgressMonitor monitor) {
    IClasspathEntry[] classpath = new IClasspathEntry[2];
    classpath[0] = JavaCore.newContainerEntry(JavaRuntime.newDefaultJREContainerPath());
    classpath[1] = JavaCore.newContainerEntry(MDECore.JAVA_SEARCH_CONTAINER_PATH);
    try {/*from  w  ww.j av  a2 s.c o  m*/
        project.setRawClasspath(classpath, monitor);
    } catch (JavaModelException e) {
    }
}

From source file:com.siteview.mde.internal.ui.editor.monitor.LibrarySection.java

License:Open Source License

private void updateJavaClasspathLibs(String[] oldPaths, String[] newPaths) {
    IProject project = ((IModel) getPage().getModel()).getUnderlyingResource().getProject();
    IJavaProject jproject = JavaCore.create(project);
    try {//  w  ww. j  a v  a 2  s . c o  m
        IClasspathEntry[] entries = jproject.getRawClasspath();
        ArrayList toBeAdded = new ArrayList();
        int index = -1;
        entryLoop: for (int i = 0; i < entries.length; i++) {
            if (entries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                if (index == -1)
                    index = i;
                // do not add the old paths (handling deletion/renaming)
                IPath path = entries[i].getPath().removeFirstSegments(1).removeTrailingSeparator();
                for (int j = 0; j < oldPaths.length; j++)
                    if (oldPaths[j] != null && path.equals(new Path(oldPaths[j]).removeTrailingSeparator()))
                        continue entryLoop;
            } else if (entries[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                if (index == -1)
                    index = i;
            toBeAdded.add(entries[i]);
        }
        if (index == -1)
            index = entries.length;

        // add paths
        for (int i = 0; i < newPaths.length; i++) {
            if (newPaths[i] == null)
                continue;
            IClasspathEntry entry = JavaCore.newLibraryEntry(project.getFullPath().append(newPaths[i]), null,
                    null, true);
            if (!toBeAdded.contains(entry))
                toBeAdded.add(index++, entry);
        }

        if (toBeAdded.size() == entries.length)
            return;

        IClasspathEntry[] updated = (IClasspathEntry[]) toBeAdded
                .toArray(new IClasspathEntry[toBeAdded.size()]);
        jproject.setRawClasspath(updated, null);
    } catch (JavaModelException e) {
    }
}

From source file:com.siteview.mde.internal.ui.wizards.feature.AbstractCreateFeatureOperation.java

License:Open Source License

private void createProject(IProgressMonitor monitor) throws CoreException {
    CoreUtility.createProject(fProject, fLocation, monitor);
    fProject.open(monitor);/*  w ww  .  ja  v a 2s  .c o m*/
    IProjectDescription desc = fProject.getWorkspace().newProjectDescription(fProject.getName());
    desc.setLocation(fLocation);
    if (!fProject.hasNature(MDE.FEATURE_NATURE))
        CoreUtility.addNatureToProject(fProject, MDE.FEATURE_NATURE, monitor);

    if (fFeatureData.hasCustomHandler()) {
        if (!fProject.hasNature(JavaCore.NATURE_ID))
            CoreUtility.addNatureToProject(fProject, JavaCore.NATURE_ID, monitor);

        if (fFeatureData.getSourceFolderName() != null
                && fFeatureData.getSourceFolderName().trim().length() > 0) {
            IFolder folder = fProject.getFolder(fFeatureData.getSourceFolderName());
            if (!folder.exists())
                CoreUtility.createFolder(folder);
        }

        IJavaProject jproject = JavaCore.create(fProject);
        jproject.setOutputLocation(fProject.getFullPath().append(fFeatureData.getJavaBuildFolderName()),
                monitor);
        jproject.setRawClasspath(new IClasspathEntry[] {
                JavaCore.newSourceEntry(fProject.getFullPath().append(fFeatureData.getSourceFolderName())),
                JavaCore.newContainerEntry(new Path(JavaRuntime.JRE_CONTAINER)) }, monitor);
    }
}

From source file:com.siteview.mde.internal.ui.wizards.imports.FeatureImportOperation.java

License:Open Source License

private void setClasspath(IProject project, IFeatureModel model, IProgressMonitor monitor)
        throws JavaModelException {
    IJavaProject jProject = JavaCore.create(project);

    IClasspathEntry jreCPEntry = JavaCore
            .newContainerEntry(new Path("org.eclipse.jdt.launching.JRE_CONTAINER")); //$NON-NLS-1$

    String libName = model.getFeature().getInstallHandler().getLibrary();
    IClasspathEntry handlerCPEntry = JavaCore.newLibraryEntry(project.getFullPath().append(libName), null,
            null);// w  ww  .ja  v  a2s .c  om

    jProject.setRawClasspath(new IClasspathEntry[] { jreCPEntry, handlerCPEntry }, monitor);
}

From source file:com.siteview.mde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

protected void updateReferences(IProgressMonitor monitor, IProject project) throws JavaModelException {
    IJavaProject currentProject = JavaCore.create(project);
    IMonitorModelBase[] pluginstoUpdate = fData.getPluginsToUpdate();
    for (int i = 0; i < pluginstoUpdate.length; ++i) {
        IProject proj = pluginstoUpdate[i].getUnderlyingResource().getProject();
        if (currentProject.getProject().equals(proj))
            continue;
        IJavaProject javaProject = JavaCore.create(proj);
        IClasspathEntry[] cp = javaProject.getRawClasspath();
        IClasspathEntry[] updated = getUpdatedClasspath(cp, currentProject);
        if (updated != null) {
            javaProject.setRawClasspath(updated, monitor);
        }//from ww  w.j av  a 2s. c om
        try {
            updateRequiredPlugins(javaProject, monitor, pluginstoUpdate[i]);
        } catch (CoreException e) {
            MDEPlugin.log(e);
        }
    }
}

From source file:com.siteview.mde.internal.ui.wizards.plugin.NewLibraryPluginCreationOperation.java

License:Open Source License

private static void updateRequiredPlugins(IJavaProject javaProject, IProgressMonitor monitor,
        IMonitorModelBase model) throws CoreException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List classpath = new ArrayList();
    List requiredProjects = new ArrayList();
    for (int i = 0; i < entries.length; i++) {
        if (isPluginProjectEntry(entries[i])) {
            requiredProjects.add(entries[i]);
        } else {//from   ww w  . j av  a 2 s.c  o  m
            classpath.add(entries[i]);
        }
    }
    if (requiredProjects.size() <= 0)
        return;
    IFile file = PDEProject.getManifest(javaProject.getProject());
    try {
        // TODO format manifest
        Manifest manifest = new Manifest(file.getContents());
        String value = manifest.getMainAttributes().getValue(Constants.REQUIRE_BUNDLE);
        StringBuffer sb = value != null ? new StringBuffer(value) : new StringBuffer();
        if (sb.length() > 0)
            sb.append(","); //$NON-NLS-1$
        for (int i = 0; i < requiredProjects.size(); i++) {
            IClasspathEntry entry = (IClasspathEntry) requiredProjects.get(i);
            if (i > 0)
                sb.append(","); //$NON-NLS-1$
            sb.append(entry.getPath().segment(0));
            if (entry.isExported())
                sb.append(";visibility:=reexport"); // TODO is there a //$NON-NLS-1$
            // constant?
        }
        manifest.getMainAttributes().putValue(Constants.REQUIRE_BUNDLE, sb.toString());
        ByteArrayOutputStream content = new ByteArrayOutputStream();
        manifest.write(content);
        file.setContents(new ByteArrayInputStream(content.toByteArray()), true, false, monitor);
        // now update .classpath
        javaProject.setRawClasspath(
                (IClasspathEntry[]) classpath.toArray(new IClasspathEntry[classpath.size()]), monitor);
        //         ClasspathComputer.setClasspath(javaProject.getProject(), model);
    } catch (IOException e) {
    } catch (CoreException e) {
    }
}

From source file:com.siteview.mde.internal.ui.wizards.tools.ConvertProjectToPluginOperation.java

License:Open Source License

private void loadClasspathEntries(IProject project, IProgressMonitor monitor) {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] currentClassPath = new IClasspathEntry[0];
    ArrayList sources = new ArrayList();
    ArrayList libraries = new ArrayList();
    try {/*from w  ww .  jav a  2 s .c  om*/
        currentClassPath = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
    }
    for (int i = 0; i < currentClassPath.length; i++) {
        int contentType = currentClassPath[i].getEntryKind();
        if (contentType == IClasspathEntry.CPE_SOURCE) {
            String relativePath = getRelativePath(currentClassPath[i], project);
            if (relativePath.equals("")) { //$NON-NLS-1$
                sources.add("."); //$NON-NLS-1$
            } else {
                sources.add(relativePath + "/"); //$NON-NLS-1$
            }
        } else if (contentType == IClasspathEntry.CPE_LIBRARY) {
            String path = getRelativePath(currentClassPath[i], project);
            if (path.length() > 0)
                libraries.add(path);
            else
                libraries.add("."); //$NON-NLS-1$
        }
    }
    fSrcEntries = (String[]) sources.toArray(new String[sources.size()]);
    fLibEntries = (String[]) libraries.toArray(new String[libraries.size()]);

    IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1];
    System.arraycopy(currentClassPath, 0, classPath, 0, currentClassPath.length);
    classPath[classPath.length - 1] = ClasspathComputer.createContainerEntry();
    try {
        javaProject.setRawClasspath(classPath, monitor);
    } catch (JavaModelException e) {
    }
}

From source file:com.technophobia.substeps.junit.launcher.SubstepsLaunchConfigurationDelegate.java

License:Open Source License

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

        @Override/*from   w  ww . j  a v  a  2 s . c  om*/
        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.add(JavaCore.newLibraryEntry(new Path(jarFile), null, null));
                }
                project.setRawClasspath(newClasspath.toArray(new IClasspathEntry[newClasspath.size()]), null);
            } catch (final JavaModelException ex) {
                FeatureRunnerPlugin.error("Could not add substeps jars to classpath", ex);
            }
        }
    };
}