Skip to content

Commit 52b6d50

Browse files
committed
JavaEngine: always to try clean up the mess
Even if the error occurred during the builder initialization, rather than the build step, let's make sure to try calling cleanup() afterward. This change makes the code simpler, too, since we no longer need nested try/catch/finally blocks. This is an attempted bug-fix for Fiji BugZilla issue #906: http://fiji.sc/bugzilla/show_bug.cgi?id=906 This patch looks best with "git show -b" to ignore whitespace changes.
1 parent e93616a commit 52b6d50

File tree

1 file changed

+29
-35
lines changed

1 file changed

+29
-35
lines changed

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

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -205,31 +205,26 @@ public Class<?> compile(String script) throws ScriptException {
205205
final MavenProject project = builder.project;
206206
String mainClass = builder.mainClass;
207207

208-
try {
209-
project.build(true);
208+
project.build(true);
209+
if (mainClass == null) {
210+
mainClass = project.getMainClass();
210211
if (mainClass == null) {
211-
mainClass = project.getMainClass();
212-
if (mainClass == null) {
213-
throw new ScriptException("No main class found for file " + file);
214-
}
212+
throw new ScriptException("No main class found for file " + file);
215213
}
214+
}
216215

217-
// make class loader
218-
String[] paths = project.getClassPath(false).split(File.pathSeparator);
219-
URL[] urls = new URL[paths.length];
220-
for (int i = 0; i < urls.length; i++)
221-
urls[i] =
222-
new URL("file:" + paths[i] + (paths[i].endsWith(".jar") ? "" : "/"));
216+
// make class loader
217+
String[] paths = project.getClassPath(false).split(File.pathSeparator);
218+
URL[] urls = new URL[paths.length];
219+
for (int i = 0; i < urls.length; i++)
220+
urls[i] =
221+
new URL("file:" + paths[i] + (paths[i].endsWith(".jar") ? "" : "/"));
223222

224-
final URLClassLoader classLoader = new URLClassLoader(urls, Thread.currentThread()
225-
.getContextClassLoader());
223+
final URLClassLoader classLoader = new URLClassLoader(urls, Thread.currentThread()
224+
.getContextClassLoader());
226225

227-
// load main class
228-
return classLoader.loadClass(mainClass);
229-
}
230-
finally {
231-
builder.cleanup();
232-
}
226+
// load main class
227+
return classLoader.loadClass(mainClass);
233228
}
234229
catch (Exception e) {
235230
if (writer != null) {
@@ -242,6 +237,9 @@ public Class<?> compile(String script) throws ScriptException {
242237
throw new ScriptException(e);
243238
}
244239
}
240+
finally {
241+
builder.cleanup();
242+
}
245243
return null;
246244
}
247245

@@ -295,16 +293,14 @@ public void compile(final File file, final Writer errorWriter) {
295293
final Builder builder = new Builder();
296294
try {
297295
builder.initialize(file, writer);
298-
try {
299-
builder.project.build();
300-
}
301-
finally {
302-
builder.cleanup();
303-
}
296+
builder.project.build();
304297
}
305298
catch (Throwable t) {
306299
printOrThrow(t, errorWriter);
307300
}
301+
finally {
302+
builder.cleanup();
303+
}
308304
}
309305

310306
/**
@@ -321,20 +317,18 @@ public void makeJar(final File file, final boolean includeSources,
321317
final Builder builder = new Builder();
322318
try {
323319
builder.initialize(file, errorWriter);
324-
try {
325-
builder.project.build(true, true, includeSources);
326-
final File target = builder.project.getTarget();
327-
if (output != null && !target.equals(output)) {
328-
BuildEnvironment.copyFile(target, output);
329-
}
330-
}
331-
finally {
332-
builder.cleanup();
320+
builder.project.build(true, true, includeSources);
321+
final File target = builder.project.getTarget();
322+
if (output != null && !target.equals(output)) {
323+
BuildEnvironment.copyFile(target, output);
333324
}
334325
}
335326
catch (Throwable t) {
336327
printOrThrow(t, errorWriter);
337328
}
329+
finally {
330+
builder.cleanup();
331+
}
338332
}
339333

340334
/**

0 commit comments

Comments
 (0)