|
54 | 54 | import org.eclipse.core.runtime.jobs.ISchedulingRule; |
55 | 55 | import org.eclipse.jdt.core.CompletionProposal; |
56 | 56 | import org.eclipse.jdt.core.Flags; |
57 | | - |
58 | 57 | import org.eclipse.jdt.core.IAnnotatable; |
59 | 58 | import org.eclipse.jdt.core.IAnnotation; |
60 | 59 | import org.eclipse.jdt.core.IBuffer; |
|
106 | 105 | import org.eclipse.jdt.core.dom.SingleVariableDeclaration; |
107 | 106 | import org.eclipse.jdt.core.dom.SuperConstructorInvocation; |
108 | 107 | import org.eclipse.jdt.core.dom.Type; |
109 | | - |
110 | 108 | import org.eclipse.jdt.core.dom.VariableDeclarationFragment; |
111 | 109 | import org.eclipse.jdt.core.manipulation.CoreASTProvider; |
112 | 110 | import org.eclipse.jdt.core.manipulation.SharedASTProviderCore; |
|
147 | 145 |
|
148 | 146 | /** |
149 | 147 | * General utilities for working with JDT APIs |
| 148 | + * |
150 | 149 | * @author Gorkem Ercan |
151 | 150 | * |
152 | 151 | */ |
@@ -298,10 +297,10 @@ static ICompilationUnit getFakeCompilationUnit(URI uri, IProgressMonitor monitor |
298 | 297 | public IBuffer createBuffer(ICompilationUnit workingCopy) { |
299 | 298 | return new DocumentAdapter(workingCopy, path); |
300 | 299 | } |
301 | | - }; |
302 | | - try { |
303 | | - return owner.newWorkingCopy(fileName, new IClasspathEntry[] { JavaRuntime.getDefaultJREContainerEntry() }, monitor); |
304 | | - } catch (JavaModelException e) { |
| 300 | + }; |
| 301 | + try { |
| 302 | + return owner.newWorkingCopy(fileName, new IClasspathEntry[] { JavaRuntime.getDefaultJREContainerEntry() }, monitor); |
| 303 | + } catch (JavaModelException e) { |
305 | 304 | return null; |
306 | 305 | } |
307 | 306 | } |
@@ -432,9 +431,13 @@ public static IClassFile resolveClassFile(String uriString){ |
432 | 431 | * @param uri with 'jdt' scheme |
433 | 432 | * @return class file |
434 | 433 | */ |
435 | | - public static IClassFile resolveClassFile(URI uri){ |
| 434 | + public static IClassFile resolveClassFile(URI uri) { |
436 | 435 | if (uri != null && JDT_SCHEME.equals(uri.getScheme()) && "contents".equals(uri.getAuthority())) { |
437 | 436 | String handleId = uri.getQuery(); |
| 437 | + int idx = handleId.indexOf("&element="); |
| 438 | + if (idx != -1) { |
| 439 | + handleId = handleId.substring(0, idx); |
| 440 | + } |
438 | 441 | IJavaElement element = JavaCore.create(handleId); |
439 | 442 | IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); |
440 | 443 | return cf; |
@@ -891,7 +894,25 @@ public static String toUri(IClassFile classFile) { |
891 | 894 | String jarName = classFile.getParent().getParent().getElementName(); |
892 | 895 | String uriString = null; |
893 | 896 | try { |
894 | | - uriString = new URI(JDT_SCHEME, "contents", PATH_SEPARATOR + jarName + PATH_SEPARATOR + packageName + PATH_SEPARATOR + classFile.getElementName(), classFile.getHandleIdentifier(), null).toASCIIString(); |
| 897 | + String elementName = classFile.getElementName(); |
| 898 | + // Use the original source file name if available |
| 899 | + String sourceFileName = SourceFileAttributeReader.getSourceFileName(classFile); |
| 900 | + String fileName = sourceFileName == null ? elementName : sourceFileName; |
| 901 | + StringBuilder pathBuilder = new StringBuilder(); |
| 902 | + pathBuilder.append(PATH_SEPARATOR).append(jarName); |
| 903 | + if (packageName != null && !packageName.isBlank()) { |
| 904 | + pathBuilder.append(PATH_SEPARATOR).append(packageName); |
| 905 | + } |
| 906 | + pathBuilder.append(PATH_SEPARATOR).append(fileName); |
| 907 | + |
| 908 | + String handleIdentifier = classFile.getHandleIdentifier(); |
| 909 | + StringBuilder query = new StringBuilder(handleIdentifier); |
| 910 | + if (!handleIdentifier.contains(elementName)) { |
| 911 | + //Add the element name to the query so decompilers can detect it (looking at you module-info.class!) |
| 912 | + query.append("&element=").append(elementName); |
| 913 | + } |
| 914 | + uriString = new URI(JDT_SCHEME, "contents", pathBuilder.toString(), query.toString(), null).toASCIIString(); |
| 915 | + |
895 | 916 | } catch (URISyntaxException e) { |
896 | 917 | JavaLanguageServerPlugin.logException("Error generating URI for class ", e); |
897 | 918 | } |
@@ -1168,26 +1189,26 @@ public static IResource findResource(URI uri, Function<URI, IResource[]> resourc |
1168 | 1189 | } |
1169 | 1190 | } |
1170 | 1191 | switch(resources.length) { |
1171 | | - case 0: |
1172 | | - return null; |
1173 | | - case 1: |
1174 | | - return resources[0]; |
1175 | | - default://several candidates if a linked resource was created before the real project was configured |
| 1192 | + case 0: |
| 1193 | + return null; |
| 1194 | + case 1: |
| 1195 | + return resources[0]; |
| 1196 | + default://several candidates if a linked resource was created before the real project was configured |
1176 | 1197 | IResource resource = null; |
1177 | 1198 | for (IResource f : resources) { |
1178 | | - //delete linked resource |
1179 | | - if (ProjectsManager.getDefaultProject().equals(f.getProject())) { |
1180 | | - try { |
1181 | | - f.delete(true, null); |
1182 | | - } catch (CoreException e) { |
| 1199 | + //delete linked resource |
| 1200 | + if (ProjectsManager.getDefaultProject().equals(f.getProject())) { |
| 1201 | + try { |
| 1202 | + f.delete(true, null); |
| 1203 | + } catch (CoreException e) { |
1183 | 1204 | JavaLanguageServerPlugin.logException(e.getMessage(), e); |
| 1205 | + } |
1184 | 1206 | } |
1185 | | - } |
1186 | | - //find closest project containing that file, in case of nested projects |
| 1207 | + //find closest project containing that file, in case of nested projects |
1187 | 1208 | if (resource == null || f.getProjectRelativePath().segmentCount() < resource.getProjectRelativePath().segmentCount()) { |
1188 | 1209 | resource = f; |
| 1210 | + } |
1189 | 1211 | } |
1190 | | - } |
1191 | 1212 | return resource; |
1192 | 1213 | } |
1193 | 1214 | } |
@@ -1970,3 +1991,4 @@ public static CompilationUnit getAst(ITypeRoot typeRoot, IProgressMonitor monito |
1970 | 1991 | } |
1971 | 1992 |
|
1972 | 1993 | } |
| 1994 | + |
0 commit comments