Skip to content

Commit 3794804

Browse files
committed
clean: JavaCompiler merge Input & Options into single joint Builder class
1 parent 8ec7dcf commit 3794804

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

src/ch/vorburger/jvmtools/Bootstrap.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
package ch.vorburger.jvmtools;
22

3-
import ch.vorburger.main.StdIO;
3+
import static ch.vorburger.test.Assert.assertTrue;
4+
5+
import java.nio.file.Path;
46

57
public class Bootstrap {
68

79
public static void main(String[] args) throws Exception {
8-
var compiler = new JavaCompiler();
910

10-
// TODO Glob
11-
var sourcepath = new Sourcepath();
12-
sourcepath.addClasspathResource("ch/vorburger/jvmtools/JavaCompiler.java");
13-
sourcepath.addClasspathResource("ch/vorburger/jvmtools/JavaCompilerTest.java");
11+
// TODO Use a (TBD) JavaCompilerTask instead of JavaCompiler directly here...
1412

15-
var options =
16-
new JavaCompiler.Options.Builder()
13+
var input =
14+
new JavaCompiler.Input.Builder()
15+
// TODO Glob
16+
.sourceAdd(Path.of("src/ch/vorburger/jvmtools/JavaCompiler.java"))
17+
.sourceAdd(Path.of("src/ch/vorburger/jvmtools/JavaCompilerTest.java"))
1718
.outputDirectory(".build/bootstrap-classes")
1819
.build();
20+
assertTrue(new JavaCompiler().invoke(input));
1921

20-
compiler.invoke(
21-
new JavaCompiler.Input(StdIO.system(), sourcepath /* TODO, classpath */, options));
22+
// TODO Test loading and running compiled classes, in parallel!
2223

2324
// TODO JAR
2425
}

src/ch/vorburger/jvmtools/JavaCompiler.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,34 @@ public class JavaCompiler implements Service<JavaCompiler.Input, Boolean> {
1919
// dev.enola.be.jvm module that uses this JavaCompiler class. Note that they have different
2020
// lifecycles - this is a Singleton service, while Task is per-invocation.
2121

22-
// TODO Merge Input & Options into single joint record & class Builder?
23-
2422
public record Input(
25-
StdIO stdIO, Sourcepath sourcepath /*, Classpath classpath*/, Options options) {}
26-
27-
public static record Options(Path outputDirectory) {
28-
// TODO Support in-memory; see https://www.baeldung.com/java-string-compile-execute-code
23+
StdIO stdIO, Sourcepath sourcepath /*, Classpath classpath*/, Options options) {
2924

3025
public static class Builder {
26+
private StdIO stdIO = StdIO.system();
27+
private Sourcepath sourcepath = new Sourcepath();
3128
private Path outputDirectory;
3229

30+
public Builder stdIO(StdIO stdIO) {
31+
this.stdIO = stdIO;
32+
return this;
33+
}
34+
35+
// TODO Glob
36+
public Builder sourceAdd(Path path) {
37+
this.sourcepath.addPath(path);
38+
return this;
39+
}
40+
41+
public Sourcepath sourcepath() {
42+
return sourcepath;
43+
}
44+
45+
public Builder sourcepath(Sourcepath sourcepath) {
46+
this.sourcepath = sourcepath;
47+
return this;
48+
}
49+
3350
public Builder outputDirectory(Path outputDirectory) {
3451
this.outputDirectory = outputDirectory;
3552
return this;
@@ -39,12 +56,16 @@ public Builder outputDirectory(String outputDirectory) {
3956
return outputDirectory(Path.of(outputDirectory));
4057
}
4158

42-
public Options build() {
43-
return new Options(outputDirectory);
59+
public Input build() {
60+
var options = new Options(outputDirectory);
61+
return new Input(stdIO, sourcepath, options);
4462
}
4563
}
4664
}
4765

66+
// TODO Support in-memory output; see https://www.baeldung.com/java-string-compile-execute-code
67+
public static record Options(Path outputDirectory) {}
68+
4869
@Override
4970
public Boolean invoke(Input input) throws Exception {
5071
var err = input.stdIO().errWriter();

src/ch/vorburger/jvmtools/JavaCompilerTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
public class JavaCompilerTest {
99

1010
public static void main(String[] args) throws Exception {
11-
var compiler = new JavaCompiler();
1211

1312
var sourcepath = new Sourcepath();
1413
// sourcepath.addClasspathResource("ch/vorburger/jvmtools/Hello.java");
@@ -20,9 +19,14 @@ public static void main(String[] args) throws Exception {
2019
outputClassFile.delete();
2120
assertTrue(!outputClassFile.exists());
2221

23-
var options = new JavaCompiler.Options.Builder().outputDirectory(outputDirectory).build();
2422
var stdIO = ch.vorburger.main.StdIO.inMemory();
25-
assertTrue(compiler.invoke(new JavaCompiler.Input(stdIO, sourcepath, options)) == true);
23+
var input =
24+
new JavaCompiler.Input.Builder()
25+
.stdIO(stdIO)
26+
.sourcepath(sourcepath)
27+
.outputDirectory(outputDirectory)
28+
.build();
29+
assertTrue(new JavaCompiler().invoke(input) == true);
2630
stdIO.assertErrorEmpty();
2731

2832
assertExists(outputClassFile);

0 commit comments

Comments
 (0)