Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Return the uri of the parent class when the class file points to an inner class #3152

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -854,11 +854,30 @@ public static String toUri(IClassFile classFile) {
return null;
}

String classId = classFile.getHandleIdentifier();
String classFileName = classFile.getElementName();
// try to point to the parent class if this is an inner class, this avoid
// opening new tab for the actual same source file.
if (classFileName.contains("$")) {
String parentClassId = classId.substring(0, classId.lastIndexOf("$")) + ".class";
IJavaElement parentClass = JavaCore.create(parentClassId);
if (parentClass != null && parentClass instanceof IClassFile parentClassFile) {
try {
// they are from the same source file if the contents are the same.
if (Objects.equals(parentClassFile.getBuffer(), classFile.getBuffer())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding your comments on getBuffer(), is there ever a case where a nested class would not belong to the same classfile as the part of the element name to the left of the $ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because $ is a valid character for java class name. Given a class named as Foo$Bar, we cannot simply tell whether it's class Bar nested in Foo, or just a class named as Foo$Bar.

Though this should be a rare case.

Copy link
Contributor

@rgrunber rgrunber May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the getBuffer would only ever be called when the $ is present in the name to begin with, so at least it isn't slowing down got-to-definition for all requests.

Does exists() help ?

Copy link
Contributor Author

@jdneo jdneo May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the getBuffer would only ever be called when the $ is present in the name to begin with, so at least it isn't slowing down got-to-definition for all requests.

Yes, that's correct.

Does exists() help ?

I think the answer is no. My understanding is that, as long as the object returns from JavaCore.create is not null, that means it's in the Java model, then exists() will always return true.

Considering the original request is not 'hot', maybe hang on this change a little bit? At least there is a solution (though not perfect).

classId = parentClassFile.getHandleIdentifier();
classFileName = parentClassFile.getElementName();
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Error get content of the class file", e);
}
}
}
String packageName = classFile.getParent().getElementName();
String jarName = classFile.getParent().getParent().getElementName();
String uriString = null;
try {
uriString = new URI(JDT_SCHEME, "contents", PATH_SEPARATOR + jarName + PATH_SEPARATOR + packageName + PATH_SEPARATOR + classFile.getElementName(), classFile.getHandleIdentifier(), null).toASCIIString();
uriString = new URI(JDT_SCHEME, "contents", PATH_SEPARATOR + jarName + PATH_SEPARATOR + packageName + PATH_SEPARATOR + classFileName, classId, null).toASCIIString();
} catch (URISyntaxException e) {
JavaLanguageServerPlugin.logException("Error generating URI for class ", e);
}
Expand Down
Loading