use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class HandleFactory method getJarPkgFragmentRoot.
/**
* Returns the package fragment root that corresponds to the given jar path.
* See createOpenable(...) for the format of the jar path string.
* If not null, uses the given scope as a hint for getting Java project handles.
*/
private PackageFragmentRoot getJarPkgFragmentRoot(String resourcePathString, int jarSeparatorIndex, String jarPathString, IJavaSearchScope scope) {
IPath jarPath = new Path(jarPathString);
Object target = JavaModel.getTarget(jarPath, false);
if (target instanceof IFile) {
// internal jar: is it on the classpath of its project?
// e.g. org.eclipse.swt.win32/ws/win32/swt.jar
// is NOT on the classpath of org.eclipse.swt.win32
IFile jarFile = (IFile) target;
JavaProject javaProject = (JavaProject) this.javaModel.getJavaProject(jarFile);
try {
IClasspathEntry entry = javaProject.getClasspathEntryFor(jarPath);
if (entry != null) {
return (PackageFragmentRoot) javaProject.getPackageFragmentRoot(jarFile);
}
} catch (JavaModelException e) {
// ignore and try to find another project
}
}
// walk projects in the scope and find the first one that has the given jar path in its classpath
IJavaProject[] projects;
if (scope != null) {
if (scope instanceof AbstractJavaSearchScope) {
PackageFragmentRoot root = (PackageFragmentRoot) ((AbstractJavaSearchScope) scope).packageFragmentRoot(resourcePathString, jarSeparatorIndex, jarPathString);
if (root != null)
return root;
} else {
IPath[] enclosingProjectsAndJars = scope.enclosingProjectsAndJars();
int length = enclosingProjectsAndJars.length;
projects = new IJavaProject[length];
int index = 0;
for (int i = 0; i < length; i++) {
IPath path = enclosingProjectsAndJars[i];
if (path.segmentCount() == 1) {
projects[index++] = this.javaModel.getJavaProject(path.segment(0));
}
}
if (index < length) {
System.arraycopy(projects, 0, projects = new IJavaProject[index], 0, index);
}
PackageFragmentRoot root = getJarPkgFragmentRoot(jarPath, target, projects);
if (root != null) {
return root;
}
}
}
// not found in the scope, walk all projects
try {
projects = this.javaModel.getJavaProjects();
} catch (JavaModelException e) {
// java model is not accessible
return null;
}
return getJarPkgFragmentRoot(jarPath, target, projects);
}
use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class HandleFactory method getJarPkgFragmentRoot.
private PackageFragmentRoot getJarPkgFragmentRoot(IPath jarPath, Object target, IJavaProject[] projects) {
for (int i = 0, projectCount = projects.length; i < projectCount; i++) {
try {
JavaProject javaProject = (JavaProject) projects[i];
IClasspathEntry classpathEnty = javaProject.getClasspathEntryFor(jarPath);
if (classpathEnty != null) {
if (target instanceof IFile) {
// internal jar
return (PackageFragmentRoot) javaProject.getPackageFragmentRoot((IFile) target);
} else {
// external jar
return (PackageFragmentRoot) javaProject.getPackageFragmentRoot0(jarPath);
}
}
} catch (JavaModelException e) {
// JavaModelException from getResolvedClasspath - a problem occurred while accessing project: nothing we can do, ignore
}
}
return null;
}
use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class ReorgCorrectionsSubProcessor method canModifyAccessRules.
private static boolean canModifyAccessRules(IBinding binding) {
IJavaElement element = binding.getJavaElement();
if (element == null)
return false;
IPackageFragmentRoot root = JavaModelUtil.getPackageFragmentRoot(element);
if (root == null)
return false;
try {
IClasspathEntry classpathEntry = root.getRawClasspathEntry();
if (classpathEntry == null)
return false;
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
return true;
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
ClasspathContainerInitializer classpathContainerInitializer = JavaCore.getClasspathContainerInitializer(classpathEntry.getPath().segment(0));
IStatus status = classpathContainerInitializer.getAccessRulesStatus(classpathEntry.getPath(), root.getJavaProject());
return status.isOK();
}
} catch (JavaModelException e) {
return false;
}
return false;
}
use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class ClasspathBuilderTest method rawClasspathShouldBeContained3Arguments.
@Test
public void rawClasspathShouldBeContained3Arguments() throws Exception {
createTestProject();
library.add("/lib");
sourceFolders.add("/src");
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject("/project");
IJavaProject iJavaProject = JavaCore.create(project);
classpathBuilder.generateClasspath(iJavaProject, sourceFolders, library);
List<IClasspathEntry> classpathEntries = Arrays.asList(iJavaProject.getRawClasspath());
assertThat(classpathEntries).onProperty("path").containsOnly(new Path(JREContainerInitializer.JRE_CONTAINER), new Path("/project/src"), new Path(root + "/project/lib/a.jar"));
}
use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.
the class JavaProjectHelper method removeFromClasspath.
public static void removeFromClasspath(IJavaProject jproject, IPath path) throws JavaModelException {
IClasspathEntry[] oldEntries = jproject.getRawClasspath();
int nEntries = oldEntries.length;
ArrayList list = new ArrayList(nEntries);
for (int i = 0; i < nEntries; i++) {
IClasspathEntry curr = oldEntries[i];
if (!path.equals(curr.getPath())) {
list.add(curr);
}
}
IClasspathEntry[] newEntries = (IClasspathEntry[]) list.toArray(new IClasspathEntry[list.size()]);
jproject.setRawClasspath(newEntries, null);
}
Aggregations