Interface JctCompilerConfigurer<E extends Exception>
- Type Parameters:
E
- the exception that may be thrown by the configurer.
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
This can allow encapsulating common configuration logic across tests into a single place.
Implementations of this interface should declare the exception type that the implementation
can throw when invoked. If this is not a checked exception, or no case exists where an exception
could be thrown, then this can be set to RuntimeException
.
The following demonstrates an example usage of this interface. The implementation configures a specific annotation processor and sets an annotation processor flag.
class MyAnnotationProcessorConfigurer implements JctCompilerConfigurer<RuntimeException> {
@Override
public void configure(JctCompiler compiler) {
compiler
.addAnnotationProcessors(new MyAnnotationProcessor())
.addAnnotationProcessorOptions("MyAnnotationProcessor.debug=true");
}
}
...tests can then make use of this configurer directly:
@Test
void theCompilationSucceedsAsExpected() {
try (var workspace = Workspaces.newWorkspace()) {
// Given
...
var compiler = JctCompilers
.newPlatformCompiler()
.release(17)
.configure(new MyAnnotationProcessorConfigurer());
// When
var compilation = compiler.compile(workspace);
// Then
...
}
}
Since this is a functional interface, configurers can be lambda expressions, anonymous objects, or method references.
compiler
.configure(c -> c.release(11))
.configure(this::configureFailures);
The JUnit support allows for specifying these configurers in an annotation instead. This will apply the configurer before passing it to the test as a parameter:
@JavacCompilersTest(configurers = {MyAnnotationProcessorConfigurer.class})
void theCompilationSucceedsAsExpected(JctCompiler compiler) {
// ...
}
Note that in this case, the configurer must be an outer class or a static nested class rather than a Lambda expression, anonymous class, class instance, or nested class. It must also have a single public no-arguments constructor in order to be accessible.
- Since:
- 0.0.1
- Author:
- Ashley Scopes
-
Method Summary
Modifier and TypeMethodDescriptionvoid
configure
(JctCompiler compiler) Apply configuration logic to the given compiler.
-
Method Details
-
configure
Apply configuration logic to the given compiler.- Parameters:
compiler
- the compiler.- Throws:
E
- any exception that may be thrown by the configurer.
-