Skip to content

fix: resolve constructor references via context rather than constructed type#5030

Merged
jlerbsc merged 2 commits into
javaparser:masterfrom
jlerbsc:master
May 30, 2026
Merged

fix: resolve constructor references via context rather than constructed type#5030
jlerbsc merged 2 commits into
javaparser:masterfrom
jlerbsc:master

Conversation

@jlerbsc

@jlerbsc jlerbsc commented May 30, 2026

Copy link
Copy Markdown
Collaborator

TypeExtractor previously short-circuited all ::new method references by
returning the constructed type (e.g. String for String::new). Per JLS
§15.13, a constructor reference is a poly expression: its type is the
functional interface it satisfies in context, not the type it constructs.
Remove the early return and let constructor references fall through to the
same parent-context logic used for ordinary method references:

  • In a MethodCallExpr: return the formal parameter type at the reference
    position (solveLambdas=false), or perform full inference via
    FunctionalInterfaceLogic (solveLambdas=true). The toMethodUsage()
    inference step is skipped for ::new because constructors are not
    returned by getAllMethods(); skipping is safe since a constructor always
    returns the constructed type, matching the functional interface's return
    type exactly, so the inference pair carries no new information.
  • In a VariableDeclarator: return the declared type of the variable.
  • In an AssignExpr: return the type of the left-hand side.
    Combined with the matchTypeParameters wildcard fix, this allows
    Collectors.groupingBy(String::new, Collectors.counting()) to resolve
    without throwing UnsolvedSymbolException. Full inference of K=String
    requires two-phase poly-expression inference (JLS §15.12.2.7) which is
    not yet implemented.
    Add Javadoc to visit(MethodReferenceExpr) documenting all handled cases.
    Add two regression tests covering the MethodCallExpr and VariableDeclarator
    contexts.

@jlerbsc jlerbsc merged commit fc1f17c into javaparser:master May 30, 2026
35 checks passed
@jlerbsc jlerbsc added this to the next release milestone May 30, 2026
@jlerbsc jlerbsc added the PR: Fixed A PR that offers a fix or correction label May 30, 2026
@codecov

codecov Bot commented May 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.692%. Comparing base (5874940) to head (a445b1b).
⚠️ Report is 4 commits behind head on master.

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

Impacted file tree graph

@@               Coverage Diff               @@
##              master     #5030       +/-   ##
===============================================
+ Coverage     58.686%   58.692%   +0.005%     
- Complexity      2595      2599        +4     
===============================================
  Files            702       702               
  Lines          40282     40295       +13     
  Branches        7342      7349        +7     
===============================================
+ Hits           23640     23650       +10     
  Misses         13667     13667               
- Partials        2975      2978        +3     
Flag Coverage Δ
AlsoSlowTests 58.692% <66.666%> (+0.005%) ⬆️
javaparser-core 58.692% <66.666%> (+0.005%) ⬆️
javaparser-symbol-solver 58.692% <66.666%> (+0.005%) ⬆️
jdk-10 58.263% <66.666%> (+0.008%) ⬆️
jdk-11 58.264% <66.666%> (+0.006%) ⬆️
jdk-12 58.264% <66.666%> (+0.008%) ⬆️
jdk-13 58.264% <66.666%> (+0.006%) ⬆️
jdk-14 58.495% <66.666%> (+0.005%) ⬆️
jdk-15 58.495% <66.666%> (+0.005%) ⬆️
jdk-16 58.470% <66.666%> (+0.005%) ⬆️
jdk-17 58.619% <66.666%> (+0.005%) ⬆️
jdk-18 58.619% <66.666%> (+0.005%) ⬆️
jdk-8 58.099% <66.666%> (+0.006%) ⬆️
jdk-9 58.263% <66.666%> (+0.006%) ⬆️
macos-latest 58.667% <66.666%> (+0.005%) ⬆️
ubuntu-latest 58.662% <66.666%> (+0.005%) ⬆️
windows-latest 58.674% <66.666%> (+0.005%) ⬆️

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 74.798% <66.666%> (-0.757%) ⬇️

... and 1 file with indirect coverage changes


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 ed82314...a445b1b. Read the comment docs.

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

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.

1 participant