use of org.eclipse.jdt.internal.core.Openable in project che by eclipse.
the class HandleFactory method createOpenable.
/**
* Creates an Openable handle from the given resource path.
* The resource path can be a path to a file in the workbench (e.g. /Proj/com/ibm/jdt/core/HandleFactory.java)
* or a path to a file in a jar file - it then contains the path to the jar file and the path to the file in the jar
* (e.g. c:/jdk1.2.2/jre/lib/rt.jar|java/lang/Object.class or /Proj/rt.jar|java/lang/Object.class)
* NOTE: This assumes that the resource path is the toString() of an IPath,
* in other words, it uses the IPath.SEPARATOR for file path
* and it uses '/' for entries in a zip file.
* If not null, uses the given scope as a hint for getting Java project handles.
*/
public Openable createOpenable(String resourcePath, IJavaSearchScope scope) {
int separatorIndex;
if ((separatorIndex = resourcePath.indexOf(IJavaSearchScope.JAR_FILE_ENTRY_SEPARATOR)) > -1) {
// path to a class file inside a jar
// Optimization: cache package fragment root handle and package handles
int rootPathLength;
if (this.lastPkgFragmentRootPath == null || (rootPathLength = this.lastPkgFragmentRootPath.length()) != resourcePath.length() || !resourcePath.regionMatches(0, this.lastPkgFragmentRootPath, 0, rootPathLength)) {
String jarPath = resourcePath.substring(0, separatorIndex);
PackageFragmentRoot root = getJarPkgFragmentRoot(resourcePath, separatorIndex, jarPath, scope);
if (root == null)
// match is outside classpath
return null;
this.lastPkgFragmentRootPath = jarPath;
this.lastPkgFragmentRoot = root;
this.packageHandles = new HashtableOfArrayToObject(5);
}
// create handle
String classFilePath = resourcePath.substring(separatorIndex + 1);
String[] simpleNames = new Path(classFilePath).segments();
String[] pkgName;
int length = simpleNames.length - 1;
if (length > 0) {
pkgName = new String[length];
System.arraycopy(simpleNames, 0, pkgName, 0, length);
} else {
pkgName = CharOperation.NO_STRINGS;
}
IPackageFragment pkgFragment = (IPackageFragment) this.packageHandles.get(pkgName);
if (pkgFragment == null) {
pkgFragment = this.lastPkgFragmentRoot.getPackageFragment(pkgName);
this.packageHandles.put(pkgName, pkgFragment);
}
IClassFile classFile = pkgFragment.getClassFile(simpleNames[length]);
return (Openable) classFile;
} else {
// path to a file in a directory
// Optimization: cache package fragment root handle and package handles
int rootPathLength = -1;
if (this.lastPkgFragmentRootPath == null || !(resourcePath.startsWith(this.lastPkgFragmentRootPath) && !org.eclipse.jdt.internal.compiler.util.Util.isExcluded(resourcePath.toCharArray(), this.lastPkgFragmentRoot.fullInclusionPatternChars(), this.lastPkgFragmentRoot.fullExclusionPatternChars(), false) && (rootPathLength = this.lastPkgFragmentRootPath.length()) > 0 && resourcePath.charAt(rootPathLength) == '/')) {
PackageFragmentRoot root = getPkgFragmentRoot(resourcePath);
if (root == null)
// match is outside classpath
return null;
this.lastPkgFragmentRoot = root;
this.lastPkgFragmentRootPath = this.lastPkgFragmentRoot.internalPath().toString();
this.packageHandles = new HashtableOfArrayToObject(5);
}
// create handle
resourcePath = resourcePath.substring(this.lastPkgFragmentRootPath.length() + 1);
String[] simpleNames = new Path(resourcePath).segments();
String[] pkgName;
int length = simpleNames.length - 1;
if (length > 0) {
pkgName = new String[length];
System.arraycopy(simpleNames, 0, pkgName, 0, length);
} else {
pkgName = CharOperation.NO_STRINGS;
}
IPackageFragment pkgFragment = (IPackageFragment) this.packageHandles.get(pkgName);
if (pkgFragment == null) {
pkgFragment = this.lastPkgFragmentRoot.getPackageFragment(pkgName);
this.packageHandles.put(pkgName, pkgFragment);
}
String simpleName = simpleNames[length];
if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(simpleName)) {
ICompilationUnit unit = pkgFragment.getCompilationUnit(simpleName);
return (Openable) unit;
} else if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(simpleName)) {
IClassFile classFile = pkgFragment.getClassFile(simpleName);
return (Openable) classFile;
}
return null;
}
}
Aggregations