Skip to content

Commit

Permalink
Handle any script's URI and provide script content when not readable …
Browse files Browse the repository at this point in the history
…from file.
  • Loading branch information
entlicher committed Nov 1, 2021
1 parent 76448e5 commit 4078eeb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
Expand Up @@ -21,6 +21,7 @@
import java.io.InvalidObjectException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.netbeans.api.debugger.jpda.JPDADebugger;
import org.netbeans.api.debugger.jpda.JPDAThread;
import org.netbeans.modules.debugger.jpda.truffle.access.CurrentPCInfo;
Expand Down Expand Up @@ -77,15 +78,18 @@ public URI getSourceURI() {
return null;
}
Source source = sourcePosition.getSource();
URI uri = source.getURI();
if (uri != null && "file".equalsIgnoreCase(uri.getScheme())) {
return uri;
}
try {
return source.getUrl().toURI();
} catch (URISyntaxException ex) {
return null;
URL url = source.getUrl();
URI uri;
if (url != null) {
try {
uri = url.toURI();
} catch (URISyntaxException ex) {
uri = source.getURI();
}
} else {
uri = source.getURI();
}
return uri;
}

@Override
Expand Down
Expand Up @@ -20,6 +20,7 @@
package org.netbeans.modules.debugger.jpda.truffle.source;

import com.sun.jdi.StringReference;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
Expand Down Expand Up @@ -64,7 +65,7 @@ private Source(JPDADebugger jpda, String name, String hostMethodName, String pat
this.codeRef = codeRef;
URL url = null;
if (hostMethodName == null) {
if (uri == null || !"file".equalsIgnoreCase(uri.getScheme())) {
if (uri == null || !"file".equalsIgnoreCase(uri.getScheme()) || !fileIsReadable(uri)) { // NOI18N
try {
url = SourceFilesCache.get(jpda).getSourceFile(name, hash, uri, getContent());
} catch (IOException ex) {
Expand All @@ -87,6 +88,14 @@ private Source(JPDADebugger jpda, String name, String hostMethodName, String pat
this.hash = hash;
}

private static boolean fileIsReadable(URI uri) {
try {
return new File(uri).canRead();
} catch (IllegalArgumentException | SecurityException ex) {
return false;
}
}

public static Source getExistingSource(JPDADebugger debugger, long id) {
synchronized (KNOWN_SOURCES) {
Map<Long, Source> dbgSources = KNOWN_SOURCES.get(debugger);
Expand Down
Expand Up @@ -23,6 +23,8 @@
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -325,15 +327,29 @@ public CompletableFuture<StackTraceResponse> stackTrace(StackTraceArguments args
sourceURI = URI.create(URITranslator.getDefault().uriToLSP(sourceURI.toString()));
Source source = new Source();
String scheme = sourceURI.getScheme();
if (null == scheme || scheme.isEmpty() || "file".equalsIgnoreCase(scheme)) {
source.setName(Paths.get(sourceURI).getFileName().toString());
source.setPath(sourceURI.getPath());
Path sourcePath = null;
if (null == scheme) {
sourcePath = Paths.get(sourceURI.getPath());
} else if ("file".equalsIgnoreCase(scheme)) { // NOI18N
try {
sourcePath = Paths.get(sourceURI);
} catch (FileSystemNotFoundException | SecurityException | IllegalArgumentException ex) {
sourcePath = null;
}
}
if (sourcePath != null) {
source.setName(sourcePath.getFileName().toString());
source.setPath(sourcePath.toString());
source.setSourceReference(0);
} else {
int ref = context.createSourceReference(sourceURI, frame.getSourceMimeType());
String path = sourceURI.getPath();
if (path == null) {
if (path == null || path.isEmpty()) {
path = sourceURI.getSchemeSpecificPart();
while (path.startsWith("//")) {
// Remove multiple initial slashes
path = path.substring(1);
}
}
if (path != null) {
int sepIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf(File.separatorChar));
Expand Down

0 comments on commit 4078eeb

Please sign in to comment.