Skip to content

Sonar JDK-modernization sweep (S1481, S1117, S6201, S6208, S6880) - #3393

Merged
dbwiddis merged 5 commits into
oshi:masterfrom
dbwiddis:sonar-ffm-modernization
Jun 13, 2026
Merged

dbwiddis merged 5 commits into
oshi:masterfrom
dbwiddis:sonar-ffm-modernization

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 13, 2026 •

Copy link
Copy Markdown
Member

Mechanical SonarCloud modernization, one commit per rule. No behavior change; verified locally with a clean install -P native-comparison (NativeComparisonTest green) plus checkstyle/forbiddenapis/javadoc on the touched modules.

Commits

  • S1481 — close-only FFM RAII wrappers (CFTypeRef, NativeHandle) wrapped in try-with-resources purely to run the native release on close() were flagged as unused locals. Replaced their names with the JDK 22 unnamed variable _ (oshi-core-ffm is release 25) and added a short comment noting the cleanup-only intent. Deleting them, as the rule literally suggests, would leak the native objects. One genuine stray case (a CoreFoundationTest Arena never allocated from) is removed outright.
  • S1117 — three name-shadowing fixes: LinuxOSProcess ctxt-switch locals assigned straight to their fields; MemoizerTest local ex → executor; eight OperatingSystemTest method locals os → system. Pure renames.
  • S6201 — five instanceof X + cast pairs → pattern-matching instanceof X binding (JDK 16).
  • S6208 — consecutive fall-through case labels merged into comma-separated labels (JDK 14, colon form).
  • S6880 — four single-variable if/else cascades → switch (JDK 14+ arrow form). Two are clean value-producing switch expressions; the two MacInternetProtocolStatsFFM chains assign several locals with an early return, so they become arrow switch statements (verified on macOS via NativeComparisonTest).

All changes are confined to oshi-core-ffm except the S1117 renames (oshi-common / oshi-core). A separate follow-up will cover the java8-tagged findings (project-wide, release 8/9 modules).

Summary by CodeRabbit

  • Refactor

    • Improved code patterns throughout multiple modules for better maintainability and consistency.
    • Modernized Java syntax usage across platform-specific drivers and utilities.
    • Streamlined native resource management handling.
  • Tests

    • Updated test implementations to use fresh instances for improved isolation.
    • Removed unnecessary test infrastructure patterns.

dbwiddis added 5 commits June 13, 2026 11:43
The FFM port wraps native handles/pointers in AutoCloseable RAII helpers
(CFTypeRef, NativeHandle) inside try-with-resources purely so close() runs the
native release (CFRelease, udev_unref, CloseHandle, ...). The body uses the raw
segment, never the wrapper, so Sonar S1481 flagged the wrapper as an unused
local. Replace those names with the Java 22 unnamed variable '_' (oshi-core-ffm
is release 25) and add a short comment noting the cleanup-only intent. Deleting
them, as the rule literally suggests, would leak the native objects.

One genuine case differed: CoreFoundationTest opened an Arena it never allocated
from, so that one is removed outright rather than renamed. Left untouched the
named handles that ARE read in their bodies (e.g. WindowsOperatingSystemFFM
hToken). No behavior change.
Three independent name-shadowing fixes flagged by Sonar S1117:

- LinuxOSProcess: the two ctxt-switch locals shadowed the same-named fields
  and added nothing, so assign the parsed values straight to the fields.
- MemoizerTest: the ThreadPoolExecutor local 'ex' shadowed the ExecutorService
  field 'ex'; rename the local to 'executor'.
- OperatingSystemTest: eight test methods declared a local 'os' shadowing the
  shared 'os' field; rename those locals to 'system'. Pure rename, no change to
  which instance each test uses (the locals were createOperatingSystem() calls,
  not the field).

No behavior change.
Collapse the five 'instanceof X' + explicit cast pairs in the FFM port into
'instanceof X binding' form (JDK 16; oshi-core-ffm is release 25). No behavior
change.
Merge consecutive fall-through 'case' labels into single comma-separated labels
(JDK 14 multi-label cases, colon form). Purely a label-syntax change; the
switch bodies and fall-through semantics are unchanged.
Convert four single-variable if/else cascades to switch (JDK 14+ arrow form):
- WindowsCentralProcessorFFM: clean value-producing switch expression for freqs.
- HkeyPerformanceDataUtilFFM: switch expression yielding the counter value, then
  put/return null as before (the default arm is the not-in-registry sentinel).
