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.motorola.studio.android.model.ProjectCreationSupport.java

License:Apache License

/**
 * Setup src folders//from   www .  j  a  v  a 2  s.  com
 * @param javaProject
 * @param sourceFolder
 * @param monitor
 * @throws JavaModelException
 */
private static void setupSourceFolders(IJavaProject javaProject, List<String> sourceFolders,
        IProgressMonitor monitor) throws JavaModelException {
    monitor.beginTask(AndroidNLS.UI_ProjectCreationSupport_Preparing_Source_Folders_Task,
            (sourceFolders.size() * 100) + 100);
    try {
        IProject project = javaProject.getProject();
        IClasspathEntry[] entries = javaProject.getRawClasspath();

        for (String sourceFolder : sourceFolders) {
            IFolder srcFolder = project.getFolder(sourceFolder);
            entries = removeClasspathEntry(entries, srcFolder);
            entries = removeClasspathEntry(entries, srcFolder.getParent());
            entries = ProjectUtils.addEntryToClasspath(entries,
                    JavaCore.newSourceEntry(srcFolder.getFullPath()));
            monitor.worked(100);
        }

        javaProject.setRawClasspath(entries, new SubProgressMonitor(monitor, 100));
    } finally {
        monitor.done();
    }
}

From source file:com.mtcflow.project.util.MTCProjectSupport.java

License:Open Source License

private IJavaProject createProject(String projName) throws Exception {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    if (projName == null || projName.trim().length() == 0)
        return null;

    // create eclipse project
    IProject project = root.getProject(projName);
    if (project.exists())
        project.delete(true, null);// w  ww  . j  av a 2s. co  m

    project.create(null);
    project.open(null);

    addNature(project);

    // create java project
    IJavaProject javaProject = JavaCore.create(project);

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

    // add libs to project class path
    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));
    }

    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

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

    IPackageFragmentRoot srcRoot = 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(srcRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    return javaProject;
}

From source file:com.nginious.http.plugin.NewProjectWizard.java

License:Apache License

public boolean performFinish() {
    String name = pageOne.getProjectName();
    IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(name);

    if (newProject.exists()) {
        throw new RuntimeException("Project exists!");
    }//  w  w  w. j  ava 2 s .  c  o m

    try {
        int listenPort = pageOne.getListenPort();

        if (!pageOne.validate(listenPort)) {
            return false;
        }

        IProgressMonitor progressMonitor = new NullProgressMonitor();

        // Create project
        newProject.create(progressMonitor);
        newProject.open(progressMonitor);

        // Create folder structure
        String[] paths = { "src", "WebContent", "WebContent/WEB-INF", "WebContent/WEB-INF/classes",
                "WebContent/WEB-INF/lib", "WebContent/WEB-INF/xsp" };
        addToProjectStructure(newProject, paths);

        ClassPathBuilder builder = new ClassPathBuilder(newProject, progressMonitor);
        builder.build(progressMonitor);

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

        // Create java project
        IJavaProject javaProject = JavaCore.create(newProject);

        // Set classes output folder
        IFolder classesFolder = newProject.getFolder("WebContent/WEB-INF/classes");
        javaProject.setOutputLocation(classesFolder.getFullPath(), null);

        // Set classpath
        IClasspathEntry[] entries = builder.getClassPath();
        javaProject.setRawClasspath(entries, progressMonitor);

        // Set source folder
        IFolder sourceFolder = newProject.getFolder("src");
        IPackageFragmentRoot root = 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(root.getPath());
        javaProject.setRawClasspath(newEntries, null);
        BasicNewProjectResourceWizard.updatePerspective(this.configurationElement);

        newProject.setPersistentProperty(NginiousPlugin.LISTEN_PORT_PROP_KEY, Integer.toString(listenPort));
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_URL_PROP_KEY, pageOne.getPublishUrl());
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_USERNAME_PROP_KEY,
                pageOne.getPublishUsername());
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_PASSWORD_PROP_KEY,
                pageOne.getPublishPassword());
        newProject.setPersistentProperty(NginiousPlugin.MIN_MEMORY_PROP_KEY,
                Integer.toString(pageOne.getMinMemory()));
        newProject.setPersistentProperty(NginiousPlugin.MAX_MEMORY_PROP_KEY,
                Integer.toString(pageOne.getMaxMemory()));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    return true;
}

