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:org.eclipse.xtext.junit4.ui.util.JavaProjectSetupUtil.java

License:Open Source License

public static void addToClasspath(IJavaProject javaProject, IClasspathEntry newClassPathEntry)
        throws JavaModelException {
    IClasspathEntry[] newClassPath;//from   w  w  w.  j  a va2s. c  o m
    IClasspathEntry[] classPath = javaProject.getRawClasspath();
    for (IClasspathEntry classPathEntry : classPath) {
        if (classPathEntry.equals(newClassPathEntry)) {
            return;
        }
    }
    newClassPath = new IClasspathEntry[classPath.length + 1];
    System.arraycopy(classPath, 0, newClassPath, 1, classPath.length);
    newClassPath[0] = newClassPathEntry;
    javaProject.setRawClasspath(newClassPath, null);
    reallyWaitForAutoBuild();
}

From source file:org.eclipse.xtext.ui.util.JavaProjectFactory.java

License:Open Source License

@Override
protected void enhanceProject(IProject project, SubMonitor monitor, Shell shell) throws CoreException {
    super.enhanceProject(project, monitor, shell);
    if (builderIds.contains(JavaCore.BUILDER_ID)) {
        SubMonitor subMonitor = SubMonitor.convert(monitor, 10);
        try {//w ww  . ja  va2 s . c  o  m
            subMonitor.subTask(Messages.JavaProjectFactory_ConfigureJavaProject + projectName);
            IJavaProject javaProject = JavaCore.create(project);
            List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
            for (final IProject referencedProject : project.getReferencingProjects()) {
                final IClasspathEntry referencedProjectClasspathEntry = JavaCore
                        .newProjectEntry(referencedProject.getFullPath());
                classpathEntries.add(referencedProjectClasspathEntry);
            }
            for (final String folderName : folders) {
                final IFolder sourceFolder = project.getFolder(folderName);
                final IClasspathEntry srcClasspathEntry = JavaCore.newSourceEntry(sourceFolder.getFullPath());
                classpathEntries.add(srcClasspathEntry);
            }
            classpathEntries.addAll(extraClasspathEntries);

            IClasspathEntry defaultJREContainerEntry = getJreContainerEntry();
            classpathEntries.add(defaultJREContainerEntry);
            addMoreClasspathEntriesTo(classpathEntries);

            javaProject.setRawClasspath(classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]),
                    subMonitor.newChild(1));
            javaProject.setOutputLocation(new Path("/" + project.getName() + "/" + defaultOutput),
                    subMonitor.newChild(1));

            String executionEnvironmentId = JavaRuntime
                    .getExecutionEnvironmentId(defaultJREContainerEntry.getPath());
            if (executionEnvironmentId != null) {
                BuildPathSupport.setEEComplianceOptions(javaProject, executionEnvironmentId, null);
            }
        } catch (JavaModelException e) {
            logger.error(e.getMessage(), e);
        }
    }
}

From source file:org.eclipseguru.gwt.core.classpath.GwtClasspathUtil.java

License:Open Source License

/**
 * Adds the GWT classpath container to the specified project.
 * //from w w  w.j  av a 2s .  com
 * @param project
 * @param monitor
 * @param <code>true</code> if the container was added, <code>false</code>
 *        if the project already had a container entry
 * @throws CoreException
 */
public static boolean addGwtContainer(final IProject project, IProgressMonitor monitor) throws CoreException {
    monitor = ProgressUtil.monitor(monitor);
    try {
        monitor.beginTask(
                MessageFormat.format("Adding GWT classpath container to project {0}", project.getName()), 20);

        // get current classpath
        final IJavaProject javaProject = JavaCore.create(project);
        final IClasspathEntry[] oldClasspath = javaProject.getRawClasspath();

        // check if entry is already present
        final List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>(oldClasspath.length + 1);
        for (final IClasspathEntry entry : oldClasspath) {
            if (isGwtContainer(entry)) {
                return false;
            }
            newClasspath.add(entry);
        }

        ProgressUtil.checkCanceled(monitor);

        // add container entry
        newClasspath.add(JavaCore.newContainerEntry(new Path(GwtCore.GWT_CONTAINER)));

        // set new classpath
        javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[newClasspath.size()]),
                ProgressUtil.subProgressMonitor(monitor, 10));

        return true; // added
    } finally {
        monitor.done();
    }
}

From source file:org.eclipseguru.gwt.core.classpath.GwtClasspathUtil.java

License:Open Source License

/**
 * Updates the JRE container of the specified project to match the GWT
 * execution environment.// w w w  .  j ava  2 s  . c om
 * 
 * @param project
 * @param monitor
 * @param setAccessRules
 * @throws CoreException
 */
