|
|
|
|
@ -53,6 +53,8 @@ public final class TestCompiler {
@@ -53,6 +53,8 @@ public final class TestCompiler {
|
|
|
|
|
|
|
|
|
|
private final JavaCompiler compiler; |
|
|
|
|
|
|
|
|
|
private final Locale locale; |
|
|
|
|
|
|
|
|
|
private final SourceFiles sourceFiles; |
|
|
|
|
|
|
|
|
|
private final ResourceFiles resourceFiles; |
|
|
|
|
@ -64,12 +66,13 @@ public final class TestCompiler {
@@ -64,12 +66,13 @@ public final class TestCompiler {
|
|
|
|
|
private final List<String> compilerOptions; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TestCompiler(@Nullable ClassLoader classLoader, JavaCompiler compiler, |
|
|
|
|
SourceFiles sourceFiles, ResourceFiles resourceFiles, ClassFiles classFiles, |
|
|
|
|
List<Processor> processors, List<String> compilerOptions) { |
|
|
|
|
private TestCompiler(@Nullable ClassLoader classLoader, JavaCompiler compiler, Locale locale, |
|
|
|
|
SourceFiles sourceFiles, ResourceFiles resourceFiles, ClassFiles classFiles, List<Processor> processors, |
|
|
|
|
List<String> compilerOptions) { |
|
|
|
|
|
|
|
|
|
this.classLoader = classLoader; |
|
|
|
|
this.compiler = compiler; |
|
|
|
|
this.locale = locale; |
|
|
|
|
this.sourceFiles = sourceFiles; |
|
|
|
|
this.resourceFiles = resourceFiles; |
|
|
|
|
this.classFiles = classFiles; |
|
|
|
|
@ -92,8 +95,9 @@ public final class TestCompiler {
@@ -92,8 +95,9 @@ public final class TestCompiler {
|
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
*/ |
|
|
|
|
public static TestCompiler forCompiler(JavaCompiler javaCompiler) { |
|
|
|
|
return new TestCompiler(null, javaCompiler, SourceFiles.none(), |
|
|
|
|
ResourceFiles.none(), ClassFiles.none(), Collections.emptyList(), Collections.emptyList()); |
|
|
|
|
return new TestCompiler(null, javaCompiler, Locale.getDefault(), |
|
|
|
|
SourceFiles.none(), ResourceFiles.none(), |
|
|
|
|
ClassFiles.none(), Collections.emptyList(), Collections.emptyList()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -105,13 +109,26 @@ public final class TestCompiler {
@@ -105,13 +109,26 @@ public final class TestCompiler {
|
|
|
|
|
return customizer.apply(this); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Create a new {@code TestCompiler} instance that uses the specified {@link Locale} |
|
|
|
|
* to render compiler messages. |
|
|
|
|
* @param locale the locale to use |
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
* @since 6.1 |
|
|
|
|
*/ |
|
|
|
|
public TestCompiler withLocale(Locale locale) { |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, locale, |
|
|
|
|
this.sourceFiles, this.resourceFiles, |
|
|
|
|
this.classFiles, this.processors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Create a new {@code TestCompiler} instance with additional source files. |
|
|
|
|
* @param sourceFiles the additional source files |
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
*/ |
|
|
|
|
public TestCompiler withSources(SourceFile... sourceFiles) { |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles.and(sourceFiles), this.resourceFiles, |
|
|
|
|
this.classFiles, this.processors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
@ -122,7 +139,7 @@ public final class TestCompiler {
@@ -122,7 +139,7 @@ public final class TestCompiler {
|
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
*/ |
|
|
|
|
public TestCompiler withSources(Iterable<SourceFile> sourceFiles) { |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles.and(sourceFiles), this.resourceFiles, |
|
|
|
|
this.classFiles, this.processors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
@ -133,7 +150,7 @@ public final class TestCompiler {
@@ -133,7 +150,7 @@ public final class TestCompiler {
|
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
*/ |
|
|
|
|
public TestCompiler withSources(SourceFiles sourceFiles) { |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles.and(sourceFiles), this.resourceFiles, |
|
|
|
|
this.classFiles, this.processors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
@ -144,9 +161,9 @@ public final class TestCompiler {
@@ -144,9 +161,9 @@ public final class TestCompiler {
|
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
*/ |
|
|
|
|
public TestCompiler withResources(ResourceFile... resourceFiles) { |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles, |
|
|
|
|
this.resourceFiles.and(resourceFiles), this.classFiles, this.processors, |
|
|
|
|
this.compilerOptions); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles, this.resourceFiles.and(resourceFiles), |
|
|
|
|
this.classFiles, this.processors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -155,9 +172,9 @@ public final class TestCompiler {
@@ -155,9 +172,9 @@ public final class TestCompiler {
|
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
*/ |
|
|
|
|
public TestCompiler withResources(Iterable<ResourceFile> resourceFiles) { |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles, |
|
|
|
|
this.resourceFiles.and(resourceFiles), this.classFiles, this.processors, |
|
|
|
|
this.compilerOptions); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles, this.resourceFiles.and(resourceFiles), |
|
|
|
|
this.classFiles, this.processors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -166,9 +183,9 @@ public final class TestCompiler {
@@ -166,9 +183,9 @@ public final class TestCompiler {
|
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
*/ |
|
|
|
|
public TestCompiler withResources(ResourceFiles resourceFiles) { |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles, |
|
|
|
|
this.resourceFiles.and(resourceFiles), this.classFiles, this.processors, |
|
|
|
|
this.compilerOptions); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles, this.resourceFiles.and(resourceFiles), |
|
|
|
|
this.classFiles, this.processors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -177,9 +194,9 @@ public final class TestCompiler {
@@ -177,9 +194,9 @@ public final class TestCompiler {
|
|
|
|
|
* @return a new {@code TestCompiler} instance |
|
|
|
|
*/ |
|
|
|
|
public TestCompiler withClasses(Iterable<ClassFile> classFiles) { |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles, |
|
|
|
|
this.resourceFiles, this.classFiles.and(classFiles), this.processors, |
|
|
|
|
this.compilerOptions); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles, this.resourceFiles, this.classFiles.and(classFiles), |
|
|
|
|
this.processors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -190,8 +207,9 @@ public final class TestCompiler {
@@ -190,8 +207,9 @@ public final class TestCompiler {
|
|
|
|
|
public TestCompiler withProcessors(Processor... processors) { |
|
|
|
|
List<Processor> mergedProcessors = new ArrayList<>(this.processors); |
|
|
|
|
mergedProcessors.addAll(Arrays.asList(processors)); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles, |
|
|
|
|
this.resourceFiles, this.classFiles, mergedProcessors, this.compilerOptions); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles, this.resourceFiles, this.classFiles, mergedProcessors, |
|
|
|
|
this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -202,8 +220,9 @@ public final class TestCompiler {
@@ -202,8 +220,9 @@ public final class TestCompiler {
|
|
|
|
|
public TestCompiler withProcessors(Iterable<Processor> processors) { |
|
|
|
|
List<Processor> mergedProcessors = new ArrayList<>(this.processors); |
|
|
|
|
processors.forEach(mergedProcessors::add); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles, |
|
|
|
|
this.resourceFiles, this.classFiles, mergedProcessors, this.compilerOptions); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles, this.resourceFiles, this.classFiles, |
|
|
|
|
mergedProcessors, this.compilerOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -215,8 +234,9 @@ public final class TestCompiler {
@@ -215,8 +234,9 @@ public final class TestCompiler {
|
|
|
|
|
public TestCompiler withCompilerOptions(String... options) { |
|
|
|
|
List<String> mergedCompilerOptions = Stream.concat(this.compilerOptions.stream(), |
|
|
|
|
Arrays.stream(options)).distinct().toList(); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles, |
|
|
|
|
this.resourceFiles, this.classFiles, this.processors, mergedCompilerOptions); |
|
|
|
|
return new TestCompiler(this.classLoader, this.compiler, this.locale, |
|
|
|
|
this.sourceFiles, this.resourceFiles, this.classFiles, |
|
|
|
|
this.processors, mergedCompilerOptions); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -308,7 +328,7 @@ public final class TestCompiler {
@@ -308,7 +328,7 @@ public final class TestCompiler {
|
|
|
|
|
DynamicJavaFileManager fileManager = new DynamicJavaFileManager( |
|
|
|
|
standardFileManager, classLoaderToUse, this.classFiles, this.resourceFiles); |
|
|
|
|
if (!this.sourceFiles.isEmpty()) { |
|
|
|
|
Errors errors = new Errors(); |
|
|
|
|
Errors errors = new Errors(this.locale); |
|
|
|
|
CompilationTask task = this.compiler.getTask(null, fileManager, errors, |
|
|
|
|
this.compilerOptions, null, compilationUnits); |
|
|
|
|
if (!this.processors.isEmpty()) { |
|
|
|
|
@ -349,13 +369,19 @@ public final class TestCompiler {
@@ -349,13 +369,19 @@ public final class TestCompiler {
|
|
|
|
|
*/ |
|
|
|
|
static class Errors implements DiagnosticListener<JavaFileObject> { |
|
|
|
|
|
|
|
|
|
private final Locale locale; |
|
|
|
|
|
|
|
|
|
private final StringBuilder message = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
Errors(Locale locale) { |
|
|
|
|
this.locale = locale; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void report(Diagnostic<? extends JavaFileObject> diagnostic) { |
|
|
|
|
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { |
|
|
|
|
this.message.append('\n'); |
|
|
|
|
this.message.append(diagnostic.getMessage(Locale.getDefault())); |
|
|
|
|
this.message.append(diagnostic.getMessage(this.locale)); |
|
|
|
|
if (diagnostic.getSource() != null) { |
|
|
|
|
this.message.append(' '); |
|
|
|
|
this.message.append(diagnostic.getSource().getName()); |
|
|
|
|
|