From source file:com.nginious.http.plugin.ServerManager.java

License:Apache License

private boolean updateProjectWithPluginVersion(IProject project) {
    logger.log("ENTER ServerManager.updateProjectWithPluginVersion project={0}", project);

    try {/*from   w  w w  .  j  av a  2s  .  com*/
        URL apiJar = NginiousPlugin.getApiJar();
        String filePath = apiJar.toString();
        filePath = filePath.substring(5);
        Path apiJarPath = new Path(filePath);

        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();
        boolean changed = false;

        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                IPath path = entry.getPath();

                if (path.lastSegment() != null && path.lastSegment().endsWith("nginious-api.jar")) {
                    changed = true;
                    entry = JavaCore.newLibraryEntry(apiJarPath, null, null);
                }
            }

            newEntries.add(entry);
        }

        if (changed) {
            IProgressMonitor progress = new NullProgressMonitor();
            entries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            javaProject.setRawClasspath(entries, progress);
            javaProject.save(progress, true);
        }

        logger.log("EXIT ServerManager.updateProjectWithPluginVersion changed={0}", changed);
        return changed;
    } catch (JavaModelException e) {
        logger.log("ServerManager.updateProkectWithPluginVersion exception", e);
        return false;
    } catch (IOException e) {
        logger.log("ServerManager.updateProkectWithPluginVersion exception", e);
        return false;
    }
}

From source file:com.proxiad.emfcustomizer.stylesheet.dsl.contentassist.StylesheetProposalProvider.java

License:Open Source License

/**
 * For an IJavaProject, browse all the ClasspathEntry and check if theirs
 * OutputLocation() is corresponding to a given IFolder
 * //from   ww w. java2  s .c o  m
 * @param javaProject
 *            an IJavaProject (a project with JAVA nature)
 * @param folder
 *            a given IFolder
 * @return returns true if one of the outputLocation in classpathEntry of
 *         the javaProject is equal to a given folder.getFullPath().
 */
