12
12
import javax .lang .model .element .PackageElement ;
13
13
import javax .lang .model .element .TypeElement ;
14
14
import javax .tools .Diagnostic ;
15
+
16
+ import io .toolisticon .cute .CuteApi .ExceptionAssertion ;
17
+ import io .toolisticon .cute .CuteApi .ExceptionCheckBB ;
18
+
15
19
import java .util .HashSet ;
16
20
import java .util .Set ;
17
21
21
25
final class AnnotationProcessorWrapper implements Processor {
22
26
23
27
private final Processor wrappedProcessor ;
24
- private final Class <? extends Throwable > expectedThrownException ;
28
+ private final ExceptionCheckBB exceptionChecks ;
25
29
private Messager messager ;
26
30
27
31
private boolean firstRound = true ;
@@ -31,9 +35,9 @@ private AnnotationProcessorWrapper(Processor processor) {
31
35
this (processor , null );
32
36
}
33
37
34
- private AnnotationProcessorWrapper (Processor processor , Class <? extends Throwable > expectedThrownException ) {
38
+ private AnnotationProcessorWrapper (Processor processor , ExceptionCheckBB exceptionChecks ) {
35
39
this .wrappedProcessor = processor ;
36
- this .expectedThrownException = expectedThrownException ;
40
+ this .exceptionChecks = exceptionChecks ;
37
41
}
38
42
39
43
@@ -83,19 +87,23 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
83
87
throw (AssertionError ) e ;
84
88
}
85
89
86
- if (this .expectedThrownException != null ) {
90
+ if (this .exceptionChecks != null && this . exceptionChecks . getExceptionIsThrown () != null ) {
87
91
88
- if (!this .expectedThrownException .isAssignableFrom (e .getClass ())) {
92
+ if (!this .exceptionChecks . getExceptionIsThrown () .isAssignableFrom (e .getClass ())) {
89
93
throw new FailingAssertionException (
90
94
Constants .Messages .ASSERTION_GOT_UNEXPECTED_EXCEPTION_INSTEAD_OF_EXPECTED .produceMessage (
91
- this .expectedThrownException .getCanonicalName (),
95
+ this .exceptionChecks . getExceptionIsThrown () .getCanonicalName (),
92
96
e .getClass ().getCanonicalName (),
93
97
e .getMessage () != null ? Constants .Messages .TOKEN__WITH_MESSAGE + e .getMessage () : "" )
94
98
, e );
95
99
}
96
100
97
101
// Exception has been found
98
102
expectedExceptionWasThrown = true ;
103
+
104
+ if (this .exceptionChecks .getExceptionAssertion () != null ) {
105
+ doExceptionAssertion (this .exceptionChecks .getExceptionAssertion (), (Exception ) e );
106
+ }
99
107
100
108
} else {
101
109
@@ -112,15 +120,20 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
112
120
}
113
121
114
122
// check in last round if expected exception has been thrown - in this case trigger assertion error
115
- if (roundEnv .processingOver () && !expectedExceptionWasThrown && this .expectedThrownException != null ) {
123
+ if (roundEnv .processingOver () && !expectedExceptionWasThrown && this .exceptionChecks != null && this . exceptionChecks . getExceptionIsThrown () != null ) {
116
124
throw new FailingAssertionException (
117
- Constants .Messages .ASSERTION_EXPECTED_EXCEPTION_NOT_THROWN .produceMessage (this .expectedThrownException .getCanonicalName ())
125
+ Constants .Messages .ASSERTION_EXPECTED_EXCEPTION_NOT_THROWN .produceMessage (this .exceptionChecks . getExceptionIsThrown () .getCanonicalName ())
118
126
);
119
127
}
120
128
121
129
122
130
return returnValue ;
123
131
}
132
+
133
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
134
+ private <EXCEPTION extends Exception > void doExceptionAssertion (ExceptionAssertion exceptionAssertion , Exception e ) {
135
+ ((ExceptionAssertion <EXCEPTION >)exceptionAssertion ).doAssertion ((EXCEPTION )e );
136
+ }
124
137
125
138
@ Override
126
139
public Iterable <? extends Completion > getCompletions (Element element , AnnotationMirror annotation , ExecutableElement member , String userText ) {
@@ -139,13 +152,13 @@ public static AnnotationProcessorWrapper wrapProcessor(Processor processorToWrap
139
152
return wrapProcessor (processorToWrap , null );
140
153
}
141
154
142
- public static AnnotationProcessorWrapper wrapProcessor (Processor processorToWrap , Class <? extends Throwable > expectedThrownException ) {
155
+ public static AnnotationProcessorWrapper wrapProcessor (Processor processorToWrap , ExceptionCheckBB exceptionChecks ) {
143
156
144
157
if (processorToWrap == null ) {
145
158
throw new IllegalArgumentException (Constants .Messages .IAE_PASSED_PARAMETER_MUST_NOT_BE_NULL .produceMessage ("processor" ));
146
159
}
147
160
148
- return new AnnotationProcessorWrapper (processorToWrap , expectedThrownException );
161
+ return new AnnotationProcessorWrapper (processorToWrap , exceptionChecks );
149
162
}
150
163
151
164
public static <T extends Processor > AnnotationProcessorWrapper wrapProcessor (Class <T > processorTypeToWrap ) {
@@ -161,18 +174,18 @@ public static <T extends Processor> AnnotationProcessorWrapper wrapProcessor(Cla
161
174
162
175
}
163
176
164
- public static <T extends Processor > AnnotationProcessorWrapper wrapProcessor (Class <T > processorTypeToWrap , Class <? extends Throwable > expectedThrownException ) {
177
+ public static <T extends Processor > AnnotationProcessorWrapper wrapProcessor (Class <T > processorTypeToWrap , ExceptionCheckBB exceptionChecks ) {
165
178
166
179
if (processorTypeToWrap == null ) {
167
180
throw new IllegalArgumentException (Constants .Messages .IAE_PASSED_PARAMETER_MUST_NOT_BE_NULL .produceMessage ("type" ));
168
181
}
169
182
170
- if (expectedThrownException == null ) {
183
+ if (exceptionChecks == null || exceptionChecks . getExceptionIsThrown () == null ) {
171
184
throw new IllegalArgumentException (Constants .Messages .IAE_PASSED_PARAMETER_MUST_NOT_BE_NULL .produceMessage ("expected exception" ));
172
185
}
173
186
174
187
try {
175
- return new AnnotationProcessorWrapper (processorTypeToWrap .getDeclaredConstructor ().newInstance (), expectedThrownException );
188
+ return new AnnotationProcessorWrapper (processorTypeToWrap .getDeclaredConstructor ().newInstance (), exceptionChecks );
176
189
} catch (Exception e ) {
177
190
throw new IllegalArgumentException (Constants .Messages .IAE_CANNOT_INSTANTIATE_PROCESSOR .produceMessage (processorTypeToWrap .getCanonicalName ()), e );
178
191
}
@@ -258,7 +271,7 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest
258
271
}
259
272
260
273
if (processor != null ) {
261
- wrappedProcessors .add (AnnotationProcessorWrapper .wrapProcessor (processor , compilerTestBB .getExceptionIsThrown ()));
274
+ wrappedProcessors .add (AnnotationProcessorWrapper .wrapProcessor (processor , compilerTestBB .getExceptionChecks ()));
262
275
}
263
276
}
264
277
@@ -268,7 +281,7 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest
268
281
try {
269
282
270
283
Processor processor = processorType .getDeclaredConstructor ().newInstance ();
271
- wrappedProcessors .add (AnnotationProcessorWrapper .wrapProcessor (processor , compilerTestBB .getExceptionIsThrown ()));
284
+ wrappedProcessors .add (AnnotationProcessorWrapper .wrapProcessor (processor , compilerTestBB .getExceptionChecks ()));
272
285
273
286
} catch (Exception e ) {
274
287
throw new IllegalArgumentException (Constants .Messages .IAE_CANNOT_INSTANTIATE_PROCESSOR .produceMessage (processorType .getCanonicalName ()));
0 commit comments