Skip to content

Fix WrongMethodTypeException for void invokeExact in expression lambdas - #3302

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:fix/void-invokeExact-lambda-3301
May 25, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:fix/void-invokeExact-lambda-3301

Conversation

@dbwiddis

@dbwiddis dbwiddis commented May 25, 2026 •

Copy link
Copy Markdown
Member

Summary

MethodHandle.invokeExact() is signature-polymorphic: its return type is inferred from the call-site context. In an expression lambda passed to a void-returning functional interface (ThrowingRunnable), the compiler infers Object as the return type instead of void, causing a WrongMethodTypeException at runtime.

Fix

Convert expression lambdas to block lambdas so invokeExact() is in a statement context where its return type is correctly inferred as void.

Affected methods

Why CI didn't catch it

The runOrLog() helper catches Throwable and logs at DEBUG level, so the test passed silently while the COM cleanup actually failed.

Fixes #3301

Summary by CodeRabbit

  • Style
    • Improved code formatting in internal COM interop utilities for better readability and consistency.

Review Change Stack

MethodHandle.invokeExact() is signature-polymorphic: its return type is
inferred from the call-site context. In an expression lambda passed to a
void-returning functional interface, the compiler infers Object as the
return type instead of void, causing a WrongMethodTypeException at
runtime.

Convert expression lambdas to block lambdas so invokeExact() is in a
statement context where its return type is correctly inferred as void.

Fixes #3301
@coderabbitai

coderabbitai Bot commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: c6d1daa4-d952-46e1-881c-de7eea49b084

📥 Commits

Reviewing files that changed from the base of the PR and between a85ad0c and 00c0ba0.

📒 Files selected for processing (2)
  • oshi-core-ffm/src/main/java/oshi/ffm/windows/com/BStrFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/windows/com/Ole32FFM.java
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Analyze (java)
  • GitHub Check: JMH Benchmarks, macos-latest
  • GitHub Check: JMH Benchmarks, windows-latest
  • GitHub Check: JMH Benchmarks, ubuntu-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
🔇 Additional comments (2)
oshi-core-ffm/src/main/java/oshi/ffm/windows/com/BStrFFM.java (1)

72-74: LGTM!

oshi-core-ffm/src/main/java/oshi/ffm/windows/com/Ole32FFM.java (1)

98-100: LGTM!


📝 Walkthrough

Walkthrough

Two COM wrapper methods in the FFM API now use multi-line block-bodied lambdas instead of single-line expressions when invoking method handles through runOrLog. The refactoring clarifies return type signatures for SysFreeString.invokeExact() and CoUninitialize.invokeExact() without changing behavior or logging.

Changes

COM FFM Lambda Block Formatting

Layer / File(s) Summary
Multi-line lambda blocks in COM method handles
oshi-core-ffm/src/main/java/oshi/ffm/windows/com/BStrFFM.java, oshi-core-ffm/src/main/java/oshi/ffm/windows/com/Ole32FFM.java
BStrFFM and Ole32FFM convert single-line lambdas in runOrLog calls to explicit multi-line block-bodied lambdas, clarifying return type signatures for method handle invocations without altering error logging or control flow.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A lambda's shape now clear and bright,
With blocks that show the method's type just right,
No tricks, no guile, just void returned,
COM calls work smoothly—lesson learned! 🌿

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Title accurately describes the core fix: converting expression lambdas to block lambdas to resolve WrongMethodTypeException for void invokeExact calls.
Linked Issues check ✅ Passed PR successfully addresses issue #3301 by converting expression lambdas to block lambdas in Ole32FFM.CoUninitialize and BStrFFM.free to fix the WrongMethodTypeException caused by invokeExact return type inference.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the WrongMethodTypeException issue in the two affected FFM methods; no unrelated modifications present.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented May 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.05%. Comparing base (31b3612) to head (00c0ba0).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3302      +/-   ##
============================================
- Coverage     76.19%   76.05%   -0.14%     
  Complexity       76       76              
============================================
  Files           410      410              
  Lines         15830    15830              
  Branches       2546     2546              
============================================
- Hits          12061    12039      -22     
- Misses         2274     2286      +12     
- Partials       1495     1505      +10     
Flag Coverage Δ
linux 40.25% <ø> (-0.03%) ⬇️
macos 40.82% <ø> (ø)
windows 48.98% <ø> (-0.11%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

@dbwiddis
dbwiddis merged commit baf208c into oshi:master May 25, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the fix/void-invokeExact-lambda-3301 branch May 25, 2026 06:50
dbwiddis added a commit that referenced this pull request Jul 1, 2026
…#3424)

* Fix WrongMethodTypeException in BStrFFM.free void invokeExact (#3422)

SysFreeString is a void downcall, so its handle type is (MemorySegment)void.
MethodHandle.invokeExact is signature-polymorphic and infers its return type
from the call site; in an expression lambda passed to the void-returning
runOrLog(ThrowingRunnable), javac infers Object instead of void, so the call
site's (MemorySegment)Object does not match the handle and throws
WrongMethodTypeException at runtime (swallowed at DEBUG, leaking the BSTR).

Convert to a block lambda so invokeExact is in a statement context and its
return type is inferred as void. This reinstates the fix from #3302, which was
lost when the FFM code moved from oshi.ffm.windows.com to
oshi.ffm.platform.windows.com; a comment now marks why the block form matters.

Fixes #3422

* Add source-policy test guarding against void invokeExact regression

The WrongMethodTypeException from a void invokeExact in an expression lambda
only surfaces on the native platform and is swallowed by runOrLog, so no
ordinary unit test can observe it -- and it has already regressed twice when
the FFM code was moved between packages (#3301, #3422).

VoidInvokeExactSourceTest scans oshi-core-ffm's main sources for the
expression-lambda form and fails with the offending file:line, so the
regression is caught on every build (including non-Windows CI). Verified it
fails on the pre-fix form and passes on the block-lambda fix.

* Suppress S1602 on BStrFFM.free block lambda; scan multi-line; CHANGELOG

- Add // NOSONAR (java:S1602) on the BStrFFM.free block lambda so a future
  Sonar S1602 sweep can't collapse it back to the expression form that
  reintroduced #3422. It is the only single-statement void invokeExact block
  lambda; the other runOrLog/runSilently blocks are multi-statement, and
  Ole32FFM.CoUninitialize is a safe no-arg method reference.
- VoidInvokeExactSourceTest now scans whole-file content (bounded to a single
  statement with [^{};]*) so an expression lambda wrapped across lines is also
  caught, not just single-line occurrences.
- Add CHANGELOG entry.
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.

handle's method type ()void but found ()Object on windows when getFileStores with FFM

1 participant