Skip to content

Commit e467c5d

Browse files
committed
ScriptInfo: add fallback code for null url & path
Now, the URL and (psuedo-)path can both be null. If that happens, the getURL() and getPath() methods will both return null. And the ScriptLanguage will be detected as the highest priority plugin available (typically Groovy, but depends on runtime classpath).
1 parent ed9cb47 commit e467c5d

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/main/java/org/scijava/script/ScriptInfo.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,16 @@ public ScriptLanguage getLanguage() {
232232
if (scriptLanguage == null) {
233233
// infer the language from the script path's extension
234234
final String scriptPath = getPath();
235-
final String extension = FileUtils.getExtension(scriptPath);
236-
scriptLanguage = scriptService.getLanguageByExtension(extension);
235+
if (scriptPath != null) {
236+
// use language associated with the script path extension
237+
final String extension = FileUtils.getExtension(scriptPath);
238+
scriptLanguage = scriptService.getLanguageByExtension(extension);
239+
}
240+
else {
241+
// use the highest priority language
242+
final List<ScriptLanguage> langs = scriptService.getLanguages();
243+
if (langs != null && !langs.isEmpty()) scriptLanguage = langs.get(0);
244+
}
237245
}
238246
return scriptLanguage;
239247
}
@@ -371,7 +379,7 @@ public void setContext(final Context context) {
371379

372380
@Override
373381
public String getIdentifier() {
374-
return "script:" + path;
382+
return "script:" + (path == null ? "<inline>" : path);
375383
}
376384

377385
// -- Locatable methods --
@@ -403,6 +411,7 @@ public String getVersion() {
403411

404412
private URL url(final URL u, final String p) {
405413
if (u != null) return u;
414+
if (p == null) return null;
406415
try {
407416
return new File(p).toURI().toURL();
408417
}
@@ -413,7 +422,8 @@ private URL url(final URL u, final String p) {
413422
}
414423

415424
private String path(final URL u, final String p) {
416-
return p == null ? u.getPath() : p;
425+
if (p != null) return p;
426+
return u == null ? null : u.getPath();
417427
}
418428

419429
private void parseParam(final String param) throws ScriptException {

0 commit comments

Comments
 (0)