Skip to content

Move more macOS hardware superclasses to oshi-common - #3148

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:mac-hw
Apr 12, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:mac-hw

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Apr 12, 2026 •

Copy link
Copy Markdown
Member

Continues the macOS hardware migration to oshi-common

Split into superclass (oshi-common) + JNA/FFM subclasses

oshi-common base JNA subclass FFM subclass
MacNetworkIF MacNetworkIFJNA MacNetworkIFFM
MacPowerSource MacPowerSourceJNA MacPowerSourceFFM

MacNetworkIF base class holds 11 fields, 11 getters, and 11 protected setters. Subclasses provide queryIfDisplayName (native SystemConfiguration calls), getNetworks, updateAttributes, and updateNetworkStats (which depend on NetStat in oshi-core).

MacPowerSource base class is a thin constructor pass-through to AbstractPowerSource, following the same pattern as LinuxPowerSource and WindowsPowerSource.

JNA renames (no split — too little shared code)

Before After
MacDisplay MacDisplayJNA
MacGpuStats MacGpuStatsJNA

Other changes

  • MacHardwareAbstractionLayerJNA: Updated references to renamed classes
  • MacGraphicsCardJNA: Updated MacGpuStats → MacGpuStatsJNA
  • MacPowerSourceFFM: Now extends common MacPowerSource instead of AbstractPowerSource

Summary by CodeRabbit

Release Notes

  • Refactor
    • Reorganized macOS hardware component implementations for improved code structure and maintainability.
    • Enhanced internal architecture for network interface and power source handling.
    • Clarified platform-specific implementation separation.

