Skip to content

fix: resolve constructor parameter type for lambda in ObjectCreationExpr#5024

Merged
jlerbsc merged 1 commit into
javaparser:masterfrom
jlerbsc:master
May 26, 2026
Merged

fix: resolve constructor parameter type for lambda in ObjectCreationExpr#5024
jlerbsc merged 1 commit into
javaparser:masterfrom
jlerbsc:master

Conversation

@jlerbsc

@jlerbsc jlerbsc commented May 26, 2026

Copy link
Copy Markdown
Collaborator

Fixes #3626 .

When a LambdaExpr is passed as an argument to a constructor
(e.g. new Thread(() -> ...)), TypeExtractor.visit(LambdaExpr)
was calling expr.getType().resolve() on the ObjectCreationExpr,
which returned the type of the constructed object (e.g. Thread)
instead of the functional-interface type expected at that
constructor parameter position (e.g. Runnable).
Because Thread is not itself a functional interface, resolveLambda()
could not find a functional method on it, producing a wrong result.
The fix mirrors the existing MethodCallExpr handling: look up the
argument position of the lambda, resolve the constructor via
facade.solve(expr), then call
MethodResolutionLogic.getMethodsExplicitAndVariadicParameterType()
on the ResolvedConstructorDeclaration (which extends
ResolvedMethodLikeDeclaration) to obtain the correct parameter type.
Two regression tests are added to LambdaResolutionTest:

  • lambdaTypeInObjectCreationExpr_builtinConstructor
    new Thread(() -> ...) → java.lang.Runnable
  • lambdaTypeInObjectCreationExpr_customFunctionalInterface
    new MyTask(() -> 42) → MyTask.Task

@codecov

codecov Bot commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 44.44444% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.709%. Comparing base (2c5d41f) to head (6ad1e57).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...er/symbolsolver/javaparsermodel/TypeExtractor.java 44.444% 3 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff               @@
##              master     #5024       +/-   ##
===============================================
+ Coverage     58.701%   58.709%   +0.008%     
- Complexity      2592      2593        +1     
===============================================
  Files            702       702               
  Lines          40224     40232        +8     
  Branches        7329      7331        +2     
===============================================
+ Hits           23612     23620        +8     
+ Misses         13641     13638        -3     
- Partials        2971      2974        +3     
Flag Coverage Δ
AlsoSlowTests 58.709% <44.444%> (+0.008%) ⬆️
javaparser-core 58.709% <44.444%> (+0.008%) ⬆️
javaparser-symbol-solver 58.709% <44.444%> (+0.008%) ⬆️
jdk-10 58.282% <44.444%> (+0.010%) ⬆️
jdk-11 58.281% <44.444%> (+0.010%) ⬆️
jdk-12 58.278% <44.444%> (+0.005%) ⬆️
jdk-13 58.281% <44.444%> (+0.008%) ⬆️
jdk-14 58.510% <44.444%> (+0.005%) ⬆️
jdk-15 58.512% <44.444%> (+0.008%) ⬆️
jdk-16 58.487% <44.444%> (+0.008%) ⬆️
jdk-17 58.634% <44.444%> (+0.005%) ⬆️
jdk-18 58.636% <44.444%> (+0.010%) ⬆️
jdk-8 58.118% <44.444%> (+0.008%) ⬆️
jdk-9 58.279% <44.444%> (+0.008%) ⬆️
macos-latest 58.684% <44.444%> (+0.008%) ⬆️
ubuntu-latest 58.679% <44.444%> (+0.008%) ⬆️
windows-latest 58.692% <44.444%> (+0.008%) ⬆️

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

Files with missing lines Coverage Δ
...er/symbolsolver/javaparsermodel/TypeExtractor.java 75.555% <44.444%> (+0.555%) ⬆️

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 2e56ea6...6ad1e57. Read the comment docs.

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

@jlerbsc jlerbsc merged commit b51d895 into javaparser:master May 26, 2026
62 of 64 checks passed
@jlerbsc jlerbsc added this to the next release milestone May 26, 2026
@jlerbsc jlerbsc added the PR: Fixed A PR that offers a fix or correction label May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Fixed A PR that offers a fix or correction

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ObjectCreationExpr as parent node of LambdaExpr not supported

1 participant