Skip to content

Commit ed9cb47

Browse files
committed
Move ScriptModule#getLanguage() into ScriptInfo
For a given script, which language to use is a constant. It should be part of the script _metadata_, not the script module _instance_.
1 parent 15e232d commit ed9cb47

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111

1212
<artifactId>scijava-common</artifactId>
13-
<version>2.63.1-SNAPSHOT</version>
13+
<version>2.64.0-SNAPSHOT</version>
1414

1515
<name>SciJava Common</name>
1616
<description>SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by downstream projects in the SciJava ecosystem, such as ImageJ and SCIFIO.</description>

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ public class ScriptInfo extends AbstractModuleInfo implements Contextual {
101101
/** True iff the return value should be appended as an output. */
102102
private boolean appendReturnValue;
103103

104+
/** Script language in which the script should be executed. */
105+
private ScriptLanguage scriptLanguage;
106+
104107
/**
105108
* Creates a script metadata object which describes the given script file.
106109
*
@@ -224,6 +227,22 @@ public BufferedReader getReader() {
224227
return new BufferedReader(new StringReader(script), PARAM_CHAR_MAX);
225228
}
226229

230+
/** Gets the scripting language of the script. */
231+
public ScriptLanguage getLanguage() {
232+
if (scriptLanguage == null) {
233+
// infer the language from the script path's extension
234+
final String scriptPath = getPath();
235+
final String extension = FileUtils.getExtension(scriptPath);
236+
scriptLanguage = scriptService.getLanguageByExtension(extension);
237+
}
238+
return scriptLanguage;
239+
}
240+
241+
/** Overrides the script language to use when executing the script. */
242+
public void setLanguage(final ScriptLanguage scriptLanguage) {
243+
this.scriptLanguage = scriptLanguage;
244+
}
245+
227246
/**
228247
* Parses the script's input and output parameters from the script header.
229248
* <p>

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.scijava.module.Module;
5151
import org.scijava.module.ModuleItem;
5252
import org.scijava.plugin.Parameter;
53-
import org.scijava.util.FileUtils;
5453

5554
/**
5655
* A {@link Module} which executes a script.
@@ -76,9 +75,6 @@ public class ScriptModule extends AbstractModule implements Contextual {
7675
@Parameter
7776
private LogService log;
7877

79-
/** Script language in which the script should be executed. */
80-
private ScriptLanguage scriptLanguage;
81-
8278
/** Script engine with which the script should be executed. */
8379
private ScriptEngine scriptEngine;
8480

@@ -96,22 +92,6 @@ public ScriptModule(final ScriptInfo info) {
9692

9793
// -- ScriptModule methods --
9894

99-
/** Gets the scripting language of the script. */
100-
public ScriptLanguage getLanguage() {
101-
if (scriptLanguage == null) {
102-
// infer the language from the script path's extension
103-
final String path = getInfo().getPath();
104-
final String extension = FileUtils.getExtension(path);
105-
scriptLanguage = scriptService.getLanguageByExtension(extension);
106-
}
107-
return scriptLanguage;
108-
}
109-
110-
/** Overrides the script language to use when executing the script. */
111-
public void setLanguage(final ScriptLanguage scriptLanguage) {
112-
this.scriptLanguage = scriptLanguage;
113-
}
114-
11595
/** Sets the writer used to record the standard output stream. */
11696
public void setOutputWriter(final Writer output) {
11797
this.output = output;
@@ -125,7 +105,7 @@ public void setErrorWriter(final Writer error) {
125105
/** Gets the script engine used to execute the script. */
126106
public ScriptEngine getEngine() {
127107
if (scriptEngine == null) {
128-
scriptEngine = getLanguage().getScriptEngine();
108+
scriptEngine = getInfo().getLanguage().getScriptEngine();
129109
}
130110
return scriptEngine;
131111
}
@@ -185,7 +165,7 @@ public void run() {
185165
}
186166

187167
// populate output values
188-
final ScriptLanguage language = getLanguage();
168+
final ScriptLanguage language = getInfo().getLanguage();
189169
for (final ModuleItem<?> item : getInfo().outputs()) {
190170
final String name = item.getName();
191171
final Object value;
@@ -230,4 +210,17 @@ public void setContext(final Context context) {
230210
context.inject(this);
231211
}
232212

213+
// -- Deprecated methods --
214+
215+
/** @deprecated Use {@link ScriptInfo#getLanguage()} instead. */
216+
@Deprecated
217+
public ScriptLanguage getLanguage() {
218+
return getInfo().getLanguage();
219+
}
220+
221+
/** @deprecated Use {@link ScriptInfo#setLanguage(ScriptLanguage)} instead. */
222+
@Deprecated
223+
public void setLanguage(final ScriptLanguage scriptLanguage) {
224+
getInfo().setLanguage(scriptLanguage);
225+
}
233226
}

0 commit comments

Comments
 (0)