Allow JavaTemplate to throw new Exception #4260
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What's changed?
Update
BlockStatementTemplateGenerator
to separately handleJ.MethodInvocation
, and not produce an assignment when the methodInvocation has a void return type. This prevents a compiler error for such templates. Additionally wrap any generated template for J.MethodInvocation in a block (class Template {{ ... }}
, as opposed toclass Template { ... }
) to allow for throwing exceptions.What's your motivation?
Broke the recipes generated for this one, which contained
final JavaTemplate after = JavaTemplate.builder("throw new AssertionError();").build();
.https://github.com/PicnicSupermarket/error-prone-support/blob/d81fe19836db9f0001fb9c01370a3b808c46b440/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/TestNGToAssertJRules.java#L156-L167
Have you considered any alternatives or workarounds?
Could have nested the generated template in the
j instanceof Expression
conditional just below it, but this seemed cleaner.Any additional context
As part of the work done towards