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:org.antlr.eclipse.core.AntlrNature.java

License:Open Source License

/**
 * Remove the ANTLR nature from a project
 * @param aProject The project to remove the nature from
 * @param aMonitor A progress monitor//from   w w  w  .  ja v  a  2  s  .c  o m
 */
public static void removeNature(final IProject aProject, final IProgressMonitor aMonitor) {
    if (aProject != null) {
        if (DEBUG) {
            System.out.println("removing ANTLR nature from project '" + aProject.getName() + "'");
        }
        try {
            if (aProject.hasNature(ID)) {
                IProjectDescription desc = aProject.getDescription();
                ArrayList<String> natures = new ArrayList<String>(Arrays.asList(desc.getNatureIds()));
                natures.remove(ID);
                desc.setNatureIds(natures.toArray(new String[natures.size()]));
                aProject.setDescription(desc, aMonitor);

                // if it's a Java Project, remove the ANTLR_HOME class path variable
                if (aProject.hasNature(JavaCore.NATURE_ID)) {
                    IJavaProject javaProject = JavaCore.create(aProject);
                    ArrayList<IClasspathEntry> rawClasspath = new ArrayList<IClasspathEntry>(
                            Arrays.asList(javaProject.getRawClasspath()));
                    boolean hadAntlrJar = false;
                    for (Iterator<IClasspathEntry> i = rawClasspath.iterator(); i.hasNext();) {
                        IClasspathEntry entry = i.next();
                        if (entry.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                            String segment0 = entry.getPath().segment(0);
                            if (AntlrCorePlugin.ANTLR_HOME.equals(segment0)) {
                                i.remove();
                                hadAntlrJar = true;
                            }
                            if ("PARSEVIEW_HOME".equals(segment0)) {
                                i.remove();
                                hadAntlrJar = true;
                            }
                        }
                    }
                    if (hadAntlrJar) {
                        javaProject.setRawClasspath(
                                rawClasspath.toArray(new IClasspathEntry[rawClasspath.size()]), null);
                    }
                }
            }
        } catch (CoreException e) {
            AntlrCorePlugin.log(e);
        }
    }
}

From source file:org.apache.buildr.eclipse.BuildrNature.java

License:Apache License

@Override
public void configure() throws CoreException {
    if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject targetProject = JavaCore.create(project);
        IClasspathEntry[] entries = targetProject.getRawClasspath();
        Path buildrContainer = new Path("org.apache.buildr.eclipse.BUILDR_CONTAINER/dependencies");
        for (IClasspathEntry entry : entries) {
            if (entry.getPath().equals(buildrContainer)) {
                return;
            }/*  ww w  .  j  ava  2  s.  c o  m*/
        }

        IClasspathEntry entry = JavaCore.newContainerEntry(buildrContainer);
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);
        newEntries[entries.length] = entry;
        targetProject.setRawClasspath(newEntries, true, new NullProgressMonitor());
    }
}

From source file:org.apache.buildr.eclipse.BuildrNature.java

License:Apache License

@Override
public void deconfigure() throws CoreException {
    if (project.hasNature(JavaCore.NATURE_ID)) {
        IJavaProject targetProject = JavaCore.create(project);
        IClasspathEntry[] entries = targetProject.getRawClasspath();
        Path buildrContainer = new Path("org.apache.buildr.eclipse.BUILDR_CONTAINER/dependencies");
        List<IClasspathEntry> newEntries = new ArrayList<>();
        for (IClasspathEntry entry : entries) {
            if (!entry.getPath().equals(buildrContainer)) {
                newEntries.add(entry);//from  ww w.  ja  v  a2 s.c  o m
            }
        }

        targetProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[newEntries.size()]), true,
                new NullProgressMonitor());
    }
}

From source file:org.apache.easyant4e.natures.EasyAntNature.java

License:Apache License

private void addDefaultJREContainer(IJavaProject javaProject) throws JavaModelException {
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    entries.addAll(Arrays.asList(javaProject.getRawClasspath()));
    entries.add(JavaRuntime.getDefaultJREContainerEntry());
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

}

From source file:org.apache.easyant4e.natures.EasyAntNature.java

License:Apache License

