IHierarchy hierarchy = model.getHierarchy();
ISourceLocation sourceLocation = onType.getSourceLocation();
String canonicalFilePath = model.getCanonicalFilePath(sourceLocation.getSourceFile());
int lineNumber = sourceLocation.getLine();
// Find the relevant source file node first
IProgramElement node = hierarchy.findNodeForSourceFile(hierarchy.getRoot(), canonicalFilePath);
if (node == null) {
// Does not exist in the model - probably an inpath
String bpath = onType.getBinaryPath();
if (bpath == null) {
return model.getHandleProvider().createHandleIdentifier(createFileStructureNode(model, canonicalFilePath));
} else {
IProgramElement programElement = model.getHierarchy().getRoot();
// =Foo/,<g(G.class[G
StringBuffer phantomHandle = new StringBuffer();
// =Foo
phantomHandle.append(programElement.getHandleIdentifier());
// /, - the comma is a 'well defined char' that means inpath
phantomHandle.append(HandleProviderDelimiter.PACKAGEFRAGMENTROOT.getDelimiter()).append(
HandleProviderDelimiter.PHANTOM.getDelimiter());
int pos = bpath.indexOf('!');
if (pos != -1) {
// jar or dir
String jarPath = bpath.substring(0, pos);
String element = model.getHandleElementForInpath(jarPath);
if (element != null) {
phantomHandle.append(element);
}
}
// <g
String packageName = onType.getPackageName();
phantomHandle.append(HandleProviderDelimiter.PACKAGEFRAGMENT.getDelimiter()).append(packageName);
// (G.class
// could fix the binary path to only be blah.class bit
int dotClassPosition = bpath.lastIndexOf(".class");// what to do if -1
if (dotClassPosition == -1) {
phantomHandle.append(HandleProviderDelimiter.CLASSFILE.getDelimiter()).append("UNKNOWN.class");
} else {
int startPosition = dotClassPosition;
char ch;
while (startPosition > 0 && ((ch = bpath.charAt(startPosition)) != '/' && ch != '\\' && ch != '!')) {
startPosition--;
}
String classFile = bpath.substring(startPosition + 1, dotClassPosition + 6);
phantomHandle.append(HandleProviderDelimiter.CLASSFILE.getDelimiter()).append(classFile);
}
// [G
phantomHandle.append(HandleProviderDelimiter.TYPE.getDelimiter()).append(onType.getClassName());
return phantomHandle.toString();
}
} else {
// Check if there is a more accurate child node of that source file node:
IProgramElement closernode = hierarchy.findCloserMatchForLineNumber(node, lineNumber);
if (closernode == null) {
return node.getHandleIdentifier();
} else {
return closernode.getHandleIdentifier();
}
}
}