Skip to content

Commit 93206c3

Browse files
committed
Add support for custom locale to render compiler messages
Closes gh-31408
1 parent ae88bba commit 93206c3

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

spring-core-test/src/main/java/org/springframework/core/test/tools/TestCompiler.java

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public final class TestCompiler {
5353

5454
private final JavaCompiler compiler;
5555

56+
private final Locale locale;
57+
5658
private final SourceFiles sourceFiles;
5759

5860
private final ResourceFiles resourceFiles;
@@ -64,12 +66,13 @@ public final class TestCompiler {
6466
private final List<String> compilerOptions;
6567

6668

67-
private TestCompiler(@Nullable ClassLoader classLoader, JavaCompiler compiler,
68-
SourceFiles sourceFiles, ResourceFiles resourceFiles, ClassFiles classFiles,
69-
List<Processor> processors, List<String> compilerOptions) {
69+
private TestCompiler(@Nullable ClassLoader classLoader, JavaCompiler compiler, Locale locale,
70+
SourceFiles sourceFiles, ResourceFiles resourceFiles, ClassFiles classFiles, List<Processor> processors,
71+
List<String> compilerOptions) {
7072

7173
this.classLoader = classLoader;
7274
this.compiler = compiler;
75+
this.locale = locale;
7376
this.sourceFiles = sourceFiles;
7477
this.resourceFiles = resourceFiles;
7578
this.classFiles = classFiles;
@@ -92,8 +95,9 @@ public static TestCompiler forSystem() {
9295
* @return a new {@code TestCompiler} instance
9396
*/
9497
public static TestCompiler forCompiler(JavaCompiler javaCompiler) {
95-
return new TestCompiler(null, javaCompiler, SourceFiles.none(),
96-
ResourceFiles.none(), ClassFiles.none(), Collections.emptyList(), Collections.emptyList());
98+
return new TestCompiler(null, javaCompiler, Locale.getDefault(),
99+
SourceFiles.none(), ResourceFiles.none(),
100+
ClassFiles.none(), Collections.emptyList(), Collections.emptyList());
97101
}
98102

99103
/**
@@ -105,13 +109,26 @@ public TestCompiler with(UnaryOperator<TestCompiler> customizer) {
105109
return customizer.apply(this);
106110
}
107111

112+
/**
113+
* Create a new {@code TestCompiler} instance that uses the specified {@link Locale}
114+
* to render compiler messages.
115+
* @param locale the locale to use
116+
* @return a new {@code TestCompiler} instance
117+
* @since 6.1
118+
*/
119+
public TestCompiler withLocale(Locale locale) {
120+
return new TestCompiler(this.classLoader, this.compiler, locale,
121+
this.sourceFiles, this.resourceFiles,
122+
this.classFiles, this.processors, this.compilerOptions);
123+
}
124+
108125
/**
109126
* Create a new {@code TestCompiler} instance with additional source files.
110127
* @param sourceFiles the additional source files
111128
* @return a new {@code TestCompiler} instance
112129
*/
113130
public TestCompiler withSources(SourceFile... sourceFiles) {
114-
return new TestCompiler(this.classLoader, this.compiler,
131+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
115132
this.sourceFiles.and(sourceFiles), this.resourceFiles,
116133
this.classFiles, this.processors, this.compilerOptions);
117134
}
@@ -122,7 +139,7 @@ public TestCompiler withSources(SourceFile... sourceFiles) {
122139
* @return a new {@code TestCompiler} instance
123140
*/
124141
public TestCompiler withSources(Iterable<SourceFile> sourceFiles) {
125-
return new TestCompiler(this.classLoader, this.compiler,
142+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
126143
this.sourceFiles.and(sourceFiles), this.resourceFiles,
127144
this.classFiles, this.processors, this.compilerOptions);
128145
}
@@ -133,7 +150,7 @@ public TestCompiler withSources(Iterable<SourceFile> sourceFiles) {
133150
* @return a new {@code TestCompiler} instance
134151
*/
135152
public TestCompiler withSources(SourceFiles sourceFiles) {
136-
return new TestCompiler(this.classLoader, this.compiler,
153+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
137154
this.sourceFiles.and(sourceFiles), this.resourceFiles,
138155
this.classFiles, this.processors, this.compilerOptions);
139156
}
@@ -144,9 +161,9 @@ public TestCompiler withSources(SourceFiles sourceFiles) {
144161
* @return a new {@code TestCompiler} instance
145162
*/
146163
public TestCompiler withResources(ResourceFile... resourceFiles) {
147-
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
148-
this.resourceFiles.and(resourceFiles), this.classFiles, this.processors,
149-
this.compilerOptions);
164+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
165+
this.sourceFiles, this.resourceFiles.and(resourceFiles),
166+
this.classFiles, this.processors, this.compilerOptions);
150167
}
151168

152169
/**
@@ -155,9 +172,9 @@ public TestCompiler withResources(ResourceFile... resourceFiles) {
155172
* @return a new {@code TestCompiler} instance
156173
*/
157174
public TestCompiler withResources(Iterable<ResourceFile> resourceFiles) {
158-
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
159-
this.resourceFiles.and(resourceFiles), this.classFiles, this.processors,
160-
this.compilerOptions);
175+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
176+
this.sourceFiles, this.resourceFiles.and(resourceFiles),
177+
this.classFiles, this.processors, this.compilerOptions);
161178
}
162179

163180
/**
@@ -166,9 +183,9 @@ public TestCompiler withResources(Iterable<ResourceFile> resourceFiles) {
166183
* @return a new {@code TestCompiler} instance
167184
*/
168185
public TestCompiler withResources(ResourceFiles resourceFiles) {
169-
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
170-
this.resourceFiles.and(resourceFiles), this.classFiles, this.processors,
171-
this.compilerOptions);
186+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
187+
this.sourceFiles, this.resourceFiles.and(resourceFiles),
188+
this.classFiles, this.processors, this.compilerOptions);
172189
}
173190

174191
/**
@@ -177,9 +194,9 @@ public TestCompiler withResources(ResourceFiles resourceFiles) {
177194
* @return a new {@code TestCompiler} instance
178195
*/
179196
public TestCompiler withClasses(Iterable<ClassFile> classFiles) {
180-
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
181-
this.resourceFiles, this.classFiles.and(classFiles), this.processors,
182-
this.compilerOptions);
197+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
198+
this.sourceFiles, this.resourceFiles, this.classFiles.and(classFiles),
199+
this.processors, this.compilerOptions);
183200
}
184201

