1212import javax .lang .model .element .PackageElement ;
1313import javax .lang .model .element .TypeElement ;
1414import javax .tools .Diagnostic ;
15+
16+ import io .toolisticon .cute .CuteApi .ExceptionAssertion ;
17+ import io .toolisticon .cute .CuteApi .ExceptionCheckBB ;
18+
1519import java .util .HashSet ;
1620import java .util .Set ;
1721
2125final class AnnotationProcessorWrapper implements Processor {
2226
2327 private final Processor wrappedProcessor ;
24- private final Class <? extends Throwable > expectedThrownException ;
28+ private final ExceptionCheckBB exceptionChecks ;
2529 private Messager messager ;
2630
2731 private boolean firstRound = true ;
@@ -31,9 +35,9 @@ private AnnotationProcessorWrapper(Processor processor) {
3135 this (processor , null );
3236 }
3337
34- private AnnotationProcessorWrapper (Processor processor , Class <? extends Throwable > expectedThrownException ) {
38+ private AnnotationProcessorWrapper (Processor processor , ExceptionCheckBB exceptionChecks ) {
3539 this .wrappedProcessor = processor ;
36- this .expectedThrownException = expectedThrownException ;
40+ this .exceptionChecks = exceptionChecks ;
3741 }
3842
3943
@@ -83,19 +87,23 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
8387 throw (AssertionError ) e ;
8488 }
8589
86- if (this .expectedThrownException != null ) {
90+ if (this .exceptionChecks != null && this . exceptionChecks . getExceptionIsThrown () != null ) {
8791
88- if (!this .expectedThrownException .isAssignableFrom (e .getClass ())) {
92+ if (!this .exceptionChecks . getExceptionIsThrown () .isAssignableFrom (e .getClass ())) {
8993 throw new FailingAssertionException (
9094 Constants .Messages .ASSERTION_GOT_UNEXPECTED_EXCEPTION_INSTEAD_OF_EXPECTED .produceMessage (
91- this .expectedThrownException .getCanonicalName (),
95+ this .exceptionChecks . getExceptionIsThrown () .getCanonicalName (),
9296 e .getClass ().getCanonicalName (),
9397 e .getMessage () != null ? Constants .Messages .TOKEN__WITH_MESSAGE + e .getMessage () : "" )
9498 , e );
9599 }
96100
97101 // Exception has been found
98102 expectedExceptionWasThrown = true ;
103+
104+ if (this .exceptionChecks .getExceptionAssertion () != null ) {
105+ doExceptionAssertion (this .exceptionChecks .getExceptionAssertion (), (Exception ) e );
106+ }
99107
100108 } else {
101109
@@ -112,15 +120,20 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
112120 }
113121
114122 // 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 ) {
116124 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 ())
118126 );
119127 }
120128
121129
122130 return returnValue ;
123131 }
132+
133+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
134+ private <EXCEPTION extends Exception > void doExceptionAssertion (ExceptionAssertion exceptionAssertion , Exception e ) {
135+ ((ExceptionAssertion <EXCEPTION >)exceptionAssertion ).doAssertion ((EXCEPTION )e );
136+ }
124137
125138 @ Override
126139 public Iterable <? extends Completion > getCompletions (Element element , AnnotationMirror annotation , ExecutableElement member , String userText ) {
@@ -139,13 +152,13 @@ public static AnnotationProcessorWrapper wrapProcessor(Processor processorToWrap
139152 return wrapProcessor (processorToWrap , null );
140153 }
141154
142- public static AnnotationProcessorWrapper wrapProcessor (Processor processorToWrap , Class <? extends Throwable > expectedThrownException ) {
155+ public static AnnotationProcessorWrapper wrapProcessor (Processor processorToWrap , ExceptionCheckBB exceptionChecks ) {
143156
144157 if (processorToWrap == null ) {
145158 throw new IllegalArgumentException (Constants .Messages .IAE_PASSED_PARAMETER_MUST_NOT_BE_NULL .produceMessage ("processor" ));
146159 }
147160
148- return new AnnotationProcessorWrapper (processorToWrap , expectedThrownException );
161+ return new AnnotationProcessorWrapper (processorToWrap , exceptionChecks );
149162 }
150163
151164 public static <T extends Processor > AnnotationProcessorWrapper wrapProcessor (Class <T > processorTypeToWrap ) {
@@ -161,18 +174,18 @@ public static <T extends Processor> AnnotationProcessorWrapper wrapProcessor(Cla
161174
162175 }
163176
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 ) {
165178
166179 if (processorTypeToWrap == null ) {
167180 throw new IllegalArgumentException (Constants .Messages .IAE_PASSED_PARAMETER_MUST_NOT_BE_NULL .produceMessage ("type" ));
168181 }
169182
170- if (expectedThrownException == null ) {
183+ if (exceptionChecks == null || exceptionChecks . getExceptionIsThrown () == null ) {
171184 throw new IllegalArgumentException (Constants .Messages .IAE_PASSED_PARAMETER_MUST_NOT_BE_NULL .produceMessage ("expected exception" ));
172185 }
173186
174187 try {
175- return new AnnotationProcessorWrapper (processorTypeToWrap .getDeclaredConstructor ().newInstance (), expectedThrownException );
188+ return new AnnotationProcessorWrapper (processorTypeToWrap .getDeclaredConstructor ().newInstance (), exceptionChecks );
176189 } catch (Exception e ) {
177190 throw new IllegalArgumentException (Constants .Messages .IAE_CANNOT_INSTANTIATE_PROCESSOR .produceMessage (processorTypeToWrap .getCanonicalName ()), e );
178191 }
@@ -258,7 +271,7 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest
258271 }
259272
260273 if (processor != null ) {
261- wrappedProcessors .add (AnnotationProcessorWrapper .wrapProcessor (processor , compilerTestBB .getExceptionIsThrown ()));
274+ wrappedProcessors .add (AnnotationProcessorWrapper .wrapProcessor (processor , compilerTestBB .getExceptionChecks ()));
262275 }
263276 }
264277
@@ -268,7 +281,7 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest
268281 try {
269282
270283 Processor processor = processorType .getDeclaredConstructor ().newInstance ();
271- wrappedProcessors .add (AnnotationProcessorWrapper .wrapProcessor (processor , compilerTestBB .getExceptionIsThrown ()));
284+ wrappedProcessors .add (AnnotationProcessorWrapper .wrapProcessor (processor , compilerTestBB .getExceptionChecks ()));
272285
273286 } catch (Exception e ) {
274287 throw new IllegalArgumentException (Constants .Messages .IAE_CANNOT_INSTANTIATE_PROCESSOR .produceMessage (processorType .getCanonicalName ()));
0 commit comments