- MacInternetProtocolStatsFFM (x2): the branches assign several locals and have
  an early return, so these become arrow switch statements rather than
  expressions. Verified on macOS via NativeComparisonTest.

No behavior change.
@coderabbitai

coderabbitai Bot commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

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: 33db0837-f581-40e8-9807-0a00087cf835

📥 Commits

Reviewing files that changed from the base of the PR and between a2a14c6 and 9845752.

📒 Files selected for processing (29)
  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSProcess.java
  • oshi-common/src/test/java/oshi/util/MemoizerTest.java
  • oshi-core-ffm/src/main/java/oshi/driver/mac/IOReportClientFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/windows/perfmon/PerfmonDisabledFFM.java
  • oshi-core-ffm/src/main/java/oshi/driver/windows/registry/HkeyPerformanceDataUtilFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/platform/mac/IOKit.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/solaris/KstatUtilFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/IPHlpAPIUtilFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WbemcliUtilFFM.java
  • oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WmiUtilFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxCentralProcessorFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHWDiskStoreFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxLogicalVolumeGroupFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxNetworkIFFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsBluetoothDeviceFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsDisplayFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsGraphicsCardFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsPowerSourceFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsPrinterFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsSoundCardFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.java
  • oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.java
  • oshi-core-ffm/src/test/java/oshi/ffm/platform/mac/CoreFoundationTest.java
  • oshi-core/src/test/java/oshi/software/os/shared/OperatingSystemTest.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). (11)
  • 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: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (3)
📚 Learning: 2026-04-08T00:58:07.212Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3125
File: oshi-core-java25/src/main/java/oshi/hardware/platform/windows/WindowsPowerSourceFFM.java:131-133
Timestamp: 2026-04-08T00:58:07.212Z
Learning: Do not flag `getPowerSources()` as incorrect when it returns a singleton list that wraps `getPowerSource("System Battery")` and/or short-circuits the enumeration after the first eligible battery pack in the Windows power source JNA/FFM implementations. This is intentional for longstanding JNA behavior; a refactor to support zero-or-more batteries is planned later, so the current single-battery behavior should be treated as by-design rather than a bug.

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsPowerSourceFFM.java
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.

Applied to files:

  • oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSProcess.java
📚 Learning: 2026-04-05T15:37:16.042Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java:59-65
Timestamp: 2026-04-05T15:37:16.042Z
Learning: For OSHI Linux USB device implementations, the behavior of `getUsbDevices(boolean tree)` with `tree=false` is intentional: the returned flat list includes controller nodes (it is not meant to exclude them). In code review, do not flag the implementation for including controllers in flat mode; instead, only flag/update documentation/JavaDoc that incorrectly claims controllers are excluded (e.g., Javadoc text like "returns a flat list excluding controllers" should be corrected).

Applied to files:

  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
🔇 Additional comments (39)
oshi-core-ffm/src/main/java/oshi/driver/mac/IOReportClientFFM.java (1)

83-84: LGTM!

Also applies to: 120-121

oshi-core-ffm/src/main/java/oshi/ffm/platform/mac/IOKit.java (1)

263-264: LGTM!

Also applies to: 279-280, 299-300, 319-320, 339-340, 357-358

oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java (1)

63-64: LGTM!

oshi-core-ffm/src/test/java/oshi/ffm/platform/mac/CoreFoundationTest.java (1)

386-397: LGTM!

oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxCentralProcessorFFM.java (1)

87-88: LGTM!

Also applies to: 90-91, 105-106, 130-131, 133-134, 179-180, 182-183

oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxHWDiskStoreFFM.java (1)

73-74: LGTM!

Also applies to: 76-77, 91-92

oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxLogicalVolumeGroupFFM.java (1)

54-55: LGTM!

Also applies to: 57-58, 72-73

oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxNetworkIFFFM.java (1)

49-50: LGTM!

Also applies to: 53-54

oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java (1)

82-83: LGTM!

Also applies to: 85-86, 102-103

oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java (1)

60-64: LGTM!

Also applies to: 82-83

oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsBluetoothDeviceFFM.java (1)

72-79: LGTM!

Also applies to: 123-125

oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsDisplayFFM.java (1)

60-61: LGTM!

Also applies to: 77-78

oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsPowerSourceFFM.java (1)

134-135: LGTM!

Also applies to: 168-169

oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsFileSystemFFM.java (1)

161-162: LGTM!

oshi-core-ffm/src/main/java/oshi/software/os/windows/WindowsOperatingSystemFFM.java (1)

155-156: LGTM!

oshi-common/src/test/java/oshi/util/MemoizerTest.java (1)

43-48: LGTM!

oshi-core/src/test/java/oshi/software/os/shared/OperatingSystemTest.java (8)

226-261: LGTM!


281-393: LGTM!


399-408: LGTM!


412-417: LGTM!


423-435: LGTM!


442-465: LGTM!


472-485: LGTM!


492-505: LGTM!

oshi-common/src/main/java/oshi/software/common/os/linux/LinuxOSProcess.java (1)

366-367: LGTM!

oshi-core-ffm/src/main/java/oshi/driver/windows/perfmon/PerfmonDisabledFFM.java (1)

45-46: LGTM!

oshi-core-ffm/src/main/java/oshi/driver/windows/registry/HkeyPerformanceDataUtilFFM.java (1)

186-195: LGTM!

oshi-core-ffm/src/main/java/oshi/ffm/util/platform/unix/solaris/KstatUtilFFM.java (1)

277-278: LGTM!

oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/IPHlpAPIUtilFFM.java (1)

390-391: LGTM!

oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WbemcliUtilFFM.java (3)

244-246: LGTM!


247-249: LGTM!


271-273: LGTM!

oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WmiUtilFFM.java (1)

120-121: LGTM!

oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsCentralProcessorFFM.java (1)

284-288: LGTM!

oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsGraphicsCardFFM.java (1)

282-283: LGTM!

oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsPrinterFFM.java (1)

56-57: LGTM!

oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsSoundCardFFM.java (1)

76-76: LGTM!

oshi-core-ffm/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsFFM.java (2)

157-173: LGTM!


179-198: LGTM!


📝 Walkthrough

Walkthrough

This PR modernizes the codebase across multiple dimensions: Java language features (pattern matching, switch expressions, combined switch cases), resource management in CoreFoundation and native-handle contexts via unnamed try-with-resources bindings, and test refactoring to use local OS instances. Changes span driver, utility, hardware-platform, and test modules.

Changes

Systematic Java Modernization and Resource Management Refactoring