public static void updateJREContainer(final IProject project, IProgressMonitor monitor,
        final boolean setAccessRules) throws CoreException {
    monitor = ProgressUtil.monitor(monitor);
    try {
        monitor.beginTask(MessageFormat.format("Updateing JRE container of project {0}", project.getName()),
                20);

        // get project
        final IJavaProject javaProject = JavaCore.create(project);

        // set access rules if necessary
        if (setAccessRules) {
            // get current classpath
            final IClasspathEntry[] oldClasspath = javaProject.getRawClasspath();

            // update existing JRE entry
            final List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>(oldClasspath.length + 1);
            for (IClasspathEntry entry : oldClasspath) {
                if (isJREContainer(entry)) {
                    entry = JavaCore.newContainerEntry(entry.getPath(), AccessRulesUtil.getJREAccessRules(),
                            new IClasspathAttribute[0], false);
                }
                newClasspath.add(entry);
                ProgressUtil.checkCanceled(monitor);
            }

            // set new classpath
            javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[newClasspath.size()]),
                    ProgressUtil.subProgressMonitor(monitor, 10));

            ProgressUtil.checkCanceled(monitor);
        }
    } finally {
        monitor.done();
    }
}

From source file:org.eclipselabs.jar2uml.test.JarToUMLTest.java

License:Open Source License

/**
 * Test method for {@link org.eclipselabs.jar2uml.JarToUML#findJavaProjectReferences(org.eclipse.jdt.core.IJavaProject, java.util.Set)}.
 * @throws JavaModelException //from   w  ww  . ja  v  a  2 s.c o  m
 */
public void testFindJavaProjectReferences() throws JavaModelException {
    //
    // Retrieve Java project
    //
    IProject project = getProject(javatestProject);
    IJavaProject jproject = JarToUML.getJavaProject(project.getFullPath());
    //
    // Retrieve another Java project and add it to the classpath of the first project
    //
    IProject projectref = getProject(javatestReferredProject);
    IClasspathEntry[] cp = jproject.getResolvedClasspath(true);
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry entry : cp) {
        entries.add(entry);
    }
    entries.add(JavaCore.newProjectEntry(projectref.getFullPath()));
    jproject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);
    JarToUMLResources.logger.info("Java project classpath entries: " + entries);
    IJavaProject jprojectref = JarToUML.getJavaProject(projectref.getFullPath());
    //
    // Find references of the first project
    //
    Set<IJavaProject> refs = new HashSet<IJavaProject>();
    JarToUML.findJavaProjectReferences(jproject, refs);
    JarToUMLResources.logger.info("Java project references: " + refs);
    assertFalse(refs.isEmpty());
    assertTrue(refs.contains(jprojectref));
}

From source file:org.ect.codegen.JavaCodeGenerator.java

License:Open Source License

/**
 * Createa Java project. If the project exists already, nothing is changed.
 * If the project exists, but is closed, it is opened, but besides this,
 * nothing is done. If the project does not exist already, further 
 * initialization is done:/*  ww w . j av a 2 s  . c  o  m*/
 * <ul>
 * <li>The Java project nature is explicitly added to the project. 
 * <li>Default <code>src</code> and <code>bin</code> folders are created.
 * <li>The classpath is initialized with the source folder and the default JRE libraries.
 * </ul>
 * @param name Name of the Java project.
 * @param srcFolder Initial source folder.
 * @param binFolder Initial binaries folder.
 * @param monitor Progress monitor to be used.
 * @return Instance of IJavaProject.
 * @throws CoreException If an exception occurs during creation.
 */
protected IJavaProject createJavaProject(String name, String srcFolder, String binFolder,
        IProgressMonitor monitor) throws CoreException {

    monitor.beginTask("Creating Java project", 3);
    IProject project = createProject(name, monitor);

    // Create source folder.  
    IFolder folder = project.getFolder(srcFolder);
    if (!folder.exists()) {
        folder.create(true, true, NULL_MONITOR);
    }

    // Create binaries folder.  
    folder = project.getFolder(binFolder);
    if (!folder.exists()) {
        folder.create(true, true, NULL_MONITOR);
    }

    monitor.worked(1);

    // Set Java project nature.
    String[] natures = { JavaCore.NATURE_ID };
    IProjectDescription description = project.getDescription();
    description.setNatureIds(natures);
    project.setDescription(description, NULL_MONITOR);

    // Create the Java project.
    IJavaProject javaProject = JavaCore.create(project);

    // Set Java src and bin folder.
    IPath srcPath = project.getFullPath().append(srcFolder);
    IPath binPath = project.getFullPath().append(binFolder);

    javaProject.setOutputLocation(binPath, NULL_MONITOR);

    monitor.worked(1);

    // Adjust the classpath.
    if (javaProject.getRawClasspath().length <= 2) {
        IClasspathEntry[] classpath = new IClasspathEntry[2];
        classpath[0] = JavaCore.newSourceEntry(srcPath);
        classpath[1] = JavaCore.newContainerEntry(new Path(DEFAULT_JRE_CONTAINER));
        javaProject.setRawClasspath(classpath, NULL_MONITOR);
    }

    monitor.worked(1);
    monitor.done();

    return javaProject;

}

