Skip to content

Commit 4913afd

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 <[email protected]>
1 parent 510a488 commit 4913afd

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,14 @@ 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 {
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 Reader reader = (script == null) ? null : new StringReader(script);
186179
final Builder builder = new Builder(file, reader, writer);
187180
final MavenProject project = builder.project;
188181
String mainClass = builder.mainClass;
@@ -229,6 +222,23 @@ public Class<?> compile(Reader reader) throws ScriptException {
229222
return null;
230223
}
231224

225+
/**
226+
* Compiles and runs the specified {@code .java} class.
227+
*
228+
* @param reader the reader producing the source code for a Java class
229+
* @return the compiled Java class as {@link Class}.
230+
*/
231+
public Class<?> compile(Reader reader) throws ScriptException {
232+
String script;
233+
try {
234+
script = getReaderContentsAsString(reader);
235+
}
236+
catch (IOException e) {
237+
throw new ScriptException(e);
238+
}
239+
return compile(script);
240+
}
241+
232242
/**
233243
* Compiles the specified {@code .java} file.
234244
*

0 commit comments

Comments
 (0)