Layer / File(s) Summary
Pattern matching and switch expression modernization
oshi-core-ffm/.../PerfmonDisabledFFM.java, oshi-core-ffm/.../HkeyPerformanceDataUtilFFM.java, oshi-core-ffm/.../IOKit.java, oshi-core-ffm/.../WmiUtilFFM.java, oshi-core-ffm/.../WindowsCentralProcessorFFM.java, oshi-core-ffm/.../WindowsGraphicsCardFFM.java, oshi-core-ffm/.../WindowsPrinterFFM.java, oshi-core-ffm/.../WindowsSoundCardFFM.java, oshi-core-ffm/.../KstatUtilFFM.java, oshi-core-ffm/.../IPHlpAPIUtilFFM.java, oshi-core-ffm/.../WbemcliUtilFFM.java, oshi-core-ffm/.../MacInternetProtocolStatsFFM.java
Explicit casts after instanceof checks replaced with pattern matching, if/else dispatch chains converted to switch expressions, and duplicate switch case labels merged into single combined cases.
CoreFoundation resource management refactoring
oshi-core-ffm/.../IOReportClientFFM.java, oshi-core-ffm/.../IOKit.java, oshi-core-ffm/.../MacPowerSourceFFM.java
CFTypeRef try-with-resources wrappers migrated from named variables to unnamed var _ bindings, with inline comments documenting resource-release semantics on close.
Linux NativeHandle resource management refactoring
oshi-core-ffm/.../LinuxCentralProcessorFFM.java, oshi-core-ffm/.../LinuxHWDiskStoreFFM.java, oshi-core-ffm/.../LinuxLogicalVolumeGroupFFM.java, oshi-core-ffm/.../LinuxNetworkIFFFM.java, oshi-core-ffm/.../LinuxPowerSourceFFM.java, oshi-core-ffm/.../LinuxUsbDeviceFFM.java
Udev context, enumerate, and device handle try-with-resources wrappers converted from named NativeHandle variables to unnamed var _ bindings while preserving unref cleanup semantics.
Windows NativeHandle resource management refactoring
oshi-core-ffm/.../WindowsBluetoothDeviceFFM.java, oshi-core-ffm/.../WindowsDisplayFFM.java, oshi-core-ffm/.../WindowsPowerSourceFFM.java, oshi-core-ffm/.../WindowsFileSystemFFM.java, oshi-core-ffm/.../WindowsOperatingSystemFFM.java
Device enumeration and registry-key handle try-with-resources wrappers refactored from named to unnamed var _ bindings, with comments clarifying close-on-exit semantics.
Test refactoring and minor cleanup
oshi-core/src/test/java/.../OperatingSystemTest.java, oshi-core-ffm/src/test/java/.../CoreFoundationTest.java, oshi-common/src/test/java/.../MemoizerTest.java, oshi-common/src/main/java/.../LinuxOSProcess.java
OperatingSystemTest creates fresh local OS instances per method instead of reusing shared field, CoreFoundationTest removes confined Arena wrapper, MemoizerTest local executor variable renamed, and LinuxOSProcess context switch counts assigned directly from parsed status values.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • oshi/oshi#3293: Migrates CoreFoundation resource handling in IOReportClientFFM.java to try (var _ = new CFTypeRef(...)) wrappers.
  • oshi/oshi#3291: Refactors native handle management in Linux/Windows FFM files into try (var _ = ...)/NativeHandle-style try-with-resources.
  • oshi/oshi#3192: Updates WindowsCentralProcessorFFM.queryNTPower(int fieldIndex) frequency value logic.

A rabbit hops through the code with glee,
Pattern matching and resources set free!
Switch cases combined, handles unnamed with care,
Java modernized everywhere! 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 34.78% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: a Sonar-driven JDK-modernization sweep addressing five specific rules (S1481, S1117, S6201, S6208, S6880).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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 Jun 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 68.65672% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.02%. Comparing base (a2a14c6) to head (9845752).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...i/software/os/mac/MacInternetProtocolStatsFFM.java 69.23% 6 Missing and 2 partials ⚠️
...r/windows/registry/HkeyPerformanceDataUtilFFM.java 57.14% 1 Missing and 2 partials ⚠️
...re/platform/windows/WindowsBluetoothDeviceFFM.java 0.00% 3 Missing ⚠️
...e/platform/windows/WindowsCentralProcessorFFM.java 40.00% 2 Missing and 1 partial ⚠️
...shi/driver/windows/perfmon/PerfmonDisabledFFM.java 0.00% 1 Missing and 1 partial ⚠️
...i/hardware/platform/windows/WindowsPrinterFFM.java 50.00% 0 Missing and 1 partial ⚠️
...hardware/platform/windows/WindowsSoundCardFFM.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3393      +/-   ##
============================================
+ Coverage     68.65%   75.02%   +6.37%     
  Complexity       76       76              
============================================
  Files           579      579              
  Lines         21363    21364       +1     
  Branches       3560     3556       -4     
============================================
+ Hits          14666    16029    +1363     
+ Misses         4868     3307    -1561     
- Partials       1829     2028     +199     
Flag Coverage Δ
freebsd 26.31% <0.00%> (-0.01%) ⬇️
linux 30.91% <25.37%> (+<0.01%) ⬆️
macos 31.23% <26.86%> (+0.01%) ⬆️
openbsd 25.79% <0.00%> (-0.01%) ⬇️
solaris 27.86% <0.00%> (?)
windows 39.50% <16.41%> (-0.03%) ⬇️

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

☔ View full report in Codecov by Harness.
📢 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 9abc1ac into oshi:master Jun 13, 2026
20 checks passed
@dbwiddis
dbwiddis deleted the sonar-ffm-modernization branch June 13, 2026 19:22
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.

1 participant