Skip to content

Commit 211c47a

Browse files
Expose a command to resolve the source mapping for the specified stacktrace (#354)
1 parent 24a7260 commit 211c47a

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/adapter/handler/LaunchRequestHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ protected CompletableFuture<Response> launch(LaunchArguments launchArguments, Re
258258
return resultFuture;
259259
}
260260

261-
private static final Pattern STACKTRACE_PATTERN = Pattern.compile("\\s+at\\s+([\\w$\\.]+\\/)?(([\\w$]+\\.)*[\\w$]+)\\(([\\w-$]+\\.java:\\d+)\\)");
261+
private static final Pattern STACKTRACE_PATTERN = Pattern.compile("\\s+at\\s+([\\w$\\.]+\\/)?(([\\w$]+\\.)+[<\\w$>]+)\\(([\\w-$]+\\.java:\\d+)\\)");
262262

263263
private static OutputEvent convertToOutputEvent(String message, Category category, IDebugAdapterContext context) {
264264
Matcher matcher = STACKTRACE_PATTERN.matcher(message);

com.microsoft.java.debug.plugin/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<command id="vscode.java.resolveJavaExecutable"/>
2020
<command id="vscode.java.fetchPlatformSettings"/>
2121
<command id="vscode.java.resolveClassFilters"/>
22+
<command id="vscode.java.resolveSourceUri"/>
2223
</delegateCommandHandler>
2324
</extension>
2425
</plugin>

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler
4646
public static final String RESOLVE_JAVA_EXECUTABLE = "vscode.java.resolveJavaExecutable";
4747
public static final String FETCH_PLATFORM_SETTINGS = "vscode.java.fetchPlatformSettings";
4848
public static final String RESOLVE_CLASSFILTERS = "vscode.java.resolveClassFilters";
49+
public static final String RESOLVE_SOURCEURI = "vscode.java.resolveSourceUri";
4950

5051
@Override
5152
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
@@ -87,6 +88,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
8788
return PlatformSettings.getPlatformSettings();
8889
case RESOLVE_CLASSFILTERS:
8990
return JavaClassFilter.resolveClassFilters(arguments);
91+
case RESOLVE_SOURCEURI:
92+
return ResolveSourceMappingHandler.resolveSourceUri(arguments);
9093
default:
9194
break;
9295
}

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JdtSourceLookUpProvider.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,7 @@ public String getSourceFileURI(String fullyQualifiedName, String sourcePath) {
178178
if (sourceElement instanceof IResource) {
179179
return getFileURI((IResource) sourceElement);
180180
} else if (sourceElement instanceof IClassFile) {
181-
try {
182-
IClassFile file = (IClassFile) sourceElement;
183-
if (file.getBuffer() != null) {
184-
return getFileURI(file);
185-
}
186-
} catch (JavaModelException e) {
187-
// do nothing.
188-
}
181+
return getFileURI((IClassFile) sourceElement);
189182
}
190183
return null;
191184
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2020 Microsoft Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Microsoft Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.microsoft.java.debug.plugin.internal;
13+
14+
import java.io.File;
15+
import java.util.List;
16+
import java.util.regex.Matcher;
17+
import java.util.regex.Pattern;
18+
19+
import org.apache.commons.lang3.StringUtils;
20+
21+
public class ResolveSourceMappingHandler {
22+
private static final Pattern SOURCE_PATTERN = Pattern.compile("([\\w$\\.]+\\/)?(([\\w$]+\\.)+[<\\w$>]+)\\(([\\w-$]+\\.java:\\d+)\\)");
23+
private static final JdtSourceLookUpProvider sourceProvider = new JdtSourceLookUpProvider();
24+
25+
public static String resolveSourceUri(List<Object> arguments) {
26+
if (arguments == null || arguments.isEmpty()) {
27+
return null;
28+
}
29+
30+
return resolveSourceUri((String) arguments.get(0));
31+
}
32+
33+
public static String resolveSourceUri(String lineText) {
34+
if (lineText == null) {
35+
return null;
36+
}
37+
38+
Matcher matcher = SOURCE_PATTERN.matcher(lineText);
39+
if (matcher.find()) {
40+
String methodField = matcher.group(2);
41+
String locationField = matcher.group(matcher.groupCount());
42+
String fullyQualifiedName = methodField.substring(0, methodField.lastIndexOf("."));
43+
String packageName = fullyQualifiedName.lastIndexOf(".") > -1 ? fullyQualifiedName.substring(0, fullyQualifiedName.lastIndexOf(".")) : "";
44+
String[] locations = locationField.split(":");
45+
String sourceName = locations[0];
46+
String sourcePath = StringUtils.isBlank(packageName) ? sourceName
47+
: packageName.replace('.', File.separatorChar) + File.separatorChar + sourceName;
48+
return sourceProvider.getSourceFileURI(fullyQualifiedName, sourcePath);
49+
}
50+
51+
return null;
52+
}
53+
}

0 commit comments

Comments
 (0)