Repository navigation
Use ForeignFunctions arena helpers in IOKitUtilFFM and Advapi32UtilFFM - #3352
Conversation
Replace seven hand-rolled `try (Arena = Arena.ofConfined()) … catch
(Throwable) { return default; }` blocks with the existing
`callInArenaOrDefault` / `callInArenaIntOrDefault` /
`callInArenaBooleanOrDefault` helpers in `ForeignFunctions`. Centralising
the swallow keeps the catch-and-return pattern DRY and matches the
convention already used in `Mac*FFM`, `LinuxUsbDeviceFFM`, and the
`*CentralProcessorFFM` family.
The helpers log via the SLF4J fluent API. Logged at TRACE: when TRACE is
not enabled the call resolves to a single `isTraceEnabled()` check, which
the JIT inlines and the branch predictor pegs almost immediately, so the
runtime cost on the success path is effectively zero. When someone needs
to chase an unexpected null/0/false return, enabling TRACE on the
offending class surfaces the swallowed throwable — a signal we did not
have before. Messages are static literals so no string is constructed on
the success path.
Inner try/finally `RegCloseKey` blocks in `Advapi32UtilFFM` stay inside
the lambda; outer-catch sites in `IOReportClientFFM`,
`MacInternetProtocolStatsFFM`, `MacOSProcessFFM`, and `HkeyUserDataFFM`
were intentionally left alone — their catches cover scopes broader than
the Arena open, or the Arena is passed in as a parameter, so the helper
does not fit cleanly.
📝 WalkthroughWalkthroughTwo FFM platform-specific utility classes ( ChangesFFM Confined Arena Wrapper Migration
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## master #3352 +/- ##
============================================
- Coverage 74.95% 68.24% -6.71%
Complexity 76 76
============================================
Files 602 602
Lines 25845 25832 -13
Branches 3990 3984 -6
============================================
- Hits 19371 17629 -1742
- Misses 4217 6187 +1970
+ Partials 2257 2016 -241
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…tions helpers (#3355) Continuation of the centralisation work from #3352. Six more `try { ... } catch (Throwable) { return default; }` sites now route their swallow-and-return through the existing `ExceptionUtil` / `ForeignFunctions` helpers so the catch-and-default behaviour lives in one tested place per pattern: * `IOReportClientFFM.sampleGpuTicks`, `MacInternetProtocolStatsFFM.queryFdList`, and `MacInternetProtocolStatsFFM.queryIPConnection` move to `ExceptionUtil.getOrDefault` (no Arena owned at the catch site). * `HkeyUserDataFFM.lookupAccountBySid`, `MacInternetProtocolStatsFFM.getConnections`, and `MacOSProcessFFM.updateAttributes` move to `ForeignFunctions.callInArenaOrDefault` / `callInArenaBooleanOrDefault` — these are catches paired with `try (Arena = Arena.ofConfined())` that the heuristic missed in #3352. `updateAttributes` hoists its `this.state = INVALID` side-effect outside the helper so the catch arm itself stays pure. `ExceptionUtil` grows `Level`-taking overloads in parity with `ForeignFunctions.callInArenaOrDefault`'s signature. Existing `(supplier, default, log, msg)` methods become one-line delegates passing `Level.DEBUG`, preserving behaviour for the 20+ existing callers. New call sites log at `TRACE`: SLF4J's level guard collapses to a single JIT-inlined `isTraceEnabled()` check when TRACE is not configured, so the success-path cost is effectively zero, while turning TRACE on for the offending class surfaces the swallowed throwable when debugging. The fluent log call passes `t.getMessage()` as the substitution argument (matching `ForeignFunctions`'s pattern), so `{}` placeholders in caller messages render as the throwable's message string rather than its full `toString()`. No public API removal, no signature changes — JAPICMP stays quiet. `IOReportClientFFM.sampleGpuUtilization` (L167) and `samplePowerWatts` (L221) were considered but left as-is: their outer `finally { cfRelease(sample); }` cleanup with mid-body sample ownership transfer doesn't fit the helper's try/catch/default shape without a structural refactor that costs more clarity than it recovers. Folds in two unrelated fixes that surfaced from CI on the prior push: * `FileSystemTest` now skips the "total space should not change after update" assertion on FreeBSD in addition to Solaris — ZFS pools report a dynamically computed total that can fluctuate by a few MB between back-to-back queries due to pool/dataset metadata refresh. Uses an `EnumSet` named `FLUCTUATING_TOTAL_SPACE` so future platforms with the same behaviour can be added without growing the conditional. DragonFly's HAMMER2 doesn't share this property, so it's left out. A planned `**/ffm/**` jacoco exclusion (mirroring the existing `**/jna/**`) was prepared and then dropped from this PR — it accidentally drops 25 real driver classes in `oshi.ffm.driver.*` (AIX perfstat, Solaris PsInfo + kstat, Windows InstalledApps) from coverage. The underlying structure needs cleanup first: those drivers belong in `oshi.driver.*` alongside the Mac/Linux/Windows FFM drivers, and the Windows FFM mapping files (`*FFM.java`) should follow the same `*Functions.java` convention used elsewhere. Handle in a follow-up PR.
Summary
Replaces seven hand-rolled
try (Arena = Arena.ofConfined()) … catch (Throwable) { return default; }blocks acrossIOKitUtilFFM(5) andAdvapi32UtilFFM(2) with the existingcallInArenaOrDefault/callInArenaIntOrDefault/callInArenaBooleanOrDefaulthelpers inForeignFunctions. Centralises the swallow-and-return pattern so the catch-default convention lives in one place, matching howMac*FFM,LinuxUsbDeviceFFM, and the*CentralProcessorFFMfamily already do it.isTraceEnabled()check that the JIT inlines and the branch predictor pegs — the runtime cost on the success path is effectively zero. The benefit is a debug signal that previously did not exist: enabling TRACE on the offending utility class surfaces the swallowed throwable when chasing an unexpected null/0/false return.IOKitUtilFFMpicks up aprivate static final Logger LOGfield;Advapi32UtilFFMalready had one.Sites intentionally left alone after closer reading:
IOReportClientFFM(outer catches cover CFString/CFType allocations beyond the Arena, ortry (CFTypeRef)resources withfinally { cfRelease }cleanup that must run after the catch),MacInternetProtocolStatsFFMandHkeyUserDataFFM(Arena passed in as a parameter), andMacOSProcessFFML600 (inner catch inside a larger Arena scope).Test plan
./mvnw -pl oshi-core-ffm -am spotless:apply checkstyle:check install javadoc:javadoc -DskipTests— all gates pass locally./mvnw -pl oshi-core-ffm -am test— full FFM test suite green on macOS arm64Summary by CodeRabbit