private void addSourceFolders(IJavaProject javaProject, String... path) throws JavaModelException {
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    entries.addAll(Arrays.asList(javaProject.getRawClasspath()));
    IResource outputLocation = javaProject.getProject().findMember("target");
    if (outputLocation == null) {
        outputLocation = createSourceFolder(javaProject.getProject(), "target");
    }// w  w w  .j a  va  2  s.  c  o  m
    javaProject.setOutputLocation(outputLocation.getFullPath(), null);
    for (String p : path) {
        IResource sourceFolder = javaProject.getProject().findMember(p);
        if (sourceFolder == null) {
            sourceFolder = createSourceFolder(javaProject.getProject(), p);
        }
        if (sourceFolder != null) {
            IPath[] exclusionPatterns = new IPath[] { outputLocation.getFullPath() };
            IClasspathEntry srcEntry = JavaCore.newSourceEntry(sourceFolder.getFullPath(), exclusionPatterns,
                    outputLocation.getFullPath());
            entries.add(srcEntry);
        }
    }
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
}

From source file:org.apache.easyant4e.natures.EasyAntNature.java

License:Apache License

private void addClassPathEntry(IJavaProject javaProject, IClasspathEntry newEntry) {
    try {//from w w w.  j  a  v a2s  .c  o m
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        List newEntries = new ArrayList(Arrays.asList(entries));
        newEntries.add(newEntry);
        entries = (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);

        javaProject.setRawClasspath(entries, javaProject.getOutputLocation(), null);
    } catch (CoreException e) {
        // unless there are issues with the JDT, this should never happen
        Activator.getEasyAntPlugin().log(IStatus.ERROR, "Cannot add ClassPath entry", e);
    }
}

From source file:org.apache.easyant4e.natures.EasyAntNature.java

License:Apache License

private void removeIvyClasspathContainer() {
    IJavaProject javaProject = JavaCore.create(project);
    ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();
    //FIXME call IvyDE command
    try {/*  w  ww.j  a va2  s.c  om*/
        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 (IvyClasspathContainerHelper.isIvyClasspathContainer(path)) {
                    continue;
                }
            }
            newEntries.add(entry);
        }
        // TODO add progress monitor
        IClasspathEntry[] newClasspathEntries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
        javaProject.setRawClasspath(newClasspathEntries, null);
    } catch (JavaModelException e) {
        Activator.getEasyAntPlugin().log(IStatus.ERROR, "Cannot remove Ivy ClassPath container", e);
    }
}

From source file:org.apache.easyant4e.tests.EclipseProjectBuilder.java

License:Apache License

private static void addSystemLibraries(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaRuntime.getDefaultJREContainerEntry();
    javaProject.setRawClasspath(newEntries, null);
}

From source file:org.apache.easyant4e.tests.ImportProjectTest.java

License:Apache License

private void assertSourceFolders(IProject project) throws Exception {
    assertTrue(project.hasNature(JavaCore.NATURE_ID));
    IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
    assertNotNull(javaProject);//from w  ww.j  ava2s  . c  om
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    entries.addAll(Arrays.asList(javaProject.getRawClasspath()));
    assertTrue(entries.size() > 0);
    HashSet<String> sourceFolders = new HashSet<String>();
    for (IClasspathEntry entry : entries) {
        if (IClasspathEntry.CPE_SOURCE == entry.getEntryKind()) {
            String path = entry.getPath().toOSString();
            assertNotNull(path);
            sourceFolders.add(path);
        }
    }
    assertTrue(sourceFolders.contains("/simplejavaproject/src/main/java"));
    assertTrue(sourceFolders.contains("/simplejavaproject/src/test/java"));
    assertTrue(sourceFolders.contains("/simplejavaproject/src/main/resources"));
    assertTrue(sourceFolders.contains("/simplejavaproject/src/test/resources"));
}

From source file:org.apache.felix.sigil.eclipse.SigilCore.java

License:Apache License

public static void makeSigilProject(IProject project, IProgressMonitor monitor) throws CoreException {
    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] = SigilCore.NATURE_ID;
    description.setNatureIds(newNatures);

    ICommand sigilBuild = description.newCommand();
    sigilBuild.setBuilderName(SigilCore.BUILDER_ID);

    ICommand javaBuild = description.newCommand();
    javaBuild.setBuilderName(JavaCore.BUILDER_ID);

    description.setBuildSpec(new ICommand[] { javaBuild, sigilBuild });

    project.setDescription(description, new SubProgressMonitor(monitor, 2));

    IJavaProject java = JavaCore.create(project);
    if (java.exists()) {
        IClasspathEntry[] cp = java.getRawClasspath();
        // check if sigil container is already on classpath - if not add it
        if (!isSigilOnClasspath(cp)) {
            ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(Arrays.asList(cp));
            entries.add(JavaCore.newContainerEntry(new Path(SigilCore.CLASSPATH_CONTAINER_PATH)));
            java.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);
        }/*from   w  ww  . ja  v  a  2s  .co  m*/
    }
}