18
18
19
19
import java .io .PrintStream ;
20
20
import java .util .ArrayList ;
21
+ import java .util .Arrays ;
22
+ import java .util .Collections ;
21
23
import java .util .List ;
22
24
import java .util .Locale ;
23
25
import java .util .function .Consumer ;
24
26
27
+ import javax .annotation .processing .Processor ;
25
28
import javax .tools .Diagnostic ;
26
29
import javax .tools .DiagnosticListener ;
27
30
import javax .tools .JavaCompiler ;
43
46
* Utility that can be used to dynamically compile and test Java source code.
44
47
*
45
48
* @author Phillip Webb
49
+ * @author Scott Frederick
46
50
* @since 6.0
47
51
* @see #forSystem()
48
52
*/
@@ -57,13 +61,18 @@ public final class TestCompiler {
57
61
58
62
private final ResourceFiles resourceFiles ;
59
63
64
+ private final List <Processor > processors ;
65
+
60
66
61
67
private TestCompiler (@ Nullable ClassLoader classLoader , JavaCompiler compiler ,
62
- SourceFiles sourceFiles , ResourceFiles resourceFiles ) {
68
+ SourceFiles sourceFiles , ResourceFiles resourceFiles ,
69
+ List <Processor > processors ) {
70
+
63
71
this .classLoader = classLoader ;
64
72
this .compiler = compiler ;
65
73
this .sourceFiles = sourceFiles ;
66
74
this .resourceFiles = resourceFiles ;
75
+ this .processors = processors ;
67
76
}
68
77
69
78
@@ -83,7 +92,7 @@ public static TestCompiler forSystem() {
83
92
*/
84
93
public static TestCompiler forCompiler (JavaCompiler javaCompiler ) {
85
94
return new TestCompiler (null , javaCompiler , SourceFiles .none (),
86
- ResourceFiles .none ());
95
+ ResourceFiles .none (), Collections . emptyList () );
87
96
}
88
97
89
98
/**
@@ -110,7 +119,7 @@ public TestCompiler withFiles(InMemoryGeneratedFiles generatedFiles) {
110
119
*/
111
120
public TestCompiler withSources (SourceFile ... sourceFiles ) {
112
121
return new TestCompiler (this .classLoader , this .compiler ,
113
- this .sourceFiles .and (sourceFiles ), this .resourceFiles );
122
+ this .sourceFiles .and (sourceFiles ), this .resourceFiles , this . processors );
114
123
}
115
124
116
125
/**
@@ -120,7 +129,7 @@ public TestCompiler withSources(SourceFile... sourceFiles) {
120
129
*/
121
130
public TestCompiler withSources (Iterable <SourceFile > sourceFiles ) {
122
131
return new TestCompiler (this .classLoader , this .compiler ,
123
- this .sourceFiles .and (sourceFiles ), this .resourceFiles );
132
+ this .sourceFiles .and (sourceFiles ), this .resourceFiles , this . processors );
124
133
}
125
134
126
135
/**
@@ -130,7 +139,7 @@ public TestCompiler withSources(Iterable<SourceFile> sourceFiles) {
130
139
*/
131
140
public TestCompiler withSources (SourceFiles sourceFiles ) {
132
141
return new TestCompiler (this .classLoader , this .compiler ,
133
- this .sourceFiles .and (sourceFiles ), this .resourceFiles );
142
+ this .sourceFiles .and (sourceFiles ), this .resourceFiles , this . processors );
134
143
}
135
144
136
145
/**
@@ -140,7 +149,7 @@ public TestCompiler withSources(SourceFiles sourceFiles) {
140
149
*/
141
150
public TestCompiler withResources (ResourceFile ... resourceFiles ) {
142
151
return new TestCompiler (this .classLoader , this .compiler , this .sourceFiles ,
143
- this .resourceFiles .and (resourceFiles ));
152
+ this .resourceFiles .and (resourceFiles ), this . processors );
144
153
}
145
154
146
155
/**
@@ -150,7 +159,7 @@ public TestCompiler withResources(ResourceFile... resourceFiles) {
150
159
*/
151
160
public TestCompiler withResources (Iterable <ResourceFile > resourceFiles ) {
152
161
return new TestCompiler (this .classLoader , this .compiler , this .sourceFiles ,
153
- this .resourceFiles .and (resourceFiles ));
162
+ this .resourceFiles .and (resourceFiles ), this . processors );
154
163
}
155
164
156
165
/**
@@ -160,9 +169,32 @@ public TestCompiler withResources(Iterable<ResourceFile> resourceFiles) {
160
169
*/
161
170
public TestCompiler withResources (ResourceFiles resourceFiles ) {
162
171
return new TestCompiler (this .classLoader , this .compiler , this .sourceFiles ,
163
- this .resourceFiles .and (resourceFiles ));
172
+ this .resourceFiles .and (resourceFiles ), this .processors );
173
+ }
174
+
175
+ /**
176
+ * Return a new {@link TestCompiler} instance with additional annotation processors.
177
+ * @param processors the additional annotation processors
178
+ * @return a new {@link TestCompiler} instance
179
+ */
180
+ public TestCompiler withProcessors (Processor ... processors ) {
181
+ List <Processor > mergedProcessors = new ArrayList <>(this .processors );
182
+ mergedProcessors .addAll (Arrays .asList (processors ));
183
+ return new TestCompiler (this .classLoader , this .compiler , this .sourceFiles ,
184
+ this .resourceFiles , mergedProcessors );
164
185
}
165
186
187
+ /**
188
+ * Return a new {@link TestCompiler} instance with additional annotation processors.
189
+ * @param processors the additional annotation processors
190
+ * @return a new {@link TestCompiler} instance
191
+ */
192
+ public TestCompiler withProcessors (Iterable <Processor > processors ) {
193
+ List <Processor > mergedProcessors = new ArrayList <>(this .processors );
194
+ processors .forEach (mergedProcessors ::add );
195
+ return new TestCompiler (this .classLoader , this .compiler , this .sourceFiles ,
196
+ this .resourceFiles , mergedProcessors );
197
+ }
166
198
167
199
/**
168
200
* Compile content from this instance along with the additional provided
@@ -244,6 +276,9 @@ private DynamicClassLoader compile() {
244
276
Errors errors = new Errors ();
245
277
CompilationTask task = this .compiler .getTask (null , fileManager , errors , null ,
246
278
null , compilationUnits );
279
+ if (!this .processors .isEmpty ()) {
280
+ task .setProcessors (this .processors );
281
+ }
247
282
boolean result = task .call ();
248
283
if (!result || errors .hasReportedErrors ()) {
249
284
throw new CompilationException (errors .toString (), this .sourceFiles , this .resourceFiles );
0 commit comments