Skip to content

Sonar cleanup: demo MBean stubs (S1168), deprecations (S1874), duplicated literals (S1192) - #3396

Merged
dbwiddis merged 3 commits into
oshi:masterfrom
dbwiddis:sonar-s1168-demo-deprecations
Jun 15, 2026
Merged

dbwiddis merged 3 commits into
oshi:masterfrom
dbwiddis:sonar-s1168-demo-deprecations

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jun 15, 2026 •

Copy link
Copy Markdown
Member

Targeted Sonar fixes, one commit per concern. No behavior change to production query results; verified locally with a clean install -P aggregate-coverage,native-comparison (full unit suite + NativeComparisonTest green — the latter exercises the page-size path on macOS) plus checkstyle/forbiddenapis/javadoc.

Fixed

  • S1168 (6) — JMXOshiAgentServer / Baseboard demo MBean stubs returned null from interface methods that contractually return collections; return empty collections instead. No sentinel meaning to preserve.
  • S1874 (5)
    • MacGlobalMemoryJNA.queryPageSize → the non-deprecated SystemB.host_page_size(int, NativeLongByReference) overload (JNA 5.19, already on the classpath) — native-width vm_size_t out-param instead of a fixed 64-bit LongByReference.
    • 3 deserialize(...) overrides implement deprecated JMX MBeanServer methods → marked @Deprecated (truthful, and silences the javac warning too).
    • NetBsdOperatingSystem.getThreadId uses Thread.getId(); the replacement Thread.threadId() needs Java 19 but this module compiles to Java 8, so it's suppressed in code with a comment.
  • S1192 (5) — extracted constants for the repeated plist XML tags (MacInstalledApps) and 0x0000 default PCI id (BsdGraphicsCard).

Context: the rest of these rules were triaged separately

The bulk of S1168 (19), all of S107 (8), and 23 S1192 were evaluated and accepted as won't-fix with reasons — notably, OSHI's native code deliberately uses null = "native call failed/unavailable" vs. empty = "succeeded, zero results" to drive retry/fallback logic (e.g. AdlUtil/NvmlUtil return null on driver error but emptyMap for zero adapters; LinuxFileSystem null triggers the Java File fallback). Returning empty there would break that logic, so those are intentional sentinels, not bugs.

Summary by CodeRabbit

  • Refactor

    • Improved code maintainability through constant extraction and cleanup across multiple components
    • Corrected JNA binding for macOS page-size queries to use native-long references
    • Standardized null-return handling in JMX components to return empty collections instead
  • Deprecations

    • Marked two deserialization methods in JMX agent server as deprecated

dbwiddis added 3 commits June 14, 2026 17:38
These demo MBeanServer/DynamicMBean stubs returned null from interface methods
that contractually return collections; return empty collections instead (S1168),
which is the correct interface behavior and has no sentinel meaning to preserve.

The three deserialize(...) overrides implement deprecated JMX MBeanServer methods;
mark them @deprecated (S1874). That's the truthful fix -- the overrides are
deprecated-by-inheritance -- and it also silences the javac deprecation warning
that suppressing the Sonar rule alone would leave behind.
MacGlobalMemoryJNA.queryPageSize used the deprecated SystemB.host_page_size(int,
LongByReference) overload. JNA 5.19 (already on OSHI's classpath) added a
non-deprecated host_page_size(int, NativeLongByReference) variant that uses the
native-width type for the vm_size_t out-param instead of a fixed 64-bit
LongByReference; switch to it via the existing CloseableNativeLongByReference.

NetBsdOperatingSystem.getThreadId uses the deprecated Thread.getId(). The
replacement Thread.threadId() requires Java 19, but this module compiles to Java
8, so the deprecation can't be removed here; suppress it in code with an
explanatory comment.
Hoist the repeated plist XML tags (<string>/</string>/<array>/</array>) in
MacInstalledApps and the '0x0000' default PCI id in BsdGraphicsCard into named
constants. No behavior change. The remaining S1192 findings (native struct
field-name strings, per-platform messages) are left as-is -- a shared constant
there would couple unrelated code for no readability gain.
@coderabbitai

coderabbitai Bot commented Jun 15, 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: 49c4311d-2e12-4fa4-964d-886f96a4eae2

📥 Commits

Reviewing files that changed from the base of the PR and between 40ec2a0 and 61d6fb1.

📒 Files selected for processing (6)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdGraphicsCard.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java
  • oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOperatingSystem.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGlobalMemoryJNA.java
  • oshi-demo/src/main/java/oshi/demo/jmx/JMXOshiAgentServer.java
  • oshi-demo/src/main/java/oshi/demo/jmx/mbeans/Baseboard.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). (12)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • 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: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (2)
📚 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/unix/netbsd/NetBsdOperatingSystem.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdGraphicsCard.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java
📚 Learning: 2026-04-06T04:45:47.644Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3121
File: oshi-core/src/main/java/oshi/software/os/linux/LinuxOperatingSystem.java:53-53
Timestamp: 2026-04-06T04:45:47.644Z
Learning: In the oshi/oshi project, only interfaces and classes in the root packages `oshi`, `oshi.hardware`, and `oshi.software.os` are considered the public API. Code in subpackages that represent specific implementations/platforms (e.g., `oshi.software.os.linux`, `oshi.hardware.platform.linux`, and similar subpackages) is implementation detail. When reviewing, do not treat changes to classes in these platform-specific subpackages as public API breaks (for example: making `LinuxOperatingSystem` abstract, narrowing constructor visibility, or adding/removing abstract methods should not be flagged as breaking public API).

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGlobalMemoryJNA.java
🔇 Additional comments (6)
oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdGraphicsCard.java (1)