From source file:org.ect.codegen.JavaCodeGenerator.java

License:Open Source License

/**
 * Check if a classpath entry exists already and add it if not.
 * @param javaProject Java project./*ww  w.  j av  a 2  s .  c  o m*/
 * @param entry Classpath entry to be added.
 * @throws CoreException If any kind of exception occures.
 */
protected void addClasspathEntry(IJavaProject javaProject, IClasspathEntry entry) throws CoreException {

    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    for (int i = 0; i < classpath.length; i++) {
        if (classpath[i].getPath().equals(entry.getPath()))
            return;
    }

    IClasspathEntry[] newClasspath = new IClasspathEntry[classpath.length + 1];
    System.arraycopy(classpath, 0, newClasspath, 0, classpath.length);
    newClasspath[classpath.length] = entry;

    javaProject.setRawClasspath(newClasspath, NULL_MONITOR);

}

From source file:org.efaps.jasper.plugin.EFapsClasspathContainerFactory.java

License:Apache License

@Override
public void createJRClasspathContainer(final IProgressMonitor monitor, final List<IClasspathEntry> centries,
        final IJavaProject javaProject) throws JavaModelException {
    final EFapsClasspathContainer classpathContainer = new EFapsClasspathContainer(null, javaProject);
    JavaCore.setClasspathContainer(EFapsClasspathContainer.ID, new IJavaProject[] { javaProject },
            new IClasspathContainer[] { classpathContainer }, monitor);
    centries.add(JavaCore.newContainerEntry(EFapsClasspathContainer.ID, true));
    javaProject.setRawClasspath(centries.toArray(new IClasspathEntry[centries.size()]), monitor);
}

From source file:org.entirej.ide.core.cf.CFProjectHelper.java

License:Apache License

public static void removeFromClasspath(IJavaProject jproject, IPath path) throws JavaModelException {
    IClasspathEntry[] oldEntries = jproject.getRawClasspath();
    int nEntries = oldEntries.length;
    ArrayList<IClasspathEntry> list = new ArrayList<IClasspathEntry>(nEntries);
    for (int i = 0; i < nEntries; i++) {
        IClasspathEntry curr = oldEntries[i];
        if (!path.equals(curr.getPath())) {
            list.add(curr);// w  w w  . j  a va  2  s. co m
        }
    }
    IClasspathEntry[] newEntries = (IClasspathEntry[]) list.toArray(new IClasspathEntry[list.size()]);
    jproject.setRawClasspath(newEntries, null);
}

From source file:org.evolizer.core.util.projecthandling.JavaProjectHelper.java

License:Apache License

/**
 * Adds a jar.// ww w . j a va 2  s .  c  o  m
 * 
 * @param javaProject
 *            the java project
 * @param jar
 *            the jar
 * @param progressMonitor
 *            the progress monitor
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws JavaModelException
 *             if the element could not be created. Reasons include:
 *             <ul>
 *             <li>This Java element does not exist (ELEMENT_DOES_NOT_EXIST)</li>
 *             <li>A CoreException occurred while creating an underlying resource</li>
 *             <li>This package fragment root is read only (READ_ONLY)</li>
 *             <li>The name is not a valid package name (INVALID_NAME)</li>
 *             </ul>
 */
public static void addJar(IJavaProject javaProject, IPath jar, IProgressMonitor progressMonitor)
        throws IOException, JavaModelException {
    IProgressMonitor monitor = (progressMonitor == null) ? new NullProgressMonitor() : progressMonitor;
    try {
        monitor.beginTask("Adding Jar " + jar + " to Project " + javaProject.getProject().getName(), 1);
        IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
        System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
        newEntries[oldEntries.length] = JavaCore.newLibraryEntry(jar, null, null);
        javaProject.setRawClasspath(newEntries, null);
        monitor.worked(1);
    } finally {
        monitor.done();
    }
}