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();
}
}
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;
}
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;
}
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);
}
}
Aggregations