…n; add JNA suffix to MacDisplay and MacGpuStats (#3147)
@coderabbitai

coderabbitai Bot commented Apr 12, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7e519299-91e0-46da-8d48-7c1b7bb4cf6b

📥 Commits

Reviewing files that changed from the base of the PR and between 4d58475 and e3d6f08.

📒 Files selected for processing (10)
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacNetworkIF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacPowerSource.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacNetworkIFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacDisplayJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGpuStatsJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGraphicsCardJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacNetworkIFJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacPowerSourceJNA.java
📜 Recent review details
🧰 Additional context used
🧠 Learnings (10)
📓 Common learnings
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:51.516Z
Learning: In the oshi/oshi project, the public API is strictly limited to interfaces and classes in the `oshi`, `oshi.hardware`, and `oshi.software.os` packages. All platform-specific code under subpackages like `oshi.software.os.linux`, `oshi.hardware.platform.linux`, etc., is considered implementation detail — not public API. Do not flag changes to platform-specific classes (e.g., making `LinuxOperatingSystem` abstract, narrowing constructor visibility, adding abstract methods) as public API breaks.
📚 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/MacGraphicsCardJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGpuStatsJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacDisplayJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacPowerSourceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacNetworkIFJNA.java
📚 Learning: 2026-04-08T00:58:10.736Z
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:10.736Z
Learning: In `oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsPowerSourceJNA.java` and `oshi-core-java25/src/main/java/oshi/hardware/platform/windows/WindowsPowerSourceFFM.java`, the `getPowerSources()` method intentionally returns a singleton list wrapping a single `getPowerSource("System Battery")` call, and the enumeration loop short-circuits after the first eligible battery pack. This matches the longstanding JNA behavior and is not a bug. A broader refactor to support zero-or-more batteries across both implementations is planned for a later date. Do not flag this as an issue.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacPowerSourceJNA.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacPowerSource.java
📚 Learning: 2026-04-03T02:07:47.621Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3110
File: oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java:119-121
Timestamp: 2026-04-03T02:07:47.621Z
Learning: In OSHI’s Java FFM/API implementation classes (e.g., *FFM.java) that implement/override `updateAttributes()`, preserve the intentional failure-handling pattern: catch `Throwable` and return `false` without logging. `updateAttributes()` is a public API whose contract is to signal failure via the boolean return value, and callers rely on that return value (so emitting logs from this path is not desired and should not be introduced during review unless the method contract changes).

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacNetworkIFFM.java
📚 Learning: 2026-04-06T21:48:11.921Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3122
File: oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java:85-111
Timestamp: 2026-04-06T21:48:11.921Z
Learning: In `oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java` (and `MacUsbDeviceFFM.java`), the `id = 0L` sentinel is intentional when `device.getParentEntry(IOSERVICE)` returns null. It acts as a catch-all controller group for devices whose controller cannot be identified (an edge case that "should never happen" given the IORegistry format). Do not flag this as a bug or suggest skipping such devices; the design intentionally avoids both silently dropping orphaned devices and placing them at the top level without a controller context.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGpuStatsJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacDisplayJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacNetworkIFJNA.java
📚 Learning: 2026-04-11T05:51:49.185Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3139
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxGpuStatsFFM.java:36-58
Timestamp: 2026-04-11T05:51:49.185Z
Learning: In OSHI's `LinuxGpuStats` (oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxGpuStats.java), the abstract `nvml*` methods (nvmlGetVramUsed, nvmlGetCoreClockMhz, nvmlGetMemoryClockMhz, nvmlGetTemperature, nvmlGetPowerDraw, nvmlGetFanSpeedPercent) are contractually expected to return `-1` (long or double) on failure. The base class applies `if (val >= 0)` guards before using any NVML result and falls through to sysfs/hwmon paths when negative. Subclasses (LinuxGpuStatsJNA, LinuxGpuStatsFFM) correctly pass through the -1 sentinel unchanged — do not flag this as a missing clamp or contract violation.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacGpuStatsJNA.java
📚 Learning: 2026-03-29T20:01:06.452Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3014
File: oshi-core-java11/src/main/java/module-info.java:10-17
Timestamp: 2026-03-29T20:01:06.452Z
Learning: In the oshi project, `oshi-core-java25/src/main/java/module-info.java` intentionally does NOT export `oshi.util.platform.unix.freebsd`, `oshi.util.platform.unix.openbsd`, or `oshi.util.platform.unix.solaris`, because the FFM (Foreign Function & Memory) implementation only supports Windows, macOS, and Linux. This differs from `oshi-core-java11/src/main/java/module-info.java` which exports all Unix platform packages. The asymmetry is by design.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacDisplayJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacNetworkIFJNA.java
📚 Learning: 2026-04-03T02:07:49.467Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3110
File: oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java:119-121
Timestamp: 2026-04-03T02:07:49.467Z
Learning: In `oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacHWDiskStoreFFM.java` (and similar FFM/API implementations in OSHI), the `updateAttributes()` method intentionally uses `catch (Throwable e) { return false; }` without logging. This is by design: `updateAttributes()` is a public API method whose contract is to return `false` on failure; callers consume the boolean and would never see log output, so adding a log statement is not desired.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacDisplayJNA.java
📚 Learning: 2026-04-06T22:30:07.083Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3122
File: oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java:215-234
Timestamp: 2026-04-06T22:30:07.083Z
Learning: In `oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`, the `found` flag in `getControllerIdByLocation` is intentionally set to `true` as soon as either `vendor-id` or `device-id` is found in a matching service's parent. This is a deliberate design choice: a partial match (one of the two IDs) is preferred over continuing to iterate, because iterating further risks pairing IDs from two different and potentially mismatched services. Do not flag this as a bug or suggest splitting into separate `foundVendor`/`foundProduct` flags.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacDisplayJNA.java
📚 Learning: 2026-04-03T18:44:48.200Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3112
File: oshi-core/src/main/java/oshi/PlatformEnum.java:63-66
Timestamp: 2026-04-03T18:44:48.200Z
Learning: In oshi/oshi PlatformEnum.java, the DRAGONFLYBSD enum constant is intentionally added for platform detection only (no implementation classes exist yet). It allows UnsupportedOperationException messages to say "DragonFly BSD" rather than "Unknown". Full DragonFly BSD support is tracked in issue `#2583`. Do not flag the missing switch-case implementations as a bug.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacDisplayJNA.java
🔇 Additional comments (11)
oshi-core/src/main/java/oshi/hardware/platform/mac/MacGraphicsCardJNA.java (1)

27-27: Consistent subclass instantiation after rename.

Line 27 correctly switches to MacGpuStatsJNA while preserving the GpuStats contract and constructor inputs.

oshi-core/src/main/java/oshi/hardware/platform/mac/MacGpuStatsJNA.java (2)

45-45: Class/constructor rename is internally consistent.

Lines 45 and 66 are aligned with the new MacGpuStatsJNA naming and keep the same GpuStats implementation shape.

Also applies to: 66-66


11-13: Logger wiring matches the renamed class.

Updating imports and logger initialization to MacGpuStatsJNA.class avoids stale log categories.

Also applies to: 47-47

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

29-31: JNA display rename is consistently applied.

Class/constructor/logger updates and the instantiation site are all in sync with MacDisplayJNA.

Also applies to: 34-40, 87-87

oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacPowerSource.java (1)

16-28: Common superclass extraction looks clean.

The new MacPowerSource base is a straightforward constructor pass-through and fits the intended shared JNA/FFM hierarchy.

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

27-27: FFM class now correctly targets the shared macOS base.

This inheritance update cleanly aligns with the superclass split and keeps implementation behavior intact.

Also applies to: 36-36

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

25-25: JNA power source migration is consistent.

Superclass import, class/constructor rename, and object creation site all match the new MacPowerSourceJNA pattern.

Also applies to: 33-33, 38-38, 214-214

oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacNetworkIF.java (1)

17-33: Good consolidation of shared macOS NIC state.

Moving the counters/getters/setters into oshi-common reduces duplication and gives JNA/FFM implementations a consistent update surface.

Also applies to: 35-88, 90-132

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

52-52: HAL delegation updates are correct.

getPowerSources(), getDisplays(), and getNetworkIFs() now point to the intended JNA implementations after the rename/split.

Also applies to: 62-62, 67-67

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

26-26: FFM network stats migration is well-applied.

Switching to MacNetworkIF plus protected setter writes is consistent and keeps updateNetworkStats() behavior aligned with pre-refactor logic.

Also applies to: 32-32, 96-106

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

23-23: JNA network implementation refactor is consistent.

Class rename, instantiation site, and migration to inherited setter-based stat updates are all correctly wired.

Also applies to: 28-35, 74-74, 92-102


📝 Walkthrough

Walkthrough

This PR introduces abstract base classes for macOS network and power source components into oshi-common, then refactors existing JNA and FFM implementations to extend these new common classes. Several concrete implementation classes are also renamed for consistency (e.g., MacDisplay → MacDisplayJNA, MacGpuStats → MacGpuStatsJNA).

Changes

Cohort / File(s) Summary
New macOS Common Base Classes
oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacNetworkIF.java, oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacPowerSource.java
Added two abstract base classes: MacNetworkIF with private fields for network statistics and protected setters; MacPowerSource with constructor forwarding all power-source attributes to superclass.
JNA Platform Implementations
oshi-core/src/main/java/oshi/hardware/platform/mac/MacNetworkIFJNA.java, oshi-core/src/main/java/oshi/hardware/platform/mac/MacPowerSourceJNA.java
Updated to extend the new common MacNetworkIF and MacPowerSource base classes; removed duplicated field declarations and getter overrides; now use inherited setters for stats updates; updated class names and instantiation calls.
FFM Platform Implementations
oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacNetworkIFFM.java, oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacPowerSourceFFM.java
Updated to extend the new common MacNetworkIF and MacPowerSource base classes instead of abstract base classes; removed field declarations and getter methods from MacNetworkIFFM; now delegate to inherited setters.
Class Renames (JNA)
oshi-core/src/main/java/oshi/hardware/platform/mac/MacDisplayJNA.java, oshi-core/src/main/java/oshi/hardware/platform/mac/MacGpuStatsJNA.java
Renamed MacDisplay → MacDisplayJNA and MacGpuStats → MacGpuStatsJNA with corresponding constructor and logger updates to maintain clarity between concrete and abstract implementations.
References & HAL Updates
oshi-core/src/main/java/oshi/hardware/platform/mac/MacGraphicsCardJNA.java, oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
Updated instantiation and delegation to use renamed/refactored classes (MacGpuStatsJNA, MacPowerSourceJNA, MacDisplayJNA, MacNetworkIFJNA).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • PR #3146 — Continues the same macOS refactoring pattern by consolidating platform-specific base classes into oshi-common and updating platform implementations to extend them.
  • PR #3144 — Establishes the oshi.hardware.common.platform.mac package structure where new macOS base classes are being added.
  • PR #3110 — Updates FFM-backed macOS implementations (MacNetworkIFFM, MacPowerSourceFFM) to extend the new common base classes and use inherited setters.

Poem

🐰 A little refactor, oh what a day!
Base classes extracted, inheritance way,
Common platforms rising, clean and bright,
JNA and FFM now unified right!
Hops with joy 🌱✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.89% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: moving macOS hardware superclasses to oshi-common, which is the central theme across all file modifications.

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

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

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 Apr 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.12%. Comparing base (4d58475) to head (e3d6f08).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3148      +/-   ##
============================================
+ Coverage     61.39%   62.12%   +0.72%     
  Complexity       12       12              
============================================
  Files            39       39              
  Lines          2329     2329              
  Branches        407      407              
============================================
+ Hits           1430     1447      +17     
+ Misses          783      771      -12     
+ Partials        116      111       -5     

☔ 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 b0c5e39 into oshi:master Apr 12, 2026
15 of 16 checks passed
@dbwiddis
dbwiddis deleted the mac-hw branch April 12, 2026 18:06
dbwiddis added a commit that referenced this pull request Apr 12, 2026
* Move macOS MacCentralProcessor, MacFirmware, and MacHWDiskStore superclasses to oshi-common; add JNA suffix to remaining mac hardware classes (#3148)

* Fix cpufamily fallback for M4+ Macs, null-safe partitionList, and exception-safe IOKit iteration in MacFirmwareJNA
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