Skip to content

Commit 040383e

Browse files
committed
Add compile(File) method
compile(File, Writer) now uses the context error writer if the specified writer is null. compile(file) calls compile(File, null), therefore compiles a given file and writes errors to the context error writer. This maintains the consistency througout the compile(..) and eval(..) methods.
1 parent 1f8ec69 commit 040383e

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ public Class<?> compile(String script) throws ScriptException {
199199
}
200200

201201
// make class loader
202-
String[] paths = project.getClassPath(false).split(
203-
File.pathSeparator);
202+
String[] paths = project.getClassPath(false).split(File.pathSeparator);
204203
URL[] urls = new URL[paths.length];
205204
for (int i = 0; i < urls.length; i++)
206-
urls[i] = new URL("file:" + paths[i]
207-
+ (paths[i].endsWith(".jar") ? "" : "/"));
208-
URLClassLoader classLoader = new URLClassLoader(urls,
209-
Thread.currentThread().getContextClassLoader());
205+
urls[i] =
206+
new URL("file:" + paths[i] + (paths[i].endsWith(".jar") ? "" : "/"));
207+
URLClassLoader classLoader =
208+
new URLClassLoader(urls, Thread.currentThread()
209+
.getContextClassLoader());
210210

211211
// needed for annotation processing
212212
Thread.currentThread().setContextClassLoader(classLoader);
@@ -250,20 +250,42 @@ public Class<?> compile(Reader reader) throws ScriptException {
250250
}
251251

252252
/**
253-
* Compiles the specified {@code .java} file.
253+
* Compiles the specified {@code .java} file. Errors are written to the
254+
* context error writer.
254255
*
255256
* @param file the source code
256-
* @param errorWriter where to write the errors
257+
* @see #compile(File, Writer)
258+
* @see #compile(Reader)
259+
* @see #compile(String)
260+
*/
261+
public void compile(final File file) {
262+
compile(file, null);
263+
}
264+
265+
/**
266+
* Compiles the specified {@code .java} file. Errors are written to the
267+
* specified errorWriter or if it is null, to the context error writer.
268+
*
269+
* @param file the source code
270+
* @param errorWriter where to write the errors or null to use context
271+
* errorWriter
272+
* @see #compile(File)
273+
* @see #compile(Reader)
274+
* @see #compile(String)
257275
*/
258276
public void compile(final File file, final Writer errorWriter) {
259277
try {
260-
final Builder builder = new Builder(file, null, errorWriter);
278+
final Writer writer =
279+
(errorWriter == null) ? getContext().getErrorWriter() : errorWriter;
280+
final Builder builder = new Builder(file, null, writer);
261281
try {
262282
builder.project.build();
263-
} finally {
283+
}
284+
finally {
264285
builder.cleanup();
265286
}
266-
} catch (Throwable t) {
287+
}
288+
catch (Throwable t) {
267289
printOrThrow(t, errorWriter);
268290
}
269291
}

0 commit comments

Comments
 (0)