@@ -53,6 +53,8 @@ public final class TestCompiler {
53
53
54
54
private final JavaCompiler compiler ;
55
55
56
+ private final Locale locale ;
57
+
56
58
private final SourceFiles sourceFiles ;
57
59
58
60
private final ResourceFiles resourceFiles ;
@@ -64,12 +66,13 @@ public final class TestCompiler {
64
66
private final List <String > compilerOptions ;
65
67
66
68
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 ) {
70
72
71
73
this .classLoader = classLoader ;
72
74
this .compiler = compiler ;
75
+ this .locale = locale ;
73
76
this .sourceFiles = sourceFiles ;
74
77
this .resourceFiles = resourceFiles ;
75
78
this .classFiles = classFiles ;
@@ -92,8 +95,9 @@ public static TestCompiler forSystem() {
92
95
* @return a new {@code TestCompiler} instance
93
96
*/
94
97
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 ());
97
101
}
98
102
99
103
/**
@@ -105,13 +109,26 @@ public TestCompiler with(UnaryOperator<TestCompiler> customizer) {
105
109
return customizer .apply (this );
106
110
}
107
111
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
+
108
125
/**
109
126
* Create a new {@code TestCompiler} instance with additional source files.
110
127
* @param sourceFiles the additional source files
111
128
* @return a new {@code TestCompiler} instance
112
129
*/
113
130
public TestCompiler withSources (SourceFile ... sourceFiles ) {
114
- return new TestCompiler (this .classLoader , this .compiler ,
131
+ return new TestCompiler (this .classLoader , this .compiler , this . locale ,
115
132
this .sourceFiles .and (sourceFiles ), this .resourceFiles ,
116
133
this .classFiles , this .processors , this .compilerOptions );
117
134
}
@@ -122,7 +139,7 @@ public TestCompiler withSources(SourceFile... sourceFiles) {
122
139
* @return a new {@code TestCompiler} instance
123
140
*/
124
141
public TestCompiler withSources (Iterable <SourceFile > sourceFiles ) {
125
- return new TestCompiler (this .classLoader , this .compiler ,
142
+ return new TestCompiler (this .classLoader , this .compiler , this . locale ,
126
143
this .sourceFiles .and (sourceFiles ), this .resourceFiles ,
127
144
this .classFiles , this .processors , this .compilerOptions );
128
145
}
@@ -133,7 +150,7 @@ public TestCompiler withSources(Iterable<SourceFile> sourceFiles) {
133
150
* @return a new {@code TestCompiler} instance
134
151
*/
135
152
public TestCompiler withSources (SourceFiles sourceFiles ) {
136
- return new TestCompiler (this .classLoader , this .compiler ,
153
+ return new TestCompiler (this .classLoader , this .compiler , this . locale ,
137
154
this .sourceFiles .and (sourceFiles ), this .resourceFiles ,
138
155
this .classFiles , this .processors , this .compilerOptions );
139
156
}
@@ -144,9 +161,9 @@ public TestCompiler withSources(SourceFiles sourceFiles) {
144
161
* @return a new {@code TestCompiler} instance
145
162
*/
146
163
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 );
150
167
}
151
168
152
169
/**
@@ -155,9 +172,9 @@ public TestCompiler withResources(ResourceFile... resourceFiles) {
155
172
* @return a new {@code TestCompiler} instance
156
173
*/
157
174
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 );
161
178
}
162
179
163
180
/**
@@ -166,9 +183,9 @@ public TestCompiler withResources(Iterable<ResourceFile> resourceFiles) {
166
183
* @return a new {@code TestCompiler} instance
167
184
*/
168
185
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 );
172
189
}
173
190
174
191
/**
@@ -177,9 +194,9 @@ public TestCompiler withResources(ResourceFiles resourceFiles) {
177
194
* @return a new {@code TestCompiler} instance
178
195
*/
179
196
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 );
183
200
}
184
201
185
202
/**
@@ -190,8 +207,9 @@ public TestCompiler withClasses(Iterable<ClassFile> classFiles) {
190
207
public TestCompiler withProcessors (Processor ... processors ) {
191
208
List <Processor > mergedProcessors = new ArrayList <>(this .processors );
192
209
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 );
195
213
}
196
214
197
215
/**
@@ -202,8 +220,9 @@ public TestCompiler withProcessors(Processor... processors) {
202
220
public TestCompiler withProcessors (Iterable <Processor > processors ) {
203
221
List <Processor > mergedProcessors = new ArrayList <>(this .processors );
204
222
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 );
207
226
}
208
227
209
228
/**
@@ -215,8 +234,9 @@ public TestCompiler withProcessors(Iterable<Processor> processors) {
215
234
public TestCompiler withCompilerOptions (String ... options ) {
216
235
List <String > mergedCompilerOptions = Stream .concat (this .compilerOptions .stream (),
217
236
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 );
220
240
}
221
241
222
242
/**
@@ -308,7 +328,7 @@ private DynamicClassLoader compile() {
308
328
DynamicJavaFileManager fileManager = new DynamicJavaFileManager (
309
329
standardFileManager , classLoaderToUse , this .classFiles , this .resourceFiles );
310
330
if (!this .sourceFiles .isEmpty ()) {
311
- Errors errors = new Errors ();
331
+ Errors errors = new Errors (this . locale );
312
332
CompilationTask task = this .compiler .getTask (null , fileManager , errors ,
313
333
this .compilerOptions , null , compilationUnits );
314
334
if (!this .processors .isEmpty ()) {
@@ -349,13 +369,19 @@ public TestCompiler printFiles(PrintStream printStream) {
349
369
*/
350
370
static class Errors implements DiagnosticListener <JavaFileObject > {
351
371
372
+ private final Locale locale ;
373
+
352
374
private final StringBuilder message = new StringBuilder ();
353
375
376
+ Errors (Locale locale ) {
377
+ this .locale = locale ;
378
+ }
379
+
354
380
@ Override
355
381
public void report (Diagnostic <? extends JavaFileObject > diagnostic ) {
356
382
if (diagnostic .getKind () == Diagnostic .Kind .ERROR ) {
357
383
this .message .append ('\n' );
358
- this .message .append (diagnostic .getMessage (Locale . getDefault () ));
384
+ this .message .append (diagnostic .getMessage (this . locale ));
359
385
if (diagnostic .getSource () != null ) {
360
386
this .message .append (' ' );
361
387
this .message .append (diagnostic .getSource ().getName ());
0 commit comments