|
36 | 36 | import org.eclipse.core.resources.IResource; |
37 | 37 | import org.eclipse.core.resources.ResourcesPlugin; |
38 | 38 | import org.eclipse.core.runtime.CoreException; |
| 39 | +import org.eclipse.core.runtime.NullProgressMonitor; |
39 | 40 | import org.eclipse.core.runtime.URIUtil; |
40 | 41 | import org.eclipse.debug.core.sourcelookup.ISourceContainer; |
41 | 42 | import org.eclipse.jdt.core.IBuffer; |
|
60 | 61 | import org.eclipse.jdt.launching.IVMInstall; |
61 | 62 | import org.eclipse.jdt.launching.JavaRuntime; |
62 | 63 | import org.eclipse.jdt.launching.LibraryLocation; |
| 64 | +import org.eclipse.jdt.ls.core.internal.DecompilerResult; |
63 | 65 | import org.eclipse.jdt.ls.core.internal.JDTUtils; |
| 66 | +import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; |
| 67 | +import org.eclipse.jdt.ls.core.internal.managers.ContentProviderManager; |
64 | 68 |
|
65 | 69 | import com.microsoft.java.debug.BindingUtils; |
66 | 70 | import com.microsoft.java.debug.BreakpointLocationLocator; |
67 | 71 | import com.microsoft.java.debug.LambdaExpressionLocator; |
68 | 72 | import com.microsoft.java.debug.core.Configuration; |
69 | 73 | import com.microsoft.java.debug.core.DebugException; |
| 74 | +import com.microsoft.java.debug.core.DebugSettings; |
70 | 75 | import com.microsoft.java.debug.core.JavaBreakpointLocation; |
| 76 | +import com.microsoft.java.debug.core.DebugSettings.Switch; |
71 | 77 | import com.microsoft.java.debug.core.adapter.AdapterUtils; |
72 | 78 | import com.microsoft.java.debug.core.adapter.Constants; |
73 | 79 | import com.microsoft.java.debug.core.adapter.IDebugAdapterContext; |
@@ -303,10 +309,42 @@ private CompilationUnit asCompilationUnit(String uri) { |
303 | 309 | } else { |
304 | 310 | // For non-file uri (e.g. jdt://contents/rt.jar/java.io/PrintStream.class), |
305 | 311 | // leverage jdt to load the source contents. |
306 | | - ITypeRoot typeRoot = resolveClassFile(uri); |
307 | | - if (typeRoot != null) { |
308 | | - parser.setSource(typeRoot); |
309 | | - astUnit = (CompilationUnit) parser.createAST(null); |
| 312 | + IClassFile typeRoot = resolveClassFile(uri); |
| 313 | + try { |
| 314 | + if (typeRoot != null && typeRoot.getSourceRange() != null) { |
| 315 | + parser.setSource(typeRoot); |
| 316 | + astUnit = (CompilationUnit) parser.createAST(null); |
| 317 | + } else if (typeRoot != null && DebugSettings.getCurrent().debugSupportOnDecompiledSource == Switch.ON) { |
| 318 | + ContentProviderManager contentProvider = JavaLanguageServerPlugin.getContentProviderManager(); |
| 319 | + try { |
| 320 | + String contents = contentProvider.getSource(typeRoot, new NullProgressMonitor()); |
| 321 | + if (contents != null && !contents.isBlank()) { |
| 322 | + IJavaProject javaProject = typeRoot.getJavaProject(); |
| 323 | + if (javaProject != null) { |
| 324 | + parser.setProject(javaProject); |
| 325 | + } else { |
| 326 | + parser.setEnvironment(new String[0], new String[0], null, true); |
| 327 | + /** |
| 328 | + * See the java doc for { @link ASTParser#setSource(char[]) }, |
| 329 | + * the user need specify the compiler options explicitly. |
| 330 | + */ |
| 331 | + Map<String, String> javaOptions = JavaCore.getOptions(); |
| 332 | + javaOptions.put(JavaCore.COMPILER_SOURCE, this.latestJavaVersion); |
| 333 | + javaOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, this.latestJavaVersion); |
| 334 | + javaOptions.put(JavaCore.COMPILER_COMPLIANCE, this.latestJavaVersion); |
| 335 | + javaOptions.put(JavaCore.COMPILER_PB_ENABLE_PREVIEW_FEATURES, JavaCore.ENABLED); |
| 336 | + parser.setCompilerOptions(javaOptions); |
| 337 | + } |
| 338 | + parser.setUnitName(typeRoot.getElementName()); |
| 339 | + parser.setSource(contents.toCharArray()); |
| 340 | + astUnit = (CompilationUnit) parser.createAST(null); |
| 341 | + } |
| 342 | + } catch (Exception e) { |
| 343 | + JavaLanguageServerPlugin.logException(e.getMessage(), e); |
| 344 | + } |
| 345 | + } |
| 346 | + } catch (JavaModelException e) { |
| 347 | + // ignore |
310 | 348 | } |
311 | 349 | } |
312 | 350 | return astUnit; |
@@ -533,4 +571,58 @@ private boolean isSameURI(String uri1, String uri2) { |
533 | 571 | return false; |
534 | 572 | } |
535 | 573 | } |
| 574 | + |
| 575 | + public int[] getOriginalLineMappings(String uri) { |
| 576 | + IClassFile classFile = resolveClassFile(uri); |
| 577 | + try { |
| 578 | + if (classFile == null) { |
| 579 | + return null; |
| 580 | + } |
| 581 | + |
| 582 | + IPackageFragmentRoot packageRoot = (IPackageFragmentRoot) classFile.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); |
| 583 | + if (packageRoot != null && packageRoot.getSourceAttachmentPath() != null) { |
| 584 | + return null; |
| 585 | + } |
| 586 | + |
| 587 | + if (classFile.getSourceRange() == null) { |
| 588 | + ContentProviderManager contentProvider = JavaLanguageServerPlugin.getContentProviderManager(); |
| 589 | + try { |
| 590 | + DecompilerResult result = contentProvider.getSourceResult(classFile, new NullProgressMonitor()); |
| 591 | + if (result != null) { |
| 592 | + return result.getOriginalLineMappings(); |
| 593 | + } |
| 594 | + } catch (NoSuchMethodError e) { |
| 595 | + // ignore it if old language server version is installed. |
| 596 | + } catch (Exception e) { |
| 597 | + JavaLanguageServerPlugin.logException(e.getMessage(), e); |
| 598 | + } |
| 599 | + } |
| 600 | + } catch (JavaModelException e) { |
| 601 | + // ignore |
| 602 | + } |
| 603 | + return null; |
| 604 | + } |
| 605 | + |
| 606 | + public int[] getDecompiledLineMappings(String uri) { |
| 607 | + IClassFile classFile = resolveClassFile(uri); |
| 608 | + try { |
| 609 | + if (classFile != null && classFile.getSourceRange() == null) { |
| 610 | + ContentProviderManager contentProvider = JavaLanguageServerPlugin.getContentProviderManager(); |
| 611 | + try { |
| 612 | + DecompilerResult result = contentProvider.getSourceResult(classFile, new NullProgressMonitor()); |
| 613 | + if (result != null) { |
| 614 | + return result.getDecompiledLineMappings(); |
| 615 | + } |
| 616 | + } catch (NoSuchMethodError e) { |
| 617 | + // ignore it if old Java language server version is installed. |
| 618 | + } catch (Exception e) { |
| 619 | + JavaLanguageServerPlugin.logException(e.getMessage(), e); |
| 620 | + } |
| 621 | + } |
| 622 | + } catch (JavaModelException e) { |
| 623 | + // ignore |
| 624 | + } |
| 625 | + |
| 626 | + return null; |
| 627 | + } |
536 | 628 | } |
0 commit comments