Skip to content

Hoist the shared USB device-tree builder into AbstractUsbDevice - #3452

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:dedup-usb-tree-builder
Jul 12, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:dedup-usb-tree-builder

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jul 12, 2026 •

Copy link
Copy Markdown
Member

Summary

Capstone of the cross-OS USB consistency arc (follows #3447 Windows, #3448 Linux, #3449 Mac). Every platform's USB class carried its own copy of the same recursive controller/hub tree assembly — apply the parent's vendor/product IDs as defaults, recurse children through a parent→children map, sort by name, and fall back to "vid:pid" when a device reports no name. This hoists that single algorithm into AbstractUsbDevice, so the per-OS code is reduced to what genuinely differs: how each OS enumerates devices.

Producer / consumer split

  • New AbstractUsbDevice.buildDeviceTree(id, vid, pid, nameMap, vendorMap, vendorIdMap, productIdMap, serialMap, hubMap, factory) — the one recursive builder — plus a nested UsbDeviceFactory functional interface so each platform still constructs its own UsbDevice type.
  • Linux, BSD, FreeBSD, Solaris, Mac delete their private getDeviceAndChildren and call buildDeviceTree(..., XxxUsbDevice::new). Each keeps its own platform-specific map building (udev / usbdevs / lshal / prtconf / IOKit) — the legitimately-different producer.
    • Solaris passes empty vendor/serial maps (it parses neither).
    • Mac normalizes its map keys from Long to the "0x"-prefixed hex form it already used as uniqueDeviceId — behavior-identical.

Deliberately excluded (documented in-code)

  • Windows — keys each child to its parent (not parent→children), parses vendor/product/serial from PnP device-id strings, and inherits serials from the parent.
  • AIX — builds a flat single-controller list from lscfg; no parent/child tree exists to assemble.

Result

getDeviceAndChildren goes from 5 copies to 1; net −79 lines across 10 files. The producer differs per OS (inherent); the tree-building consumer is now single-source.

Behavior

No user-facing change. The tree-builder unit tests move from LinuxUsbDeviceTest into AbstractUsbDeviceTest, now exercising buildDeviceTree directly. NativeComparisonTest.usbDevices() (JNA vs FFM recursive deep-equal) passes on macOS/Apple Silicon.

Testing

  • spotless / checkstyle / forbiddenapis / javadoc: clean
  • Clean full-reactor install: BUILD SUCCESS, 0 tests failed
  • NativeComparisonTest.usbDevices() (JNA vs FFM): passes

Summary by CodeRabbit

  • Bug Fixes

    • Improved USB device tree construction across supported platforms.
    • Preserved inherited vendor and product information when device-level data is unavailable.
    • Ensured connected USB devices are consistently ordered and nested correctly.
  • Tests

    • Added coverage for leaf devices, fallback metadata, sorted hub children, and nested USB hierarchies.

The recursive controller/hub tree assembly (apply parent vid/pid defaults,
recurse children via a parent->children map, sort by name, fall back to
"vid:pid" for an absent name) was copied verbatim into five platform USB
classes. Extract it once as AbstractUsbDevice.buildDeviceTree, parameterized
by a UsbDeviceFactory so each platform still produces its own type.

Linux, BSD, FreeBSD, Solaris, and Mac now call buildDeviceTree and keep only
their platform-specific map building (udev / usbdevs / lshal / prtconf /
IOKit). Solaris passes empty vendor/serial maps; Mac keys its maps by the
"0x"-prefixed hex registry id it already used as the unique device id.

Windows (child->parent map, PnP device-id parsing, parent serial inheritance)
and AIX (flat lscfg list, no device tree) genuinely don't fit this model and
keep their own assembly, now with a comment noting why.

No user-facing change. The tree-builder unit tests move from LinuxUsbDeviceTest
to AbstractUsbDeviceTest, exercising buildDeviceTree directly; the JNA-vs-FFM
NativeComparisonTest.usbDevices() recursive deep-equal passes on Apple Silicon.
@coderabbitai

coderabbitai Bot commented Jul 12, 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: 9d141f70-0faf-4099-b50b-f87ca9cf6276

📥 Commits

Reviewing files that changed from the base of the PR and between aed7bfa and 61d9f95.

📒 Files selected for processing (10)
  • oshi-common/src/main/java/oshi/hardware/common/AbstractUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/aix/AixUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsUsbDevice.java
  • oshi-common/src/test/java/oshi/hardware/common/AbstractUsbDeviceTest.java
  • oshi-common/src/test/java/oshi/hardware/common/platform/linux/LinuxUsbDeviceTest.java
💤 Files with no reviewable changes (1)
  • oshi-common/src/test/java/oshi/hardware/common/platform/linux/LinuxUsbDeviceTest.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (16)
  • GitHub Check: CodeRabbit
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
🧰 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/hardware/common/platform/windows/WindowsUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/aix/AixUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/AbstractUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java
📚 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-common/src/main/java/oshi/hardware/common/platform/windows/WindowsUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/aix/AixUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/AbstractUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacUsbDevice.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java
🔇 Additional comments (13)
oshi-common/src/main/java/oshi/hardware/common/AbstractUsbDevice.java (1)

106-159: LGTM!

oshi-common/src/test/java/oshi/hardware/common/AbstractUsbDeviceTest.java (1)

26-187: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/linux/LinuxUsbDevice.java (1)

71-72: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdUsbDevice.java (1)

79-80: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/unix/freebsd/FreeBsdUsbDevice.java (1)

107-108: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/unix/solaris/SolarisUsbDevice.java (1)

109-110: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsUsbDevice.java (1)

27-31: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/unix/aix/AixUsbDevice.java (1)

20-21: LGTM!

oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacUsbDevice.java (5)

125-150: LGTM!


167-168: LGTM!


181-197: LGTM!


210-218: LGTM!


258-258: LGTM!


📝 Walkthrough

Walkthrough

USB device-tree construction is centralized in AbstractUsbDevice, with platform backends delegating recursive assembly to the shared helper. macOS normalizes registry identifiers to string keys, while tests cover fallback attributes, sorting, leaves, and nested hubs.

Changes

USB device tree refactor

Layer / File(s) Summary
Shared tree builder and validation
oshi-common/src/main/java/oshi/hardware/common/AbstractUsbDevice.java, oshi-common/src/main/java/oshi/hardware/common/platform/windows/WindowsUsbDevice.java, oshi-common/src/test/java/oshi/hardware/common/AbstractUsbDeviceTest.java
Adds recursive tree construction, inherited VID/PID fallback, sorted children, a factory interface, and unit tests for leaf and nested device trees.
Unix backend adoption
oshi-common/src/main/java/oshi/hardware/common/platform/{linux,unix}/...
Linux, BSD, FreeBSD, and Solaris use buildDeviceTree; AIX documents its intentionally flat lscfg model.
macOS key normalization
oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacUsbDevice.java
Switches registry identifiers to normalized hexadecimal string keys and uses the shared tree builder for controller hierarchies.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • oshi/oshi#3381: Refactors shared BSD USB enumeration code used by this tree-building change.
  • oshi/oshi#3448: Overlaps with the Linux USB tree-construction refactor.

Poem

A bunny hops through hubs in line,
With sorted kits and IDs that shine.
“Build once,” I cheer, “then share the tree!”
Nested paws connect so carefully.
USB branches bloom—whee!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely captures the main change: moving the shared USB device-tree builder into AbstractUsbDevice.
Docstring Coverage ✅ Passed Docstring coverage is 82.35% which is sufficient. The required threshold is 80.00%.
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.
✨ 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.

@codecov

codecov Bot commented Jul 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.52%. Comparing base (aed7bfa) to head (61d9f95).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
...common/platform/unix/solaris/SolarisUsbDevice.java 0.00% 2 Missing ⚠️
...common/platform/unix/freebsd/FreeBsdUsbDevice.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3452      +/-   ##
============================================
- Coverage     74.55%   74.52%   -0.03%     
  Complexity       76       76              
============================================
  Files           583      583              
  Lines         22001    21961      -40     
  Branches       3686     3682       -4     
============================================
- Hits          16402    16366      -36     
+ Misses         3557     3548       -9     
- Partials       2042     2047       +5     
Flag Coverage Δ
freebsd 26.80% <33.33%> (+0.03%) ⬆️
linux 31.46% <37.03%> (+0.03%) ⬆️
macos 31.93% <81.48%> (-0.05%) ⬇️
openbsd 26.27% <37.03%> (+0.02%) ⬆️
solaris 28.30% <33.33%> (+<0.01%) ⬆️
windows 39.75% <33.33%> (+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 e62d2de into oshi:master Jul 12, 2026
20 checks passed
@dbwiddis
dbwiddis deleted the dedup-usb-tree-builder branch July 12, 2026 04:39
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