use of org.eclipse.jdt.internal.core.search.AbstractJavaSearchScope 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);
}
Aggregations