Repository navigation
Route no-argument catch-log-return blocks through ExceptionUtil - #3528
Conversation
PR #3444 consolidated the try/catch-log-return-default pattern onto ExceptionUtil, but it only reached part of the codebase. A sweep of every `catch (Throwable)` in main source turns up 47 remaining blocks that log a message needing no arguments beyond the exception itself, which the existing helpers already handle with no API change. This converts 36 of them: - 21 to ExceptionUtil.getOrDefault / get{Int,Long,Boolean,Double}OrDefault / getOptionalInt / runOrLog - 15 to ForeignFunctions.callInArena{,Int,Long,Double,Boolean}OrDefault, where the body already opened a confined arena The remaining 11 cannot become lambdas, all for the same reason: the try body assigns locals declared outside it. - 7 are static-initializer library-binding blocks (AdlUtilFFM, NvmlFunctions, SystemdFunctions, UdevFunctions, LinuxLibcFunctions, Kstat2Functions, DxgiFFM), each binding 5-11 method handles into enclosing locals. - MacDisplayFFM and WindowsCentralProcessorFFM assign locals read after the try. - IWbemLocatorFFM and IWbemServicesFFM free a BSTR in a finally block that is assigned inside the try. Sites logging `("...: {}", t.getMessage())` now also attach the throwable as the SLF4J cause, so those lines gain a stack trace. That is inherent to the ExceptionUtil contract and already true of the call sites converted in #3444; this makes the rest consistent. Also fixes a pre-existing fully-qualified `oshi.util.LogLevel.WARN` in FreeBsdOSProcessFFM. Verified with a clean full-reactor build and a live run of the FFM provider on macOS covering the restructured paths: disk stores, file stores, network interfaces, displays, CPU ticks, processor identifier and power sources.
📝 WalkthroughWalkthroughFFM native integrations now use shared helpers for confined-arena lifecycle management, exception logging, and default results. The native operations, cleanup paths, sentinel values, and platform-specific behavior remain otherwise intact across macOS, Windows, Linux, FreeBSD, and Solaris code. ChangesFFM error-handling consolidation
Estimated code review effort: 4 (Complex) | ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ 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❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3528 +/- ##
============================================
+ Coverage 78.51% 78.63% +0.11%
- Complexity 3837 3958 +121
============================================
Files 698 698
Lines 24106 24061 -45
Branches 3989 3977 -12
============================================
- Hits 18927 18920 -7
+ Misses 2984 2943 -41
- Partials 2195 2198 +3
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:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@oshi-core-ffm/src/main/java/oshi/software/os/linux/LinuxOperatingSystemFFM.java`:
- Around line 106-107: The gettid fallback in the hasSyscallGettid
initialization currently uses the default DEBUG level and an unused placeholder.
Update the getBooleanOrDefault call to pass WARN explicitly and remove the
trailing "{}" from its message, preserving the existing fallback behavior and
log text.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e6b5cf4c-525d-4eb1-9bef-cc6fbf58763b
📒 Files selected for processing (20)
oshi-core-ffm/src/main/java/oshi/driver/mac/net/NetStatFFM.javaoshi-core-ffm/src/main/java/oshi/driver/windows/registry/ProcessWtsDataFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/NativeHandle.javaoshi-core-ffm/src/main/java/oshi/ffm/platform/windows/DxgiFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/platform/windows/Kernel32FFM.javaoshi-core-ffm/src/main/java/oshi/ffm/platform/windows/Shell32FFM.javaoshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/util/platform/mac/SmcUtilFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/solaris/KstatUtilFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/Advapi32UtilFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/unix/CupsPrinterFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsBluetoothDeviceFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsDisplayFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/linux/LinuxOperatingSystemFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/unix/solaris/SolarisOSProcessFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.java
📜 Review details
⏰ Context from checks skipped due to timeout. (8)
- GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
- GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
- GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.
Applied to files:
oshi-core-ffm/src/main/java/oshi/ffm/NativeHandle.javaoshi-core-ffm/src/main/java/oshi/driver/mac/net/NetStatFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/util/platform/mac/SmcUtilFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/platform/windows/Shell32FFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsDisplayFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/unix/CupsPrinterFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/solaris/KstatUtilFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsBluetoothDeviceFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/linux/LinuxOperatingSystemFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.javaoshi-core-ffm/src/main/java/oshi/driver/windows/registry/ProcessWtsDataFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/Advapi32UtilFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/platform/windows/DxgiFFM.javaoshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/platform/windows/Kernel32FFM.javaoshi-core-ffm/src/main/java/oshi/software/os/unix/solaris/SolarisOSProcessFFM.javaoshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.javaoshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java
🔇 Additional comments (23)
oshi-core-ffm/src/main/java/oshi/ffm/util/gpu/AdlUtilFFM.java (2)
10-15: LGTM!Also applies to: 189-203, 225-260, 273-281
331-349: LGTM!Also applies to: 362-379, 392-409, 422-441, 454-474
oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacCentralProcessorFFM.java (1)
84-110: LGTM!oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java (1)
7-9: LGTM!Also applies to: 250-349
oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/CupsPrinterFFM.java (1)
8-10: LGTM!Also applies to: 54-101
oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsBluetoothDeviceFFM.java (1)
11-17: LGTM!Also applies to: 64-89
oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsDisplayFFM.java (1)
9-9: LGTM!Also applies to: 92-110
oshi-core-ffm/src/main/java/oshi/software/os/mac/MacFileSystemFFM.java (1)
9-9: LGTM!Also applies to: 21-21, 68-187
oshi-core-ffm/src/main/java/oshi/driver/mac/net/NetStatFFM.java (1)
10-11: LGTM!Also applies to: 92-92, 149-150
oshi-core-ffm/src/main/java/oshi/driver/windows/registry/ProcessWtsDataFFM.java (1)
10-10: LGTM!Also applies to: 24-24, 75-75, 112-113
oshi-core-ffm/src/main/java/oshi/ffm/NativeHandle.java (1)
7-8: LGTM!Also applies to: 94-94
oshi-core-ffm/src/main/java/oshi/ffm/util/platform/mac/SmcUtilFFM.java (1)
10-10: LGTM!Also applies to: 28-28, 104-120
oshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/solaris/KstatUtilFFM.java (1)
28-30: LGTM!Also applies to: 88-104, 164-165
oshi-core-ffm/src/main/java/oshi/ffm/platform/windows/DxgiFFM.java (1)
11-12: LGTM!Also applies to: 84-135
oshi-core-ffm/src/main/java/oshi/ffm/platform/windows/Kernel32FFM.java (1)
20-20: LGTM!Also applies to: 73-77, 113-153
oshi-core-ffm/src/main/java/oshi/ffm/platform/windows/Shell32FFM.java (1)
10-11: LGTM!Also applies to: 47-88
oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/Advapi32UtilFFM.java (3)
13-13: LGTM!Also applies to: 35-36, 77-111
442-502: LGTM!
519-532: LGTM!oshi-core-ffm/src/main/java/oshi/software/os/linux/LinuxOperatingSystemFFM.java (1)
8-20: LGTM!Also applies to: 146-148, 151-152
oshi-core-ffm/src/main/java/oshi/software/os/unix/freebsd/FreeBsdOSProcessFFM.java (1)
9-17: LGTM!Also applies to: 101-101, 111-117
oshi-core-ffm/src/main/java/oshi/software/os/unix/solaris/SolarisOSProcessFFM.java (1)
7-9: LGTM!Also applies to: 68-75
oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.java (1)
10-12: LGTM!Also applies to: 92-122, 148-205
…elpers (#3530) #3528 converted the catch blocks whose log message needed no argument but the exception. The 29 that remained all log extra context - a path, a pid, a registry key - which the helpers could not express. This adds that support and converts 27 of them. ExceptionUtil gains `Object... args` on its 18 logging overloads in place, adding no new methods: `msg` was already the last parameter, so every existing call site still compiles. Caller arguments fill the leading placeholders in order and the exception message fills a final one, so a message carries one {} per argument plus a trailing one. ForeignFunctions cannot do the same, because callInArena* takes the default value last and leaves no room for a trailing varargs. It gains five overloads that take the default value *second*, matching ExceptionUtil's (operation, defaultValue, log, level, msg, args...) order. Second rather than merely swapped with the message on purpose. With the default fourth, a no-argument call whose T is String - reachable, e.g. KstatUtilFFM.dataLookupString - would match both overloads, and Java resolves fixed-arity before varargs, so it would silently bind to the older one with message and default transposed. Putting the default second makes argument three a Logger in one form and a LogLevel in the other, so they cannot collide at any arity. ForeignFunctionsTest covers this. Two sites could not be converted: - WindowsGraphicsCardFFM.getGraphicsCards: the body both `continue`s the enclosing loop and increments an enclosing local. - PerfDataUtilFFM.queryWildcardCounters: the nested add-counter catch aborts the whole method, which a helper cannot express from inside the enclosing lambda. WindowsOSProcessFFM.queryUserInfo and queryGroupInfo assigned an enclosing local read after the try. They were near-identical twins, so both now delegate to one queryTokenAccountInfo method whose lambda returns the pair directly. Verified with a clean full-reactor build and a live run of the FFM provider on macOS. Checked en0 still resolves to "Wi-Fi" via SCNetworkInterface rather than falling back to the raw interface name, which is what a broken conversion of MacNetworkIfFFM.queryIfDisplayName would have looked like.
* Clear the open SonarQube findings Eight are fixed in code and ten are suppressed with an inline reason. Fixed: - S1128 x2: DxgiFFM and Shell32FFM extend ForeignFunctions and WindowsForeignFunctions respectively, so they inherit callInArenaOrDefault and the static import is redundant. Spotless cannot detect this because the bare name is textually referenced. - S135 x2: restructure the SmcKeyIndex scan loop and parseConfiguredKeys as if/else. Behaviour is unchanged. - S6353: write the GPU key mask as ^Tg\d[\dA-Za-z]$. Without UNICODE_CHARACTER_CLASS this is identical to the [0-9] form. - S1192: SysctlFFM concatenated its failure message eagerly at all five call sites, formatting the sysctl name on every call whether or not it failed. Use the parameterized callInArena*OrDefault overloads with a single shared constant so the name is only formatted on failure. - S1602: add the block-lambda guard comment to AdlUtilFFM.adlUninit, matching BStrFFM. Collapsing the block would reintroduce #3422. Suppressed: - S1168 x5: null and empty are different answers. Empty is a cacheable "this machine has no GPU temperature keys"; returning it after a failed read would cache the failure and disable GPU temperature for the lifetime of the JVM. - S3077 x2: gpuTemperatureKeys is only ever assigned an unmodifiable copy of a completed discovery, so the volatile write safely publishes an immutable value. Memoizer carries the same suppression. - S5786 x3: the module system only opens public types to the JUnit platform, so public on these test classes is required rather than redundant. SystemInfoTest carries the same suppression. The four remaining S3776 findings are accepted in SonarCloud rather than refactored. Three of them are an artifact of the ExceptionUtil and callInArena conversions in #3528 and #3530: moving a method body into a callInArenaOrDefault lambda adds a nesting level that Sonar scores, without changing the logic. * Do not report an unreadable SMC key block as a confirmed absence findKeys distinguishes null ("could not read, ask again later") from an empty list ("this machine has no such keys"), because the caller caches the empty list and would otherwise disable the sensor for the lifetime of the JVM. Two paths could reach empty after a failed read. In the forward scan, readAny was satisfied by the key that ends the block rather than by a match, so an index of [TB0T, Tg0f, Th00] whose Tg0f is unreadable returned an empty list: the scan read Th00, set readAny, and broke out having matched nothing. In the binary search, probe substitutes a neighbouring key for an unreadable one so a single bad index cannot abort discovery. That substitution can move the landing point past the block, so for [TB0T, TCMb, Tg0f, Th00] with Tg0f unreadable the scan never attempted the only matching key, and no scan-side check could have caught it. Track failed reads through a single wrapper around the lookup function so both paths are covered, and return null when nothing matched and any read failed. An empty result from a run with no failed read is still returned and still cacheable, and a partial result is still returned, so the existing skip-rather-than-stop behaviour is unchanged. * Make the skipped-block test prove the scan never reached the key testFailedSearchProbeThatSkipsTheBlockIsNotReportedAsAbsent failed the sole matching index permanently, so a null result did not distinguish the case it names -- the binary search substituting a neighbour and landing past the block -- from the scan reaching that index and failing both its reads. Both produce null. Fail only the first read, which the search consumes. A null result now proves the scan never attempted the index, because the scan retries once and would have recovered Tg0f and returned a non-empty list. This also separates the test from testFailedSoleMatchIsNotReportedAsAbsent, which keeps a permanently failing index.
#3444 consolidated the try/catch-log-return-default pattern onto
ExceptionUtil, but it only reached part of the codebase. Sweeping everycatch (Throwable)in main source turns up 47 blocks still hand-rolling it with a message that needs no arguments beyond the exception — so the existing helpers cover them with no API change.This converts 36. No user-facing change, so no CHANGELOG entry.
What moved where
ExceptionUtil.getOrDefault/get{Int,Long,Boolean,Double}OrDefault/getOptionalInt/runOrLogForeignFunctions.callInArena{,Int,Long,Double,Boolean}OrDefault, where the body already opened a confined arenaMost are a straight unwrap. A few restructure slightly:
Advapi32UtilFFM.querySystemBootTime— the three "no boot event found" paths (empty log, no matching record, throwable) all now return0Land share the single uptime fallback, instead of duplicating the fallback expression.LinuxOperatingSystemFFM.getThreadId— usesgetOptionalIntrather than a sentinel, so a successfulgettidof any value is still distinguishable from a failure that should fall through to procfs.WindowsBluetoothDeviceFFM/NetStatFFM/CupsPrinterFFM/MacFileSystemFFM/ProcessWtsDataFFM— the accumulator is passed as the default, so a partial result on failure is preserved exactly as before.SmcUtilFFM.smcOpenandMacHWDiskStoreFFM.getDiskskeep an outertry/finallyfor their native cleanup, sincecallInArena*has nofinallyhook.The 11 that stay
All for one reason: the try body assigns locals declared outside it, which a lambda cannot do.
AdlUtilFFM,NvmlFunctions,SystemdFunctions,UdevFunctions,LinuxLibcFunctions,Kstat2Functions,DxgiFFM. Each binds 5–11 method handles into enclosing locals.MacDisplayFFM.synthesizeandWindowsCentralProcessorFFM.queryProcessorIdassign locals read after the try.IWbemLocatorFFM.connectServerandIWbemServicesFFM.execQueryfree a BSTR in afinallythat is assigned inside the try; converting needs an array holder, which reads worse than what is there.One behavior change
Sites that logged
("...: {}", t.getMessage())now also attach the throwable as the SLF4J cause, so those lines gain a stack trace. That is inherent to theExceptionUtilcontract and already true of every call site converted in #3444 — this makes the remainder consistent rather than introducing something new. All affected sites aredebug/trace/warn.Also fixes a pre-existing fully-qualified
oshi.util.LogLevel.WARNinFreeBsdOSProcessFFM.Verification
Clean full-reactor
install: 1122 + 84 + 67 + 30 tests, 0 failures. Spotless, checkstyle, forbidden-apis and javadoc all clean.Because several conversions restructure control flow rather than just swapping a wrapper, I also ran the FFM provider live on macOS (M2 Max) over the affected paths — disk stores, file stores, network interfaces, displays, CPU tick array, processor identifier and power sources all return real data.
Summary by CodeRabbit