Skip to content

Commit

Permalink
Support uri in Source.path (#58)
Browse files Browse the repository at this point in the history
* Support uri in Source.path

Signed-off-by: Jinbo Wang <jinbwan@microsoft.com>

* improve the comments
  • Loading branch information
testforstephen authored Oct 12, 2017
1 parent e7bb8db commit 574254a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.microsoft.java.debug.core.adapter.Messages.Response;

public class AdapterUtils {
Expand Down Expand Up @@ -150,6 +152,19 @@ public static String toUri(String path) {
}
}

/**
* Check a string variable is an uri or not.
*/
public static boolean isUri(String uriString) {
try {
URI uri = new URI(uriString);
return StringUtils.isNotBlank(uri.getScheme());
} catch (URISyntaxException | IllegalArgumentException | FileSystemNotFoundException
| SecurityException e) {
return false;
}
}

/**
* Generate an error response with the given error message.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ public void handle(Command command, Arguments arguments, Response response, IDeb
String sourcePath = clientPath;
if (bpArguments.source.sourceReference != 0 && context.getSourceUri(bpArguments.source.sourceReference) != null) {
sourcePath = context.getSourceUri(bpArguments.source.sourceReference);
} else {
sourcePath = AdapterUtils.convertPath(clientPath, context.isClientPathsAreUri(), context.isDebuggerPathsAreUri());
} else if (StringUtils.isNotBlank(clientPath)) {
// See the bug https://github.com/Microsoft/vscode/issues/30996
// Source.path in the SetBreakpointArguments could be a file system path or uri.
sourcePath = AdapterUtils.convertPath(clientPath, AdapterUtils.isUri(clientPath), context.isDebuggerPathsAreUri());
}

// When breakpoint source path is null or an invalid file path, send an ErrorResponse back.
if (sourcePath == null) {
if (StringUtils.isBlank(sourcePath)) {
AdapterUtils.setErrorResponse(response, ErrorCode.SET_BREAKPOINT_FAILURE,
String.format("Failed to setBreakpoint. Reason: '%s' is an invalid path.", bpArguments.source.path));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,17 @@ private Types.Source convertDebuggerSourceToClient(Location location, IDebugAdap
});

if (!StringUtils.isBlank(uri)) {
String clientPath = AdapterUtils.convertPath(uri, context.isDebuggerPathsAreUri(), context.isClientPathsAreUri());
// The Source.path could be a file system path or uri string.
if (uri.startsWith("file:")) {
String clientPath = AdapterUtils.convertPath(uri, context.isDebuggerPathsAreUri(), context.isClientPathsAreUri());
return new Types.Source(sourceName, clientPath, 0);
} else {
return new Types.Source(sourceName, clientPath, context.createSourceReference(uri));
// If the debugger returns uri in the Source.path for the StackTrace response, VSCode client will try to find a TextDocumentContentProvider
// to render the contents.
// Language Support for Java by Red Hat extension has already registered a jdt TextDocumentContentProvider to parse the jdt-based uri.
// The jdt uri looks like 'jdt://contents/rt.jar/java.io/PrintStream.class?=1.helloworld/%5C/usr%5C/lib%5C/jvm%5C/java-8-oracle%5C/jre%5C/
// lib%5C/rt.jar%3Cjava.io(PrintStream.class'.
return new Types.Source(sourceName, uri, 0);
}
} else {
// If the source lookup engine cannot find the source file, then lookup it in the source directories specified by user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
Expand All @@ -52,7 +51,6 @@
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.SearchRequestor;
import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
import org.eclipse.jdt.internal.debug.core.breakpoints.ValidBreakpointLocationLocator;

import com.microsoft.java.debug.core.Configuration;
Expand All @@ -65,9 +63,6 @@ public class JdtSourceLookUpProvider implements ISourceLookUpProvider {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private static final String JDT_SCHEME = "jdt";
private static final String PATH_SEPARATOR = "/";
private static final String MISSING_SOURCES_HEADER = " // Failed to get sources. Instead, stub sources have been generated.\n"
+ " // Implementation of methods is unavailable.\n";
private static final String LF = "\n";

private HashMap<String, Object> context = new HashMap<String, Object>();

Expand Down Expand Up @@ -204,9 +199,6 @@ private String getContents(IClassFile cf) {
if (buffer != null) {
source = buffer.getContents();
}
if (source == null) {
source = disassemble(cf);
}
} catch (JavaModelException e) {
logger.log(Level.SEVERE, String.format("Failed to parse the source contents of the class file: %s", e.toString()), e);
}
Expand Down Expand Up @@ -282,7 +274,6 @@ private static String getFileURI(IClassFile classFile) {
+ PATH_SEPARATOR + classFile.getElementName(), classFile.getHandleIdentifier(), null)
.toASCIIString();
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
}
Expand Down Expand Up @@ -315,19 +306,6 @@ private static IClassFile resolveClassFile(String uriString) {
return null;
}

private static String disassemble(IClassFile classFile) {
ClassFileBytesDisassembler disassembler = ToolFactory.createDefaultClassFileBytesDisassembler();
String disassembledByteCode = null;
try {
disassembledByteCode = disassembler.disassemble(classFile.getBytes(), LF,
ClassFileBytesDisassembler.WORKING_COPY);
disassembledByteCode = MISSING_SOURCES_HEADER + LF + disassembledByteCode;
} catch (Exception e) {
// ignore
}
return disassembledByteCode;
}

private static IJavaSearchScope createSearchScope(IJavaProject project) {
if (project == null) {
return SearchEngine.createWorkspaceScope();
Expand Down

0 comments on commit 574254a

Please sign in to comment.