26-26: LGTM!

Also applies to: 54-55, 87-87

oshi-common/src/main/java/oshi/software/common/os/mac/MacInstalledApps.java (1)

37-40: LGTM!

Also applies to: 154-154, 159-159, 192-195, 200-201, 204-204, 219-220, 298-301

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

17-17: LGTM!

Also applies to: 47-47, 50-50

oshi-demo/src/main/java/oshi/demo/jmx/JMXOshiAgentServer.java (1)

10-10: LGTM!

Also applies to: 189-195, 216-216, 228-228, 318-318, 325-325, 332-332

oshi-demo/src/main/java/oshi/demo/jmx/mbeans/Baseboard.java (1)

102-103: LGTM!

Also applies to: 107-108

oshi-common/src/main/java/oshi/software/common/os/unix/netbsd/NetBsdOperatingSystem.java (1)

98-100: LGTM!


📝 Walkthrough

Walkthrough

Multiple small code quality improvements across six files: BsdGraphicsCard and MacInstalledApps replace repeated string literals with named constants; MacGlobalMemoryJNA corrects a JNA page-size binding from CloseableLongByReference to CloseableNativeLongByReference; JMXOshiAgentServer and Baseboard replace null returns with empty collections and mark deprecated methods; NetBsdOperatingSystem adds a @SuppressWarnings annotation.

Changes

Code Quality and Correctness Fixes

Layer / File(s) Summary
Replace magic literals with named constants
oshi-common/.../unix/BsdGraphicsCard.java, oshi-common/.../os/mac/MacInstalledApps.java
BsdGraphicsCard adds UNKNOWN_PCI_ID = "0x0000" and uses it in two card-construction call sites. MacInstalledApps adds four TAG_* constants for <string> and <array> open/close tags and applies them in parseItems, parseDict, parseStringArray, and readStringValue.
Fix macOS page-size JNA binding to NativeLong
oshi-core/.../mac/MacGlobalMemoryJNA.java
Swaps CloseableLongByReference for CloseableNativeLongByReference in the import and in queryPageSize(), extracting the result via getValue().longValue().
Null-safe returns in JMX and deprecation/warning suppression
oshi-demo/.../jmx/JMXOshiAgentServer.java, oshi-demo/.../jmx/mbeans/Baseboard.java, oshi-common/.../unix/netbsd/NetBsdOperatingSystem.java
JMXOshiAgentServer returns Collections.emptySet() from queryMBeans/queryNames and a new empty AttributeList from getAttributes/setAttributes, and marks two deserialize overloads @Deprecated. Baseboard applies the same empty-AttributeList fix. NetBsdOperatingSystem adds @SuppressWarnings({"deprecation","java:S1874"}) before getThreadId().

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • oshi/oshi#3069: Directly modifies MacInstalledApps XML parsing, including parseDict and array handling, overlapping with this PR's tag-constant refactor.
  • oshi/oshi#3381: Modifies BsdGraphicsCard's getGraphicsCards() instantiation, directly overlapping with this PR's UNKNOWN_PCI_ID constant introduction.
  • oshi/oshi#3395: Also modifies MacInstalledApps.readStringValue(...), making it directly overlapping with this PR's tag-constant changes to the same method.

Poem

🐇 Hop hop, no more "0x0000" in the weeds,
A constant named clearly is all that one needs.
NativeLong pages now fit just right,
And null-returning JMX is set aright.
Suppressed the old warnings with a quick annotation—
The warren runs tidy with clean compilation! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 4.17% 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 summarizes the main changes: addressing three Sonar code quality findings (S1168, S1874, S1192) with fixes to demo MBean stubs, deprecations, and duplicated string literals.
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 15, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.05%. Comparing base (cbfdddd) to head (61d6fb1).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...hardware/common/platform/unix/BsdGraphicsCard.java 0.00% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master    #3396   +/-   ##
=========================================
  Coverage     75.05%   75.05%           
  Complexity       76       76           
=========================================
  Files           579      579           
  Lines         21355    21356    +1     
  Branches       3556     3557    +1     
=========================================
+ Hits          16028    16029    +1     
+ Misses         3301     3300    -1     
- Partials       2026     2027    +1     
Flag Coverage Δ
freebsd 26.31% <55.55%> (-0.02%) ⬇️
linux 30.90% <55.55%> (-0.01%) ⬇️
macos 31.24% <83.33%> (-0.01%) ⬇️
openbsd 25.81% <55.55%> (+0.01%) ⬆️
solaris 27.87% <55.55%> (-0.01%) ⬇️
windows 39.54% <55.55%> (-0.01%) ⬇️

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 6148843 into oshi:master Jun 15, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the sonar-s1168-demo-deprecations branch July 8, 2026 15:18
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