private boolean checkOutputLocationForEachFolder(IJavaProject javaProject, IFolder folder) {
    boolean res = false;
    try {
        IClasspathEntry[] classpathEntry = javaProject.getRawClasspath();
        for (IClasspathEntry iClasspathEntry : classpathEntry) {
            if (iClasspathEntry.getOutputLocation() != null) {
                res = iClasspathEntry.getOutputLocation().equals(folder.getFullPath());
            }
        }
    } catch (JavaModelException e) {
        // ignore
    }
    return res;
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerContentProvider.java

License:Open Source License

@Override
protected Object[] getPackageFragmentRoots(IJavaProject project) throws JavaModelException {
    if (!project.getProject().isOpen())
        return NO_CHILDREN;

    List<Object> result = new ArrayList<Object>();

    IPackageFragmentRoot[] roots = project.getPackageFragmentRoots();
    for (int i = 0; i < roots.length; i++) {
        IPackageFragmentRoot root = roots[i];
        IClasspathEntry classpathEntry = root.getRawClasspathEntry();
        int entryKind = classpathEntry.getEntryKind();
        if (entryKind == IClasspathEntry.CPE_CONTAINER) {
            // all ClassPathContainers are added later
        } else if (fShowLibrariesNode
                && (entryKind == IClasspathEntry.CPE_LIBRARY || entryKind == IClasspathEntry.CPE_VARIABLE)) {
            IResource resource = root.getResource();
            if (resource != null && project.getResource().equals(resource.getParent())) {
                // show resource as child of project, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=141906
                result.add(resource);/* w ww  .  j a  v  a2s. c o m*/
            } else {
                // skip: will add the referenced library node later
            }
        } else {
            if (isProjectPackageFragmentRoot(root)) {
                // filter out package fragments that correspond to projects and
                // replace them with the package fragments directly
                Object[] fragments = getPackageFragmentRootContent(root);
                for (int j = 0; j < fragments.length; j++) {
                    result.add(fragments[j]);
                }
            } else {
                result.add(root);
            }
        }
    }

    if (fShowLibrariesNode) {
        result.add(new LibraryContainer(project));
    }

    // separate loop to make sure all containers are on the classpath (even empty ones)
    IClasspathEntry[] rawClasspath = project.getRawClasspath();
    for (int i = 0; i < rawClasspath.length; i++) {
        IClasspathEntry classpathEntry = rawClasspath[i];
        if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
            result.add(new ClassPathContainer(project, classpathEntry));
        }
    }
    Object[] resources = project.getNonJavaResources();
    for (int i = 0; i < resources.length; i++) {
        result.add(resources[i]);
    }
    return result.toArray();
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathContainer.java

License:Apache License

private IClasspathEntry[] constructModifiedClasspath(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry newEntry = JavaCore.newContainerEntry(path, null, new IClasspathAttribute[0], false);
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(Arrays.asList(entries));
    int index = 0;
    boolean mustReplace = false;
    for (IClasspathEntry entry : newEntries) {
        if (entry.getPath().equals(newEntry.getPath())) {
            mustReplace = true;/*from www . ja  v a2s  .  c om*/
            break;
        }
        index++;
    }
    if (mustReplace) {
        newEntries.set(index, newEntry);
    } else {
        newEntries.add(newEntry);
    }
    return (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathUtil.java

License:Apache License

/**
 * Search the Ceylon classpath containers within the specified Java project
 * /*w  w w.j  a  v a  2 s  . c  o  m*/
 * @param javaProject
 *            the project to search into
 * @return the Ivy classpath container if found
 */
public static List<CeylonClasspathContainer> getCeylonClasspathContainers(IJavaProject javaProject) {
    List<CeylonClasspathContainer> containers = new ArrayList<CeylonClasspathContainer>();
    if (FakeProjectManager.isFake(javaProject) || !javaProject.exists()) {
        return containers;
    }
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath path = entry.getPath();
                if (isCeylonClasspathContainer(path)) {
                    IClasspathContainer cp = JavaCore.getClasspathContainer(path, javaProject);
                    if (cp instanceof CeylonClasspathContainer) {
                        containers.add((CeylonClasspathContainer) cp);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        e.printStackTrace();
    }
    return containers;
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonClasspathUtil.java

License:Apache License

/**
 * Search the Ivy classpath entry within the specified Java project with the specific path
 * /*from w ww  .ja v a2  s  .c o  m*/
 * @param containerPath
 *            the path of the container
 * @param javaProject
 *            the project to search into
 * @return the Ivy classpath container if found, otherwise return <code>null</code>
 */
public static IClasspathEntry getCeylonClasspathEntry(IPath containerPath, IJavaProject javaProject) {
    if (FakeProjectManager.isFake(javaProject) || !javaProject.exists()) {
        return null;
    }
    try {
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (containerPath.equals(entry.getPath())) {
                    return entry;
                }
            }
        }
    } catch (JavaModelException e) {
        // unless there are issues with the JDT, this should never happen
        e.printStackTrace();
    }
    return null;
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonProjectModulesContainer.java

License:Apache License

private IClasspathEntry[] constructModifiedClasspath(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry newEntry = JavaCore.newContainerEntry(path, null, new IClasspathAttribute[0], false);
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(asList(entries));
    int index = 0;
    boolean mustReplace = false;
    boolean projectModulesEntryWasExported = false;
    for (IClasspathEntry entry : newEntries) {
        if (entry.getPath().equals(newEntry.getPath())) {
            mustReplace = true;/*  ww w  .  jav  a 2 s.  c  o m*/
            projectModulesEntryWasExported = entry.isExported();
            break;
        }
        index++;
    }

    newEntry = JavaCore.newContainerEntry(path, null, new IClasspathAttribute[0],
            projectModulesEntryWasExported);
    if (mustReplace) {
        newEntries.set(index, newEntry);
    } else {
        newEntries.add(newEntry);
    }
    return (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
}