Skip to content

Implement switch expr type extraction#4968

Merged
jlerbsc merged 3 commits into
javaparser:masterfrom
mrjameshamilton:jhamilton/switch-expr-type
Feb 4, 2026
Merged

Implement switch expr type extraction#4968
jlerbsc merged 3 commits into
javaparser:masterfrom
mrjameshamilton:jhamilton/switch-expr-type

Conversation

@mrjameshamilton

@mrjameshamilton mrjameshamilton commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

I came across an UnsupportedOperationException like the following when experimenting with parsing switch expressions:

java.lang.UnsupportedOperationException: com.github.javaparser.ast.expr.SwitchExpr
	at com.github.javaparser.symbolsolver.javaparsermodel.DefaultVisitorAdapter.visit(DefaultVisitorAdapter.java:530)
	at com.github.javaparser.symbolsolver.javaparsermodel.DefaultVisitorAdapter.visit(DefaultVisitorAdapter.java:37)
	at com.github.javaparser.ast.expr.SwitchExpr.accept(SwitchExpr.java:90)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:670)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:424)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:389)
	at com.github.javaparser.symbolsolver.JavaSymbolSolver.calculateType(JavaSymbolSolver.java:443)

The cause was that the TypeExtractor did not implement the type extraction for switch expressions. For example, I had a case like this which threw the exception because the type of the method call argument (the result of the switch expression) could not be computed:

    methodCall(
        switch ("a") {
          case "a" -> 3;
          default -> 0;
        });

The PR implements:

  • Type extraction for switch expressions based on JLS 15.28.1
If the result expressions all have the same type (which may be the null type), then that is the type of the switch expression.

Otherwise, if the type of each result expression is boolean or Boolean, then an unboxing conversion (§5.1.8) is applied to each result expression of type Boolean, and the switch expression has type boolean.

Otherwise, if the type of each result expression is convertible to a numeric type ( §5.1.8), then the type of the switch expression is the result of general numeric promotion (§5.6) applied to the result expressions.

Otherwise, boxing conversion (§5.1.7 ) is applied to each result expression that has a primitive type, after which the type of the switch expression is the result of applying capture conversion (§5.1.10) to the least upper bound (§4.10.4) of the types of the result expressions.

  • Validation for switch expressions with no result expressions based on 15.28.1
It is a compile-time error if a switch expression has no result expressions.

@mrjameshamilton mrjameshamilton force-pushed the jhamilton/switch-expr-type branch from 825ecc3 to b331bd8 Compare February 3, 2026 09:18
@mrjameshamilton mrjameshamilton force-pushed the jhamilton/switch-expr-type branch from b331bd8 to c04a989 Compare February 3, 2026 09:20
@codecov

codecov Bot commented Feb 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.06250% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.716%. Comparing base (2146cfc) to head (f9c7620).
⚠️ Report is 8 commits behind head on master.

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

Impacted file tree graph

@@               Coverage Diff               @@
##              master     #4968       +/-   ##
===============================================
+ Coverage     58.657%   58.716%   +0.059%     
- Complexity      2564      2592       +28     
===============================================
  Files            699       700        +1     
  Lines          40121     40186       +65     
  Branches        7314      7323        +9     
===============================================
+ Hits           23534     23596       +62     
  Misses         13620     13620               
- Partials        2967      2970        +3     
Flag Coverage Δ
AlsoSlowTests 58.716% <89.062%> (+0.059%) ⬆️
javaparser-core 58.716% <89.062%> (+0.059%) ⬆️
javaparser-symbol-solver 58.716% <89.062%> (+0.059%) ⬆️
jdk-10 58.289% <89.062%> (+0.062%) ⬆️
jdk-11 58.288% <89.062%> (+0.060%) ⬆️
jdk-12 58.288% <89.062%> (+0.060%) ⬆️
jdk-13 58.288% <89.062%> (+0.060%) ⬆️
jdk-14 58.519% <89.062%> (+0.059%) ⬆️
jdk-15 58.519% <89.062%> (+0.059%) ⬆️
jdk-16 58.494% <89.062%> (+0.059%) ⬆️
jdk-17 58.644% <89.062%> (+0.059%) ⬆️
jdk-18 58.644% <89.062%> (+0.059%) ⬆️
jdk-8 58.125% <89.062%> (+0.062%) ⬆️
jdk-9 58.286% <89.062%> (+0.062%) ⬆️
macos-latest 58.692% <89.062%> (+0.059%) ⬆️
ubuntu-latest 58.687% <89.062%> (+0.059%) ⬆️
windows-latest 58.699% <89.062%> (+0.059%) ⬆️

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

Files with missing lines Coverage Δ
...or/language_level_validations/Java14Validator.java 100.000% <100.000%> (ø)
..._level_validations/chunks/SwitchExprValidator.java 100.000% <100.000%> (ø)
...symbolsolver/javaparsermodel/JavaParserFacade.java 75.079% <0.000%> (-0.483%) ⬇️
...er/symbolsolver/javaparsermodel/TypeExtractor.java 75.000% <90.384%> (+2.666%) ⬆️

... and 2 files 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 ecac205...f9c7620. 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 commented Feb 3, 2026

Copy link
Copy Markdown
Collaborator

This work seems absolutely excellent to me. What do you think @johannescoetzee ?
Perhaps you could rearrange some commits to make them more readable.

@johannescoetzee

Copy link
Copy Markdown
Collaborator

Looks good to me! Thanks for the contribution @mrjameshamilton

I would say the commits are fine if we squash and merge, since everything will be flattened anyways

@jlerbsc jlerbsc merged commit e6b7e7c into javaparser:master Feb 4, 2026
35 checks passed
@jlerbsc

jlerbsc commented Feb 4, 2026

Copy link
Copy Markdown
Collaborator

Thank you for your work.

@jlerbsc jlerbsc added this to the next release milestone Feb 4, 2026
@jlerbsc jlerbsc added the PR: Added A PR that introduces new behaviour (e.g. functionality, tests) label Feb 4, 2026
@mrjameshamilton mrjameshamilton deleted the jhamilton/switch-expr-type branch February 6, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Added A PR that introduces new behaviour (e.g. functionality, tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants