Skip to content

Commit e663151

Browse files
committed
Swap execution order of compile(String) and compile(Reader)
This allows us to compute a hash on the script as a String even if it was input via the gererating Reader. We need to compute the hash for future recompilation avoidance. Signed-off-by: Squareys <Squareys@googlemail.com>
1 parent b1362ea commit e663151

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/main/java/org/scijava/plugins/scripting/java/JavaEngine.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,16 @@ public Object eval(Reader reader) throws ScriptException {
168168
* @return the compiled Java class as {@link Class}.
169169
*/
170170
public Class<?> compile(String script) throws ScriptException {
171-
return compile(new StringReader(script));
172-
}
173-
174-
/**
175-
* Compiles and runs the specified {@code .java} class.
176-
*
177-
* @param reader the reader producing the source code for a Java class
178-
* @returns the compiled Java class as {@link Class}.
179-
*/
180-
public Class<?> compile(Reader reader) throws ScriptException {
181171
final String path = (String)get(FILENAME);
182172
File file = path == null ? null : new File(path);
183173

184174
final Writer writer = getContext().getErrorWriter();
185175
try {
186-
final Builder builder = new Builder(file, reader, writer);
176+
// script may be null, but then we cannot create a StringReader for it,
177+
// therefore null is passed if script is null.
178+
final Builder builder =
179+
new Builder(file, (script == null) ? null : new StringReader(script),
180+
writer);
187181
final MavenProject project = builder.project;
188182
String mainClass = builder.mainClass;
189183

@@ -221,6 +215,23 @@ public Class<?> compile(Reader reader) throws ScriptException {
221215
return null;
222216
}
223217

218+
/**
219+
* Compiles and runs the specified {@code .java} class.
220+
*
221+
* @param reader the reader producing the source code for a Java class
222+
* @returns the compiled Java class as {@link Class}.
223+
*/
224+
public Class<?> compile(Reader reader) throws ScriptException {
225+
String script;
226+
try {
227+
script = getReaderContentsAsString(reader);
228+
}
229+
catch (IOException e) {
230+
throw new ScriptException(e);
231+
}
232+
return compile(script);
233+
}
234+
224235
/**
225236
* Compiles the specified {@code .java} file.
226237
*

0 commit comments

Comments
 (0)