Skip to content

Commit 74704bc

Browse files
authored
Merge pull request #689 from ascopes/feature/functional-workspace-api
Implement a functional API for closing workspaces
2 parents 38ed82b + 05e5eb1 commit 74704bc

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

java-compiler-testing/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>io.github.ascopes.jct</groupId>
2424
<artifactId>java-compiler-testing-parent</artifactId>
25-
<version>3.1.1-SNAPSHOT</version>
25+
<version>3.2.0-SNAPSHOT</version>
2626
<relativePath>../pom.xml</relativePath>
2727
</parent>
2828

java-compiler-testing/src/it/lombok/src/test/java/io/github/ascopes/jct/acceptancetests/lombok/LombokTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class LombokTest {
3737
@DisplayName("Lombok @Data compiles the expected data class")
3838
@JavacCompilerTest
3939
void lombokDataCompilesTheExpectedDataClass(JctCompiler compiler) throws Throwable {
40-
try (var workspace = Workspaces.newWorkspace()) {
40+
Workspaces.newWorkspace().use(workspace -> {
4141
// Given
4242
workspace
4343
.createSourcePathPackage()
@@ -63,13 +63,13 @@ void lombokDataCompilesTheExpectedDataClass(JctCompiler compiler) throws Throwab
6363
softly.assertThat(animal).hasFieldOrPropertyWithValue("legCount", 4);
6464
softly.assertThat(animal).hasFieldOrPropertyWithValue("age", 5);
6565
});
66-
}
66+
});
6767
}
6868

6969
@DisplayName("Lombok @Data compiles the expected data class with module support")
7070
@JavacCompilerTest(minVersion = 9)
7171
void lombokDataCompilesTheExpectedDataClassJpms(JctCompiler compiler) throws Throwable {
72-
try (var workspace = Workspaces.newWorkspace()) {
72+
Workspaces.newWorkspace().use(workspace -> {
7373
// Given
7474
workspace
7575
.createSourcePathPackage()
@@ -95,6 +95,6 @@ void lombokDataCompilesTheExpectedDataClassJpms(JctCompiler compiler) throws Thr
9595
softly.assertThat(animal).hasFieldOrPropertyWithValue("legCount", 4);
9696
softly.assertThat(animal).hasFieldOrPropertyWithValue("age", 5);
9797
});
98-
}
98+
});
9999
}
100100
}

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/Workspace.java

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,24 @@
7272
* <p>A simple example of usage of this interface would be the following:
7373
*
7474
* <pre><code>
75-
* try (Workspace workspace = Workspaces.newWorkspace(PathStrategy.TEMP_DIRECTORIES)) {
76-
* workspace
77-
* .createSourcePathPackage()
78-
* .copyContentsFrom("src", "test", "resources", "test-data");
75+
* try (Workspace workspace = Workspaces.newWorkspace()) {
76+
* workspace
77+
* .createSourcePathPackage()
78+
* .copyContentsFrom("src", "test", "resources", "test-data");
7979
*
80-
* var compilation = someCompiler.compile(workspace);
80+
* var compilation = someCompiler.compile(workspace);
8181
*
82-
* assertThat(compilation).isSuccessful();
83-
* }
82+
* assertThat(compilation).isSuccessful();
83+
* }
84+
* </code></pre>
85+
*
86+
* <p>As of 3.2.0, you can use a functional version of the above instead if this is more
87+
* suitable for your use-case:
88+
*
89+
* <pre><code>
90+
* Workspaces.newWorkspace().use(workspace -> {
91+
* ...
92+
* });
8493
* </code></pre>
8594
*
8695
* <p>Remember that files that are created as the result of a compilation can be queried via
@@ -834,4 +843,52 @@ default List<? extends PathRoot> getModulePathModule(String moduleName) {
834843
default Map<String, List<? extends PathRoot>> getModulePathModules() {
835844
return getModules(StandardLocation.MODULE_PATH);
836845
}
846+
847+
///
848+
/// Functional APIs.
849+
///
850+
851+
/**
852+
* Functional equivalent of consuming this object with a try-with-resources.
853+
*
854+
* <p>This workspace will be {@link #close() closed} upon completion of this method or upon
855+
* an exception being raised.
856+
*
857+
* @param consumer the consumer to pass this object to.
858+
* @param <T> the exception that the consumer can throw, or {@link RuntimeException}
859+
* if no checked exception is thrown.
860+
* @throws UncheckedIOException if the closure fails after the consumer is called.
861+
* @throws T the checked exception that the consumer can throw.
862+
* @since 3.2.0
863+
*/
864+
@API(since = "3.2.0", status = Status.STABLE)
865+
default <T extends Throwable> void use(ThrowingWorkspaceConsumer<T> consumer) throws T {
866+
try {
867+
consumer.accept(this);
868+
} finally {
869+
close();
870+
}
871+
}
872+
873+
/**
874+
* A consumer functional interface that consumes a workspace and
875+
* can throw a checked exception.
876+
*
877+
* @param <T> the exception type that can be thrown, or {@link RuntimeException}
878+
* if no checked exception is thrown.
879+
* @author Ashley Scopes
880+
* @since 3.2.0
881+
*/
882+
@API(since = "3.2.0", status = Status.STABLE)
883+
public interface ThrowingWorkspaceConsumer<T extends Throwable> {
884+
885+
/**
886+
* Consume a workspace.
887+
*
888+
* @param workspace the workspace.
889+
* @throws T the checked exception that can be thrown.
890+
*/
891+
void accept(Workspace workspace) throws T;
892+
}
837893
}
894+

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<groupId>io.github.ascopes.jct</groupId>
2323
<artifactId>java-compiler-testing-parent</artifactId>
24-
<version>3.1.1-SNAPSHOT</version>
24+
<version>3.2.0-SNAPSHOT</version>
2525
<packaging>pom</packaging>
2626

2727
<name>Java Compiler Testing parent project</name>

0 commit comments

Comments
 (0)