Skip to content

Commit c2a33d3

Browse files
committed
Refactor Builder constructor for readability
There are now two constructors which clearly differentiate between creating a project for a File and creating a project for a Reader. This makes the calling code easier to read. To avoid code duplication, two small private methods were added to Builder.
1 parent 040383e commit c2a33d3

File tree

1 file changed

+90
-35
lines changed

1 file changed

+90
-35
lines changed

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

Lines changed: 90 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,21 @@ public Class<?> compile(String script) throws ScriptException {
182182

183183
final Writer writer = getContext().getErrorWriter();
184184
try {
185-
// script may be null, but then we cannot create a StringReader for it,
186-
// therefore null is passed if script is null.
187-
final Reader reader = (script == null) ? null : new StringReader(script);
188-
final Builder builder = new Builder(file, reader, writer);
185+
186+
final Builder builder;
187+
188+
if (file != null && file.exists()) {
189+
// if the filename set in engine scope bindings is valid,
190+
// ignore the given script and use that file instead.
191+
builder = new Builder(file, writer);
192+
}
193+
else {
194+
// script may be null, but then we cannot create a StringReader for it,
195+
// therefore null is passed if script is null.
196+
final Reader reader =
197+
(script == null) ? null : new StringReader(script);
198+
builder = new Builder(reader, writer);
199+
}
189200
final MavenProject project = builder.project;
190201
String mainClass = builder.mainClass;
191202

@@ -277,7 +288,7 @@ public void compile(final File file, final Writer errorWriter) {
277288
try {
278289
final Writer writer =
279290
(errorWriter == null) ? getContext().getErrorWriter() : errorWriter;
280-
final Builder builder = new Builder(file, null, writer);
291+
final Builder builder = new Builder(file, writer);
281292
try {
282293
builder.project.build();
283294
}
@@ -302,7 +313,7 @@ public void makeJar(final File file, final boolean includeSources,
302313
final File output, final Writer errorWriter)
303314
{
304315
try {
305-
final Builder builder = new Builder(file, null, errorWriter);
316+
final Builder builder = new Builder(file, errorWriter);
306317
try {
307318
builder.project.build(true, true, includeSources);
308319
final File target = builder.project.getTarget();
@@ -346,6 +357,7 @@ private void printOrThrow(Throwable t, Writer errorWriter) {
346357
* A wrapper around a (possibly only temporary) project.
347358
*
348359
* @author Johannes Schindelin
360+
* @author Jonathan Hale
349361
*/
350362
private class Builder {
351363

@@ -355,11 +367,11 @@ private class Builder {
355367
private MavenProject project;
356368

357369
/**
358-
* Constructs a wrapper around a possibly temporary project.
370+
* Constructs a wrapper around a possibly project for a source or maven
371+
* project file.
359372
*
360373
* @param file the {@code .java} file to build (or null, if {@code reader}
361374
* is set).
362-
* @param reader provides the Java source if {@code file} is {@code null}
363375
* @param errorWriter where to write the error output.
364376
* @throws ScriptException
365377
* @throws IOException
@@ -369,48 +381,91 @@ private class Builder {
369381
* @throws TransformerException
370382
* @throws TransformerFactoryConfigurationError
371383
*/
372-
private Builder(final File file, final Reader reader,
373-
final Writer errorWriter) throws ScriptException, IOException,
374-
ParserConfigurationException, SAXException,
375-
TransformerConfigurationException, TransformerException,
384+
private Builder(final File file, final Writer errorWriter)
385+
throws ScriptException, IOException, ParserConfigurationException,
386+
SAXException, TransformerConfigurationException, TransformerException,
376387
TransformerFactoryConfigurationError
377388
{
378-
if (errorWriter == null) {
379-
err = null;
380-
}
381-
else {
382-
err = new PrintStream(new LineOutputStream() {
389+
err = createErrorPrintStream(errorWriter);
383390

384-
@Override
385-
public void println(final String line) throws IOException {
386-
errorWriter.append(line).append('\n');
387-
}
391+
BuildEnvironment env = createBuildEnvironment();
388392

389-
});
393+
// will throw IOException if file does not exist.
394+
temporaryDirectory = null;
395+
if (file.getName().equals("pom.xml")) {
396+
project = env.parse(file, null);
390397
}
398+
else {
399+
mainClass = getFullClassName(file);
400+
project = getMavenProject(env, file, mainClass);
401+
}
402+
}
391403

392-
boolean verbose = "true".equals(get("verbose"));
393-
boolean debug = "true".equals(get("debug"));
394-
BuildEnvironment env = new BuildEnvironment(err, true, verbose, debug);
404+
/**
405+
* Constructs a wrapper around a possibly temporary project for source code
406+
* generated by a Reader.
407+
*
408+
* @param reader provides the Java source if {@code file} is {@code null}
409+
* @param errorWriter where to write the error output.
410+
* @throws ScriptException
411+
* @throws IOException
412+
* @throws ParserConfigurationException
413+
* @throws SAXException
414+
* @throws TransformerConfigurationException
415+
* @throws TransformerException
416+
* @throws TransformerFactoryConfigurationError
417+
*/
418+
private Builder(final Reader reader, final Writer errorWriter)
419+
throws ScriptException, IOException, ParserConfigurationException,
420+
SAXException, TransformerConfigurationException, TransformerException,
421+
TransformerFactoryConfigurationError
422+
{
423+
err = createErrorPrintStream(errorWriter);
424+
425+
BuildEnvironment env = createBuildEnvironment();
395426

396-
if (file == null || !file.exists()) try {
427+
try {
397428
project = writeTemporaryProject(env, reader);
398429
temporaryDirectory = project.getDirectory();
399430
mainClass = project.getMainClass();
400431
}
401432
catch (Exception e) {
402433
throw new ScriptException(e);
403434
}
404-
else {
405-
temporaryDirectory = null;
406-
if (file.getName().equals("pom.xml")) {
407-
project = env.parse(file, null);
408-
}
409-
else {
410-
mainClass = getFullClassName(file);
411-
project = getMavenProject(env, file, mainClass);
412-
}
435+
}
436+
437+
/**
438+
* Create a {@link PrintStream} from an error {@link Writer}.
439+
*
440+
* @param errorWriter the {@link Writer} to write errors to.
441+
* @return a {@link PrintStream} which writes to errorWriter.
442+
*/
443+
private PrintStream createErrorPrintStream(final Writer errorWriter) {
444+
if (errorWriter == null) {
445+
return null;
413446
}
447+
448+
// create a PrintStream which redirects output to errorWriter
449+
return new PrintStream(new LineOutputStream() {
450+
451+
@Override
452+
public void println(final String line) throws IOException {
453+
errorWriter.append(line).append('\n');
454+
}
455+
456+
});
457+
}
458+
459+
/**
460+
* Create a {@link BuildEnvironment} for current engine scope bindings
461+
* context.
462+
*
463+
* @return the created {@link BuildEnvironment}.
464+
*/
465+
private BuildEnvironment createBuildEnvironment() {
466+
boolean verbose = "true".equals(get("verbose"));
467+
boolean debug = "true".equals(get("debug"));
468+
return new BuildEnvironment(err, true, verbose, debug);
414469
}
415470

416471
/**

0 commit comments

Comments
 (0)