Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle any script's URI and provide script content when not readable from file. #3277

Merged
merged 1 commit into from Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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