185202
/**
@@ -190,8 +207,9 @@ public TestCompiler withClasses(Iterable<ClassFile> classFiles) {
190207
public TestCompiler withProcessors(Processor... processors) {
191208
List<Processor> mergedProcessors = new ArrayList<>(this.processors);
192209
mergedProcessors.addAll(Arrays.asList(processors));
193-
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
194-
this.resourceFiles, this.classFiles, mergedProcessors, this.compilerOptions);
210+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
211+
this.sourceFiles, this.resourceFiles, this.classFiles, mergedProcessors,
212+
this.compilerOptions);
195213
}
196214

197215
/**
@@ -202,8 +220,9 @@ public TestCompiler withProcessors(Processor... processors) {
202220
public TestCompiler withProcessors(Iterable<Processor> processors) {
203221
List<Processor> mergedProcessors = new ArrayList<>(this.processors);
204222
processors.forEach(mergedProcessors::add);
205-
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
206-
this.resourceFiles, this.classFiles, mergedProcessors, this.compilerOptions);
223+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
224+
this.sourceFiles, this.resourceFiles, this.classFiles,
225+
mergedProcessors, this.compilerOptions);
207226
}
208227

209228
/**
@@ -215,8 +234,9 @@ public TestCompiler withProcessors(Iterable<Processor> processors) {
215234
public TestCompiler withCompilerOptions(String... options) {
216235
List<String> mergedCompilerOptions = Stream.concat(this.compilerOptions.stream(),
217236
Arrays.stream(options)).distinct().toList();
218-
return new TestCompiler(this.classLoader, this.compiler, this.sourceFiles,
219-
this.resourceFiles, this.classFiles, this.processors, mergedCompilerOptions);
237+
return new TestCompiler(this.classLoader, this.compiler, this.locale,
238+
this.sourceFiles, this.resourceFiles, this.classFiles,
239+
this.processors, mergedCompilerOptions);
220240
}
221241

222242
/**
@@ -308,7 +328,7 @@ private DynamicClassLoader compile() {
308328
DynamicJavaFileManager fileManager = new DynamicJavaFileManager(
309329
standardFileManager, classLoaderToUse, this.classFiles, this.resourceFiles);
310330
if (!this.sourceFiles.isEmpty()) {
311-
Errors errors = new Errors();
331+
Errors errors = new Errors(this.locale);
312332
CompilationTask task = this.compiler.getTask(null, fileManager, errors,
313333
this.compilerOptions, null, compilationUnits);
314334
if (!this.processors.isEmpty()) {
@@ -349,13 +369,19 @@ public TestCompiler printFiles(PrintStream printStream) {
349369
*/
350370
static class Errors implements DiagnosticListener<JavaFileObject> {
351371

372+
private final Locale locale;
373+
352374
private final StringBuilder message = new StringBuilder();
353375

376+
Errors(Locale locale) {
377+
this.locale = locale;
378+
}
379+
354380
@Override
355381
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
356382
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
357383
this.message.append('\n');
358-
this.message.append(diagnostic.getMessage(Locale.getDefault()));
384+
this.message.append(diagnostic.getMessage(this.locale));
359385
if (diagnostic.getSource() != null) {
360386
this.message.append(' ');
361387
this.message.append(diagnostic.getSource().getName());

spring-core-test/src/test/java/org/springframework/core/test/tools/TestCompilerTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.InputStream;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.Locale;
2223
import java.util.Set;
2324
import java.util.function.Supplier;
2425

@@ -170,8 +171,8 @@ public static void main(String[] args) {
170171
}
171172
""");
172173
assertThatExceptionOfType(CompilationException.class).isThrownBy(
173-
() -> TestCompiler.forSystem().failOnWarning().withSources(
174-
SourceFile.of(HELLO_DEPRECATED), main).compile(compiled -> {
174+
() -> TestCompiler.forSystem().failOnWarning().withLocale(Locale.ENGLISH)
175+
.withSources(SourceFile.of(HELLO_DEPRECATED), main).compile(compiled -> {
175176
})).withMessageContaining("warnings found and -Werror specified");
176177
}
177178

0 commit comments

Comments
 (0)