-
-
Notifications
You must be signed in to change notification settings - Fork 353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: properly print type parameter reference in instanceof #4921
fix: properly print type parameter reference in instanceof #4921
Conversation
|
@@ -537,7 +537,7 @@ public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) { | |||
printer.writeSpace(); | |||
try (Writable _context = context.modify()) { | |||
if (operator.getKind() == BinaryOperatorKind.INSTANCEOF) { | |||
_context.forceWildcardGenerics(true); | |||
_context.forceWildcardGenerics(env.getComplianceLevel() < 16); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please move this in a method containing an explanation why we have this condition?
} | ||
|
||
<T> T first(Iterable<T> iterable) { | ||
if (iterable instanceof List<T> list) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test with List<T<U>>
or something more complex?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add some inline comments other LGTM
LGTM will merge tomorrow. |
Thanks @SirYwell. Almost forget merging this PR. Please don't hesitate to ping me next time. |
Since Java 16, it is allowed to have type parameters other than
<?>
in instanceof checks. This is allowed in cases where the generic type can be carried over from the type on the left side.I added a test with three such examples, one with pattern matching (the main reason this feature was added), one normal instanceof check with a type parameter as type argument and one with String as type argument.
The current solution is to not enforce wildcard in the printer for compliance mode >= 16. I would personally prefer to not change the printing output there at all, but there are tests relying on it - namely this one:
spoon/src/test/java/spoon/test/template/TemplateTest.java
Line 1096 in 59990fe
And it also might break existing code.
I'm not sure if that's the best solution. If you have any better ideas, please let me know.