Skip to content

Fix resolution of lambdas used as varargs#4752

Merged
jlerbsc merged 1 commit intojavaparser:masterfrom
johannescoetzee:johannes/fix-lambda-varargs
May 12, 2025
Merged

Fix resolution of lambdas used as varargs#4752
jlerbsc merged 1 commit intojavaparser:masterfrom
johannescoetzee:johannes/fix-lambda-varargs

Conversation

@johannescoetzee
Copy link
Copy Markdown
Collaborator

This PR fixes an issue I encountered when attempting to resolve lambdas that were used as varargs, for example the second argument in

class Test {
    void acceptConsumers(Consumer<String>... consumers) {}
    void test(Consumer<String> first) {
        acceptConsumers(first, s -> System.out.println(s));
    }
}

At first this resulted in an IndexOutOfBounds crash when trying to get the type of the parameter with index 1. The fix for this was to make MethodResolutionLogic.getMethodsExplicitAndVariadicParameterType public and to use that to get the type (since this accounts for varargs already) and to add a getMethodUsageExplicitAndVariadicParameterType to do the same for MethodUsage.

This fixed the IndexOutOfBounds crash, but resulted in a new issue where acceptConsumers couldn't be resolved. This is because, when getting the expected parameter type from the parent (in this case the acceptConsumers call), the returned type is an array of Consumer due to the varargs. It's not possible for a lambda to define an array type, however, so the resolution fails. This broke the rest of the lambda resolution logic, so to fix it the type is now unwrapped and the component type is used instead.

@codecov
Copy link
Copy Markdown

codecov bot commented May 12, 2025

Codecov Report

Attention: Patch coverage is 52.94118% with 8 lines in your changes missing coverage. Please review.

Project coverage is 58.319%. Comparing base (0798575) to head (fd89ae0).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...parser/resolution/logic/MethodResolutionLogic.java 0.000% 6 Missing ⚠️
...er/symbolsolver/javaparsermodel/TypeExtractor.java 66.666% 1 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff               @@
##              master     #4752       +/-   ##
===============================================
- Coverage     58.324%   58.319%   -0.006%     
- Complexity      2509      2511        +2     
===============================================
  Files            671       671               
  Lines          38764     38778       +14     
  Branches        7034      7039        +5     
===============================================
+ Hits           22609     22615        +6     
- Misses         13264     13271        +7     
- Partials        2891      2892        +1     
Flag Coverage Δ
AlsoSlowTests 58.319% <52.941%> (-0.006%) ⬇️
javaparser-core 58.319% <52.941%> (-0.006%) ⬇️
javaparser-symbol-solver 58.319% <52.941%> (-0.006%) ⬇️
jdk-10 57.860% <52.941%> (-0.016%) ⬇️
jdk-11 57.869% <52.941%> (-0.006%) ⬇️
jdk-12 57.859% <52.941%> (-0.016%) ⬇️
jdk-13 57.869% <52.941%> (-0.006%) ⬇️
jdk-14 58.106% <52.941%> (-0.016%) ⬇️
jdk-15 58.116% <52.941%> (-0.006%) ⬇️
jdk-16 58.091% <52.941%> (-0.006%) ⬇️
jdk-17 58.245% <52.941%> (-0.006%) ⬇️
jdk-18 58.245% <52.941%> (-0.006%) ⬇️
jdk-8 57.871% <52.941%> (-0.006%) ⬇️
jdk-9 57.860% <52.941%> (-0.014%) ⬇️
macos-latest 58.311% <52.941%> (-0.006%) ⬇️
ubuntu-latest 58.295% <52.941%> (-0.016%) ⬇️
windows-latest 58.301% <52.941%> (-0.006%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...er/javaparsermodel/contexts/LambdaExprContext.java 76.033% <100.000%> (+0.819%) ⬆️
...er/symbolsolver/javaparsermodel/TypeExtractor.java 71.476% <66.666%> (-0.293%) ⬇️
...parser/resolution/logic/MethodResolutionLogic.java 0.000% <0.000%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 32c7997...fd89ae0. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented May 12, 2025

That sounds fine to me. Thank you for your PR.

@jlerbsc jlerbsc merged commit f478dc2 into javaparser:master May 12, 2025
34 of 35 checks passed
@jlerbsc jlerbsc added this to the next release milestone May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants