Fix: UnsupportedOperationException when resolving method calls inside lambdas in Comparator.comparing (#2716)#5026
Merged
Merged
Conversation
… lambdas in Comparator.comparing (javaparser#2716)
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #5026 +/- ##
===============================================
- Coverage 58.699% 58.693% -0.007%
- Complexity 2587 2595 +8
===============================================
Files 702 702
Lines 40213 40277 +64
Branches 7325 7340 +15
===============================================
+ Hits 23605 23640 +35
- Misses 13636 13663 +27
- Partials 2972 2974 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2716 .
Resolving method calls on implicit lambda parameters (e.g.
a.getName()inComparator.comparing(a -> a.getName())) crashed with anUnsupportedOperationException when the lambda was used inside a stream chain
on a user-defined element type.
Root cause
Comparator.comparingis a static generic method, so the scope-based typeparameter substitution that works for instance calls (e.g.
stream.map(...))is never taken. Its type parameter T therefore remained as an unresolved
TypeVariable, which caused two cascading failures:
LambdaExprContext.solveSymbolAsValuereturned a constraint type whosebound was an unresolved TypeVariable T, and
AbstractJavaParserContext.findTypeDeclarationshad no handler for thatshape, throwing UnsupportedOperationException.
Fix
LambdaExprContext.java — outer-context type parameter inference:
When the enclosing method call (e.g.
comparing(...)) is itself anargument of another method call with a known receiver type (e.g.
stream.sorted(...)), infer the static method's type parameters from theouter call's expected parameter type. Concretely, the pair
(comparing's return type
Comparator<T>) ↔ (sorted's expected typeComparator<? super A>) is registered in the InferenceContext, lettingthe engine resolve T →
? super Aand thus determine the lambdaparameter type.
AbstractJavaParserContext.java — extended constraint-type handling in
findTypeDeclarations:
Added two new sub-cases inside the isConstraint() branch:
(fallback when outer-context inference is unavailable); use the
variable's explicit upper bounds, defaulting to Object.
? super Xproduced by theouter-context inference; use X's type declaration (with the same
TypeVariable fallback for X if needed).
Tests
solveSymbolAsValue("s") on the lambda in
stream.sorted(Comparator.comparing(s -> s.toLowerCase()))returns? super java.lang.String.method calls in a stream chain with a user-defined element type resolve
without throwing (direct regression for Cannot resolve sorted() in streams #2716), and one asserting the
precise qualified signature of
toLowerCase()when the element type isString.