Skip to content

Commit 9b77ad3

Browse files
committed
DefaultScriptInterpreter: improve constructors
The old constructor is deprecated. There is now a simple constructor taking only the ScriptLanguage, as well as one which allows to manually override the ScriptEngine. The SciJava application context is derived from the ScriptLanguage, and services (notably: the PrefService) are injected from there. The PrefService is now optional, and when it is null, there is no history available. (The code for null History was already in place everywhere -- it just couldn't happen previously due to the explicit "new History" call in the constructor. So now we make use of it.)
1 parent d85efbd commit 9b77ad3

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,50 @@ public class DefaultScriptInterpreter implements ScriptInterpreter {
4646
private final ScriptEngine engine;
4747
private final History history;
4848

49+
@Parameter(required = false)
50+
private PrefService prefs;
51+
4952
/**
50-
* Constructs a new {@link DefaultScriptInterpreter}.
51-
*
52-
* @param scriptService the script service
53-
* @param language the script language
53+
* @deprecated Use {@link #DefaultScriptInterpreter(ScriptLanguage)} instead.
5454
*/
55+
@Deprecated
56+
@SuppressWarnings("unused")
5557
public DefaultScriptInterpreter(final PrefService prefs,
5658
final ScriptService scriptService, final ScriptLanguage language)
5759
{
60+
this(language);
61+
}
62+
63+
/**
64+
* Creates a new script interpreter for the given script language.
65+
*
66+
* @param language {@link ScriptLanguage} of the interpreter
67+
*/
68+
public DefaultScriptInterpreter(final ScriptLanguage language) {
69+
this(language, null);
70+
}
71+
72+
/**
73+
* Creates a new script interpreter for the given script language, using the
74+
* specified script engine.
75+
*
76+
* @param language {@link ScriptLanguage} of the interpreter
77+
* @param engine {@link ScriptEngine} to use, or null for the specified
78+
* language's default engine
79+
*/
80+
public DefaultScriptInterpreter(final ScriptLanguage language,
81+
final ScriptEngine engine)
82+
{
83+
language.getContext().inject(this);
5884
this.language = language;
59-
engine = language.getScriptEngine();
60-
history = new History(prefs, engine.getClass().getName());
85+
this.engine = engine == null ? language.getScriptEngine() : engine;
86+
history = prefs == null ? null :
87+
new History(prefs, this.engine.getClass().getName());
6188
readHistory();
6289
}
6390

91+
// -- ScriptInterpreter methods --
92+
6493
@Override
6594
public synchronized void readHistory() {
6695
if (history == null) return;

0 commit comments

Comments
 (0)