Search in sources :

Example 31 with IClasspathContainer

use of org.eclipse.jdt.core.IClasspathContainer in project evosuite by EvoSuite.

the class TestGenerationAction method fixJUnitClassPath.

/**
 * If we generate JUnit tests, we need to make sure that JUnit is on the
 * classpath of the project, otherwise we will see compile errors
 *
 * @param project
 */
public void fixJUnitClassPath(IJavaProject project) {
    IPath junitPath = JUnitCore.JUNIT4_CONTAINER_PATH;
    boolean hasJUnit = false;
    boolean hasEvoSuite = false;
    boolean hasOldEvoSuite = false;
    try {
        Path containerPath = new Path("org.evosuite.eclipse.classpathContainerInitializer");
        IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, project);
        System.out.println("EvoSuite JAR at: " + container.getPath().toOSString());
        IClasspathEntry[] oldEntries = project.getRawClasspath();
        ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(oldEntries.length + 1);
        IClasspathEntry cpentry = JavaCore.newContainerEntry(junitPath);
        for (int i = 0; i < oldEntries.length; i++) {
            IClasspathEntry curr = oldEntries[i];
            // Check if JUnit is already in the build path
            if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                IPath path = curr.getPath();
                if (path.equals(cpentry.getPath())) {
                    hasJUnit = true;
                }
                if (path.equals(container.getPath())) {
                    hasEvoSuite = true;
                }
            } else if (curr.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                // Check for older EvoSuite entries
                IPath path = curr.getPath();
                if (path.toFile().getName().equals(Activator.EVOSUITE_JAR)) {
                    if (path.equals(container.getPath())) {
                        System.out.println("Is current evosuite!");
                        hasEvoSuite = true;
                    } else {
                        System.out.println("Is NOT current evosuite!");
                        hasOldEvoSuite = true;
                        continue;
                    }
                }
                if (path.equals(cpentry.getPath())) {
                    hasJUnit = true;
                }
                if (path.equals(container.getPath())) {
                    hasEvoSuite = true;
                }
            }
            if (curr != null) {
                newEntries.add(curr);
            }
        }
        if (hasJUnit && hasEvoSuite && !hasOldEvoSuite) {
            return;
        }
        // add the entry
        if (!hasJUnit) {
            newEntries.add(cpentry);
        }
        if (!hasEvoSuite && container != null) {
            for (IClasspathEntry entry : container.getClasspathEntries()) {
                newEntries.add(entry);
            }
        }
        System.out.println("New classpath: " + newEntries);
        // newEntries.add(JavaCore.newContainerEntry(EvoSuiteClasspathContainer.ID));
        // Convert newEntries to an array
        IClasspathEntry[] newCPEntries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
        project.setRawClasspath(newCPEntries, null);
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer)

Example 32 with IClasspathContainer

use of org.eclipse.jdt.core.IClasspathContainer in project bndtools by bndtools.

the class BndContainerInitializer method requestClasspathContainerUpdate.

/**
 * Request the BndContainer for the project, if there is one, be updated. This will not create one if there is not
 * already one.
 *
 * @param javaProject The java project of interest. Must not be null.
 * @return true if the classpath container was updated.
 * @throws CoreException
 */
public static boolean requestClasspathContainerUpdate(IJavaProject javaProject) throws CoreException {
    IClasspathContainer oldContainer = getClasspathContainer(javaProject);
    if (oldContainer == null) {
        // project does not have a BndContainer
        return false;
    }
    ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(BndtoolsConstants.BND_CLASSPATH_ID.segment(0));
    if (initializer != null) {
        initializer.requestClasspathContainerUpdate(BndtoolsConstants.BND_CLASSPATH_ID, javaProject, null);
    }
    return getClasspathContainer(javaProject) != oldContainer;
}
Also used : IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer)

Example 33 with IClasspathContainer

use of org.eclipse.jdt.core.IClasspathContainer in project flux by eclipse.

the class JavaModelUtil method getClasspathEntryToEdit.

/**
 * Helper method that tests if an classpath entry can be found in a
 * container. <code>null</code> is returned if the entry can not be found
 * or if the container does not allows the configuration of source
 * attachments
 * @param jproject The container's parent project
 * @param containerPath The path of the container
 * @param libPath The path of the library to be found
 * @return IClasspathEntry A classpath entry from the container of
 * <code>null</code> if the container can not be modified.
 * @throws JavaModelException thrown if accessing the container failed
 */
public static IClasspathEntry getClasspathEntryToEdit(IJavaProject jproject, IPath containerPath, IPath libPath) throws JavaModelException {
    IClasspathContainer container = JavaCore.getClasspathContainer(containerPath, jproject);
    ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(containerPath.segment(0));
    if (container != null && initializer != null && initializer.canUpdateClasspathContainer(containerPath, jproject)) {
        return findEntryInContainer(container, libPath);
    }
    // attachment not possible
    return null;
}
Also used : IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) ClasspathContainerInitializer(org.eclipse.jdt.core.ClasspathContainerInitializer)

Example 34 with IClasspathContainer

use of org.eclipse.jdt.core.IClasspathContainer in project lwjgl by LWJGL.

the class LWJGLClasspathContainerInitializer method rebindClasspathEntries.

private static void rebindClasspathEntries(IJavaModel model, IPath containerPath) throws JavaModelException {
    List<IJavaProject> affectedProjects = new ArrayList<IJavaProject>();
    IJavaProject[] projects = model.getJavaProjects();
    for (int i = 0; i < projects.length; i++) {
        IJavaProject project = projects[i];
        IClasspathEntry[] entries = project.getRawClasspath();
        for (int k = 0; k < entries.length; k++) {
            IClasspathEntry curr = entries[k];
            if (curr.getEntryKind() == IClasspathEntry.CPE_CONTAINER && containerPath.equals(curr.getPath())) {
                affectedProjects.add(project);
            }
        }
    }
    if (!affectedProjects.isEmpty()) {
        IJavaProject[] affected = (IJavaProject[]) affectedProjects.toArray(new IJavaProject[affectedProjects.size()]);
        IClasspathContainer[] containers = new IClasspathContainer[affected.length];
        for (int i = 0; i < containers.length; i++) {
            containers[i] = getNewContainer(containerPath);
        }
        JavaCore.setClasspathContainer(containerPath, affected, containers, null);
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer)

Aggregations

IClasspathContainer (org.eclipse.jdt.core.IClasspathContainer)34 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)16 IJavaProject (org.eclipse.jdt.core.IJavaProject)16 IPath (org.eclipse.core.runtime.IPath)13 CoreException (org.eclipse.core.runtime.CoreException)10 ClasspathContainerInitializer (org.eclipse.jdt.core.ClasspathContainerInitializer)8 ArrayList (java.util.ArrayList)6 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 Map (java.util.Map)5 IProject (org.eclipse.core.resources.IProject)5 HashMap (java.util.HashMap)4 WeakHashMap (java.util.WeakHashMap)3 IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)3 ILiferayRuntime (com.liferay.ide.server.core.ILiferayRuntime)2 PortalBundle (com.liferay.ide.server.core.portal.PortalBundle)2 IResource (org.eclipse.core.resources.IResource)2 IStatus (org.eclipse.core.runtime.IStatus)2 Path (org.eclipse.core.runtime.Path)2 IJavaModelStatus (org.eclipse.jdt.core.IJavaModelStatus)2 ClasspathDecorations (org.eclipse.jst.common.jdt.internal.classpath.ClasspathDecorations)2