Class AbstractCompilersProvider
- All Implemented Interfaces:
ArgumentsProvider
- Direct Known Subclasses:
JavacCompilersProvider
Each implementation is expected to provide:
- A method
initializeNewCompiler()
that returns new instances of aJctCompiler
; - A minimum acceptable language level for the compiler, as an integer;
- A maximum acceptable language level for the compiler, as an integer;
- An implementation of
AnnotationConsumer
that consumes the desired annotation. The details of the annotation should be extracted and a call toconfigure(int, int, java.lang.Class<? extends io.github.ascopes.jct.compilers.JctCompilerConfigurer<?>>[], io.github.ascopes.jct.junit.VersionStrategy)
should be made.
An example annotation would look like the following:
@ArgumentsSource(MyCompilersProvider.class)
@ParameterizedTest(name = "for {0}")
@Retention(RetentionPolicy.RUNTIME)
@Tag("java-compiler-testing-test")
@Target({
ElementType.ANNOTATION_TYPE,
ElementType.METHOD,
})
@TestTemplate
public @interface MyCompilerTest {
int minVersion() default Integer.MIN_VALUE;
int maxVersion() default Integer.MAX_VALUE;
Class<? extends JctSimpleCompilerConfigurer>[] configurers() default {};
VersionStrategy versionStrategy() default VersionStrategy.RELEASE;
}
...with the JUnit5 annotation provider being implemented as:
public final class MyCompilersProvider
extends AbstractCompilersProvider
implements AnnotationConsumer<MyCompilerTest> {
@Override
protected JctCompiler initializeNewCompiler() {
return new MyCompilerImpl();
}
@Override
protected int minSupportedVersion() {
return 11; // Support Java 11 as the minimum.
}
@Override
protected int maxSupportedVersion() {
return 19; // Support Java 19 as the maximum.
}
@Override
public void accept(MyCompilerTest annotation) {
super.configure(
annotation.minVersion(),
annotation.maxVersion(),
annotation.configurers(),
annotation.versionStrategy(),
);
}
}
This would enable you to define your test cases like so:
@MyCompilerTest(minVersion=13, maxVersion=17)
void testSomething(JctCompiler compiler) {
...
}
@MyCompilerTest(configurers=WerrorConfigurer.class)
void testSomethingElse(JctCompiler compiler) {
...
}
static class WerrorConfigurer implements JctCompilerConfigurer {
@Override
public void configure(JctCompiler compiler) {
compiler.failOnErrors(true);
}
}
Note that if you are running your tests within a JPMS module, you will need
to ensure that you declare your module to be open
to io.github.ascopes.jct
,
otherwise this component will be unable to discover the constructor to initialise your configurer
correctly, and may raise an exception as a result.
- Since:
- 0.0.1
- Author:
- Ashley Scopes
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected final void
configure
(int min, int max, Class<? extends JctCompilerConfigurer<?>>[] configurerClasses, VersionStrategy versionStrategy) Configure this provider with parameters from annotations.protected abstract JctCompiler
Initialise a new compiler.protected abstract int
Get the maximum supported compiler version.protected abstract int
Get the minimum supported compiler version.provideArguments
(ExtensionContext context)
-
Constructor Details
-
AbstractCompilersProvider
protected AbstractCompilersProvider()Initialise this provider.
-
-
Method Details
-
provideArguments
- Specified by:
provideArguments
in interfaceArgumentsProvider
-
configure
protected final void configure(int min, int max, Class<? extends JctCompilerConfigurer<?>>[] configurerClasses, VersionStrategy versionStrategy) Configure this provider with parameters from annotations.This is expected to be called from an implementation of
AnnotationConsumer
.The minimum compiler version will be set to the
min
parameter, orminSupportedVersion()
, whichever is greater. This means annotations can passInteger.MIN_VALUE
as a default value safely.The maximum compiler version will be set to the
max
parameter, ormaxSupportedVersion()
, whichever is smaller. This means annotations can passInteger.MAX_VALUE
as a default value safely.If implementations do not support specifying custom compiler configurers, then an empty array must be passed for the
configurerClasses
parameter.If implementations do not support changing the version strategy, then it is suggested to pass
VersionStrategy.RELEASE
as the value for theversionStrategy
parameter.- Parameters:
min
- the inclusive minimum compiler version to use.max
- the inclusive maximum compiler version to use.configurerClasses
- the configurer classes to apply to each compiler.versionStrategy
- the version strategy to use.
-
initializeNewCompiler
Initialise a new compiler.- Returns:
- the compiler object.
-
minSupportedVersion
Get the minimum supported compiler version.- Returns:
- the minimum supported compiler version.
- Since:
- 1.0.0
-
maxSupportedVersion
Get the maximum supported compiler version.- Returns:
- the minimum supported compiler version.
- Since:
- 1.0.0
-