Skip to content

Refactor USB device classes to reduce duplication - #3122

Merged
dbwiddis merged 4 commits into
oshi:masterfrom
dbwiddis:refactor-usb
Apr 6, 2026
Merged

dbwiddis merged 4 commits into
oshi:masterfrom
dbwiddis:refactor-usb

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Apr 6, 2026 •

Copy link
Copy Markdown
Member

Refactors all USB device classes to eliminate repeated code across platforms.

Changes

AbstractUsbDevice

  • Moved addDevicesToList() here as protected static, removing the identical private copy that existed in FreeBSD, OpenBSD, Solaris, and Windows.

Linux

  • Extracted LinuxUsbDevice as a concrete base class holding the shared getDeviceAndChildren() tree-builder.
  • Renamed the original implementation to LinuxUsbDeviceJNA.
  • LinuxUsbDeviceFFM now extends LinuxUsbDevice and inherits getDeviceAndChildren() instead of duplicating it.

Mac

  • Same pattern: MacUsbDevice base class with shared getDeviceAndChildren(); original implementation renamed to MacUsbDeviceJNA; MacUsbDeviceFFM extends MacUsbDevice.

All platforms

  • All platforms now consistently exclude controllers from the flat list (tree=false), matching the API contract documented in HardwareAbstractionLayer.

Pre-existing fix

  • Fixed toLowerCase() → toLowerCase(Locale.ROOT) in LinuxLibcFunctions (forbiddenapis violation).

Summary by CodeRabbit

  • New Features

    • Added new platform-specific USB backends for Linux and macOS to improve USB discovery reliability.
  • Bug Fixes

    • More robust architecture detection using locale-independent string handling.
  • Refactor

    • USB enumeration reworked for clearer, consistent device discovery and reduced duplication while preserving device relationships.
  • Documentation

    • Clarified USB enumeration behavior in the Hardware Abstraction Layer Javadoc.
  • Chores

    • Updated copyright years and changelog.

@coderabbitai

coderabbitai Bot commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Refactors USB enumeration: moves per-OS enumeration into new JNA-specific helpers (LinuxUsbDeviceJNA, MacUsbDeviceJNA), centralizes device-flattening in AbstractUsbDevice, removes public enumerators from platform base classes, updates HAL delegation to JNA helpers, and normalizes Linux arch detection using Locale.ROOT.

Changes

Cohort / File(s) Summary
Locale normalization
oshi-core-java25/src/main/java/oshi/ffm/linux/LinuxLibcFunctions.java
Normalize System.getProperty("os.arch", "") via toLowerCase(Locale.ROOT); added Locale import.
Abstract helper
oshi-core/src/main/java/oshi/hardware/common/AbstractUsbDevice.java
Added protected static void addDevicesToList(...) for DFS flattening; updated copyright year to 2016-2026.
Linux USB refactor
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDevice.java, oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java, oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java, oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java
Removed public getUsbDevices(boolean) from LinuxUsbDevice (constructors made protected/no-arg); added LinuxUsbDeviceJNA implementing udev-based enumeration; FFM variant updated to extend base and delegate; HAL now calls JNA helper.
macOS USB refactor
oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDevice.java, oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java, oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java, oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
Removed public getUsbDevices(boolean) from MacUsbDevice (constructors made protected/no-arg); added MacUsbDeviceJNA with IORegistry-based enumeration; FFM variant updated to delegate; HAL now calls JNA helper.
Other platforms
oshi-core/src/main/java/oshi/hardware/platform/unix/.../FreeBsdUsbDevice.java, .../openbsd/OpenBsdUsbDevice.java, .../solaris/SolarisUsbDevice.java, oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
Removed local flattening helpers, adjusted flat-vs-tree behavior to reuse discovered UsbDevice instances (no wrapper cloning), switched to static imports for emptyList()/sort(), and updated copyright years.
HAL Javadoc & misc
oshi-core/src/main/java/oshi/hardware/HardwareAbstractionLayer.java, CHANGELOG.md, oshi-core/src/main/java/oshi/SystemInfo.java, pom.xml
Expanded getUsbDevices(boolean) Javadoc clarifying tree vs flat semantics; added changelog entry; minor import reorder and Spotless importOrder update.

Sequence Diagram(s)

sequenceDiagram
    participant HAL as Hardware Abstraction Layer
    participant JNA as JNA Helper (Linux / Mac)
    participant ENUM as System Enumerator (udev / IORegistry)
    participant BASE as Platform Base Class (Linux / Mac)
    participant ABSTRACT as AbstractUsbDevice

    HAL->>+JNA: getUsbDevices(tree)
    JNA->>+ENUM: enumerate devices
    ENUM-->>-JNA: attribute maps & parent links
    JNA->>+BASE: getDeviceAndChildren(...) per controller/root
    BASE-->>-JNA: UsbDevice tree(s)
    alt tree == false
        JNA->>+ABSTRACT: addDevicesToList(flatList, roots)
        ABSTRACT->>ABSTRACT: depth-first traverse connected devices
        ABSTRACT-->>-JNA: flattened device list
        JNA-->>HAL: flattened devices
    else tree == true
        JNA-->>HAL: controller-rooted device tree
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • PR #3117: Overlapping Linux USB/udev HAL migration and LinuxUsbDeviceJNA changes.
  • PR #3111: Related macOS USB/HAL JNA plumbing and delegation updates.
  • PR #3110: Related macOS FFM USB implementation and MacUsbDeviceFFM refactor.

Poem

🐰
I hopped through kernel paths and found,
New JNA tunnels under ground,
Trees kept neat, the list runs free,
Devices sing in harmony,
A carrot cheer — enumeration crowned! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 43.90% 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 pull request title directly summarizes the main objective: refactoring USB device classes to eliminate code duplication across platforms.

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

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3122      +/-   ##
==========================================
+ Coverage   66.46%   67.75%   +1.28%     
==========================================
  Files          37       37              
  Lines        2025     2025              
  Branches      336      336              
==========================================
+ Hits         1346     1372      +26     
+ Misses        563      535      -28     
- Partials      116      118       +2     

☔ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java (1)

39-49: ⚠️ Potential issue | 🟡 Minor

Fix the stale @param tree description.

The summary paragraph and the implementation on Lines 51-58 both include controllers when tree == false, but Line 48 still says the flat list excludes them.

Also applies to: 51-58

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java`
around lines 39 - 49, The Javadoc `@param` description for the method in class
WindowsUsbDevice is stale: it states that a flat list (tree == false) excludes
controllers, but the method implementation (getUsbDevices(boolean tree) / the
USB device-instantiation logic) actually includes controllers in the flat list.
Update the `@param` tree description to say: when true returns a hierarchical list
of controllers (top-level) with their connected hubs/devices; when false returns
a flat list of all devices including controllers. Ensure the wording matches the
summary and implementation.
oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java (1)

45-46: ⚠️ Potential issue | 🟡 Minor

Fix the flat-list Javadoc.

The code below only flattens each controller's connectedDevices, so tree == false now excludes controllers. This text still says the opposite.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java`
around lines 45 - 46, The Javadoc for the 'tree' parameter is incorrect:
currently it says tree==false returns a flat list including controllers, but the
implementation in LinuxUsbDeviceFFM only flattens each controller's
connectedDevices and therefore excludes controllers when tree==false. Update the
Javadoc on the relevant method in class LinuxUsbDeviceFFM to state that when
tree==true it returns controllers with their device tree, and when tree==false
it returns a flat list of connected devices (children) excluding controller
entries, making explicit that only controllers' connectedDevices are flattened.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java`:
- Around line 53-54: queryUsbDevices() returns controller root entries, so
calling addDevicesToList(deviceList, devices) for tree == false re-adds
controllers into the flat list; filter out controller-root devices before
adding. Modify the call site where devices is passed to addDevicesToList (the
local variable devices obtained from queryUsbDevices()) so that when the tree
flag is false you pass a filtered collection excluding devices whose parent is
null (i.e., controller roots), e.g., create filteredDevices =
devices.stream().filter(d -> d.getParentUsbDevice() != null).collect(...) and
call addDevicesToList(deviceList, filteredDevices) instead; keep the original
behavior when tree == true. Ensure you reference queryUsbDevices(),
addDevicesToList(), the devices variable, and the tree parameter when making the
change.

In `@oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java`:
- Around line 47-53: The Javadoc for the USB listing behavior is inaccurate:
when tree == false the implementation excludes controllers and only adds each
controller's connectedDevices to the flat list. Update the class/method Javadoc
in LinuxUsbDeviceJNA (the method that returns List<oshi.hardware.UsbDevice>,
e.g., the getUsbDevices/getUsbDeviceList method) to state that tree == true
returns controllers with recursive connectedDevices, whereas tree == false
returns a flat list of only non-controller devices (connectedDevices of
controllers) and does not include the controller devices themselves; change both
the descriptive paragraph and the `@param` text to reflect this exact behavior.

In `@oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`:
- Around line 37-47: Update the stale Javadoc in MacUsbDeviceJNA for the method
that instantiates UsbDevice objects so the `@param` tree description matches the
implementation and summary: change the text to state that when tree is true the
method returns top-level controllers with their device trees, and when tree is
false it returns a single flat list of devices including controllers; ensure the
same corrected wording is applied consistently to the summary and the `@param`
tree tag (and any duplicate paragraph blocks in the same Javadoc) so the
documentation matches the behavior.
- Around line 81-105: The code currently uses a default id=0L when
device.getParentEntry(IOSERVICE) is null, then adds that sentinel into
usbControllers and calls addDeviceAndChildrenToMaps, producing bogus root
entries; change the logic so you only add the controller ID to usbControllers
and call addDeviceAndChildrenToMaps when controller != null (i.e., move
usbControllers.add(id) and the addDeviceAndChildrenToMaps(...) call inside the
controller != null branch), ensuring you still release any CFTypeRef and
controller objects as currently done.

---

Outside diff comments:
In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java`:
- Around line 45-46: The Javadoc for the 'tree' parameter is incorrect:
currently it says tree==false returns a flat list including controllers, but the
implementation in LinuxUsbDeviceFFM only flattens each controller's
connectedDevices and therefore excludes controllers when tree==false. Update the
Javadoc on the relevant method in class LinuxUsbDeviceFFM to state that when
tree==true it returns controllers with their device tree, and when tree==false
it returns a flat list of connected devices (children) excluding controller
entries, making explicit that only controllers' connectedDevices are flattened.

In
`@oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java`:
- Around line 39-49: The Javadoc `@param` description for the method in class
WindowsUsbDevice is stale: it states that a flat list (tree == false) excludes
controllers, but the method implementation (getUsbDevices(boolean tree) / the
USB device-instantiation logic) actually includes controllers in the flat list.
Update the `@param` tree description to say: when true returns a hierarchical list
of controllers (top-level) with their connected hubs/devices; when false returns
a flat list of all devices including controllers. Ensure the wording matches the
summary and implementation.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6ec8a181-7863-4025-bf1c-18f308fcc47a

📥 Commits

Reviewing files that changed from the base of the PR and between b88ab7f and bcc5e06.

📒 Files selected for processing (14)
  • oshi-core-java25/src/main/java/oshi/ffm/linux/LinuxLibcFunctions.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core/src/main/java/oshi/hardware/common/AbstractUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
📜 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). (7)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (13)
📓 Common learnings
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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.
📚 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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/common/AbstractUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDevice.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDevice.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.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/linux/LinuxHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/common/AbstractUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.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, 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.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/ffm/linux/LinuxLibcFunctions.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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-java25/src/main/java/oshi/ffm/linux/LinuxLibcFunctions.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/common/AbstractUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-03T18:44:46.910Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3112
File: oshi-core/src/main/java/oshi/PlatformEnum.java:63-66
Timestamp: 2026-04-03T18:44:46.910Z
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/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
📚 Learning: 2026-04-03T18:43:50.370Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3112
File: oshi-core/src/main/java/oshi/PlatformEnum.java:99-100
Timestamp: 2026-04-03T18:43:50.370Z
Learning: In oshi/oshi PlatformEnum.java, the DragonFly BSD detection uses `osName.equalsIgnoreCase("dragonflybsd")` intentionally, mirroring the exact same check in JNA's `Platform.java` source. Java's `os.name` property on DragonFly BSD is "dragonflybsd" (no space), so `startsWith("DragonFly")` is NOT the correct pattern here. Do not suggest changing it to `startsWith`.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.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-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI’s Linux FFM implementations (e.g., LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the corresponding JNA udev code, do not require/flag a missing null check around `udev_enumerate_new()`/`enumerateNew()`. Per libudev semantics, `udev_enumerate_new()` returns NULL only if udev itself is broken (e.g., OOM). Since `udev_new()` is already guarded and is the successful prerequisite for enumeration, treating `udev_enumerate_new()` as non-null is consistent with the established pattern; adding a redundant null guard would be inconsistent.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: When reviewing OSHI Linux FFM/JNA code that uses libudev, do not flag a missing null check on `udev_enumerate_new()` if `udev_new()` has already been guarded. Per libudev semantics, `udev_enumerate_new()` returns NULL only when the udev context itself is broken (e.g., OOM), so a successful `udev_new()` implies `udev_enumerate_new()` should succeed as well. Therefore, lack of a redundant null guard on `udev_enumerate_new()` is acceptable in this pattern.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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/linux/LinuxUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
🔇 Additional comments (6)
oshi-core-java25/src/main/java/oshi/ffm/linux/LinuxLibcFunctions.java (2)

22-22: LGTM: Import correctly added.

The Locale import is necessary for the locale-aware string normalization below.


47-47: Excellent: Locale-independent normalization prevents architecture detection bugs.

Replacing toLowerCase() with toLowerCase(Locale.ROOT) ensures consistent behavior across all system locales. The platform-default toLowerCase() can produce incorrect results in certain locales (e.g., Turkish locale where uppercase "I" lowercases to dotless "ı" instead of "i"), which could break architecture detection.

oshi-core/src/main/java/oshi/hardware/common/AbstractUsbDevice.java (1)

80-91: Nice helper extraction.

Centralizing the depth-first flattening here removes the duplicated per-platform copies without changing the traversal shape.

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

51-65: Nice extraction of the shared tree builder.

This gives the macOS implementations one place to keep recursion, VID/PID fallback, and child sorting aligned.

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

67-70: LGTM.

The HAL now points at the extracted JNA USB enumerator, so the wiring stays aligned with the refactor.

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

23-35: Nice extraction of the shared Linux USB base.

Making the constructors protected and moving the recursive builder here keeps the JNA/FFM helpers thin while preserving a single tree-construction path.

Also applies to: 51-63

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

♻️ Duplicate comments (1)
oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java (1)

53-54: ⚠️ Potential issue | 🟠 Major

Flat mode still returns controller roots.

queryUsbDevices() builds controller-level entries, so flattening devices directly here re-adds those roots and breaks the tree == false contract. Flatten each controller's children instead.

🔧 Proposed fix
         List<UsbDevice> deviceList = new ArrayList<>();
-        addDevicesToList(deviceList, devices);
+        for (UsbDevice device : devices) {
+            addDevicesToList(deviceList, device.getConnectedDevices());
+        }
         return deviceList;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java`
around lines 53 - 54, The code currently flattens the entire devices list (which
contains controller roots from queryUsbDevices()) by calling
addDevicesToList(deviceList, devices), causing controller entries to be re-added
when tree == false; instead iterate the top-level controllers in devices and
call addDevicesToList(deviceList, controller.getChildren()) (or equivalent) so
only each controller's children are flattened into deviceList, preserving the
tree==false contract in queryUsbDevices()/addDevicesToList handling.
🧹 Nitpick comments (1)
oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java (1)

68-68: Use a Set to deduplicate controller IDs in the collection loop.

The current code adds a controller ID to the list once for each device under that controller (line 102 within the device loop). If multiple devices share the same controller parent, duplicate controller IDs accumulate in the list, causing the loop at lines 120-123 to create multiple UsbDevice entries for the same controller.

Use LinkedHashSet<Long> to deduplicate:

♻️ Proposed fix
-        List<Long> usbControllers = new ArrayList<>();
+        Set<Long> usbControllers = new LinkedHashSet<>();

Windows implementation already uses a Set to avoid this issue; Linux avoids it by design (only collecting root-level controllers).

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java` at
line 68, The usbControllers collection in MacUsbDeviceJNA is using List<Long>
usbControllers = new ArrayList<>() and accumulates duplicate controller IDs when
multiple devices share the same parent; replace it with a LinkedHashSet<Long>
(or Set<Long>) to deduplicate controller IDs during the device loop so
subsequent iteration over usbControllers only processes each controller once,
preserving insertion order and preventing creation of duplicate UsbDevice
entries.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java`:
- Around line 92-95: The code currently pushes the fallback anonymous controller
id (0L) into usbControllers for every device whose parent is unknown, causing
repeated synthetic controllers; update the logic around usbControllers.add(id)
(near addDeviceAndChildrenToMaps calls and hubMap usage) to only add id when id
!= 0L or, if id == 0L, add it only once by checking usbControllers.contains(0L)
before adding so the synthetic controller is recorded a single time.

In `@oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java`:
- Around line 84-87: Guard the result of Udev.INSTANCE.udev_new() before using
it: check if the returned Udev.UdevContext udev is null and, if so, skip
creating the enumerator and return early (or handle error) to avoid NPEs; only
call udev.enumerateNew() and later udev.unref() when udev != null. Update the
code paths in the method that use udev (references: Udev.INSTANCE.udev_new(),
Udev.UdevContext udev, enumerateNew(), unref()) to follow the same null-guard
pattern used in the other Linux*JNA implementations.

In `@oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`:
- Around line 171-172: The code calls device.getChildIterator(IOUSB) and
immediately uses childIter.next() (in MacUsbDeviceJNA) without checking for a
null iterator; update the logic around IOIterator childIter =
device.getChildIterator(IOUSB) to check whether childIter is null before calling
childIter.next() and handle the null case (e.g., skip processing this device or
return early) to avoid a NullPointerException when getChildIterator returns
null; ensure any downstream use of IORegistryEntry childDevice is guarded
accordingly.

---

Duplicate comments:
In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java`:
- Around line 53-54: The code currently flattens the entire devices list (which
contains controller roots from queryUsbDevices()) by calling
addDevicesToList(deviceList, devices), causing controller entries to be re-added
when tree == false; instead iterate the top-level controllers in devices and
call addDevicesToList(deviceList, controller.getChildren()) (or equivalent) so
only each controller's children are flattened into deviceList, preserving the
tree==false contract in queryUsbDevices()/addDevicesToList handling.

---

Nitpick comments:
In `@oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`:
- Line 68: The usbControllers collection in MacUsbDeviceJNA is using List<Long>
usbControllers = new ArrayList<>() and accumulates duplicate controller IDs when
multiple devices share the same parent; replace it with a LinkedHashSet<Long>
(or Set<Long>) to deduplicate controller IDs during the device loop so
subsequent iteration over usbControllers only processes each controller once,
preserving insertion order and preventing creation of duplicate UsbDevice
entries.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6874c763-6586-45f7-9eea-596d36b7d984

📥 Commits

Reviewing files that changed from the base of the PR and between bcc5e06 and 6e5b86d.

📒 Files selected for processing (6)
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core/src/main/java/oshi/hardware/HardwareAbstractionLayer.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
✅ Files skipped from review due to trivial changes (1)
  • oshi-core/src/main/java/oshi/hardware/HardwareAbstractionLayer.java
📜 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). (9)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (11)
📓 Common learnings
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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.
📚 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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.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/windows/WindowsUsbDevice.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/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.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/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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/MacUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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, 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.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI’s Linux FFM implementations (e.g., LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the corresponding JNA udev code, do not require/flag a missing null check around `udev_enumerate_new()`/`enumerateNew()`. Per libudev semantics, `udev_enumerate_new()` returns NULL only if udev itself is broken (e.g., OOM). Since `udev_new()` is already guarded and is the successful prerequisite for enumeration, treating `udev_enumerate_new()` as non-null is consistent with the established pattern; adding a redundant null guard would be inconsistent.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: When reviewing OSHI Linux FFM/JNA code that uses libudev, do not flag a missing null check on `udev_enumerate_new()` if `udev_new()` has already been guarded. Per libudev semantics, `udev_enumerate_new()` returns NULL only when the udev context itself is broken (e.g., OOM), so a successful `udev_new()` implies `udev_enumerate_new()` should succeed as well. Therefore, lack of a redundant null guard on `udev_enumerate_new()` is acceptable in this pattern.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
🔇 Additional comments (13)
oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java (3)

1-8: LGTM on copyright and import changes.

Copyright year update and static import for Collections.sort are appropriate housekeeping changes.


51-61: Flat-list controller exclusion logic is correct.

The implementation properly excludes controllers when tree=false by iterating over each controller and calling addDevicesToList only on device.getConnectedDevices() rather than on the controller itself. This aligns with the Javadoc contract on lines 47-48 and the PR objective to consistently exclude controllers from flat lists across all platforms.


113-113: LGTM on static import usage.

Using the statically imported sort method is a clean simplification.

oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java (4)

26-29: LGTM - Utility class inheriting shared helper methods.

The design where LinuxUsbDeviceFFM extends LinuxUsbDevice but is never instantiated is intentional—it allows inheriting the protected static getDeviceAndChildren() method while preventing direct instantiation. The private no-arg constructor correctly delegates to the parent's protected constructor.


41-59: LGTM - Controller exclusion correctly implemented.

The flat-list behavior now properly excludes controllers by iterating through controller devices and only adding their getConnectedDevices() to the result list. The Javadoc accurately describes this behavior, aligning with the HardwareAbstractionLayer API contract as stated in the PR objectives.


61-77: LGTM - Clean early-exit pattern with proper error handling.

The queryUsbDevices() method correctly handles error conditions with emptyList() returns. The absence of a null check on udev_enumerate_new() at line 80 is intentional and consistent with OSHI's established pattern—per libudev semantics, udev_enumerate_new() only returns NULL when udev itself is broken, and a successful udev_new() (which is guarded) implies enumeration will succeed.


149-155: LGTM - Tree building delegated to parent class.

The controller device tree construction correctly uses the inherited getDeviceAndChildren() method from LinuxUsbDevice. This eliminates code duplication as the same tree-building logic is now shared between the JNA and FFM implementations.

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

47-53: Tree vs flat USB listing behavior is consistent and clear.

tree=true returns controller roots; tree=false flattens only connected devices, excluding controllers. The Javadoc and implementation are aligned.

Also applies to: 55-65

oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java (5)

45-46: Javadoc inconsistency with implementation (previously flagged).

The @param tree description at Line 46 states the flat list "exclud[es] controllers," but the implementation in Lines 49-56 (using addDevicesToList) includes controllers in the flattened output. The summary paragraph at Line 43 is also inconsistent.


1-36: LGTM!

The class structure follows the utility class pattern correctly with a private constructor and static methods. The inheritance from MacUsbDevice provides access to the shared getDeviceAndChildren() method, and the constants are appropriately scoped.


49-57: LGTM!

The method correctly delegates to queryUsbDevices() for tree building and uses the inherited addDevicesToList() for flattening, aligning with the refactor's goal of centralizing the flattening logic.


183-239: LGTM!

The controller ID lookup correctly cross-references by locationID, properly manages IOKit resources (releasing propertyDict, parent, matchingService, serviceIterator), and uses Locale.ROOT for consistent hex formatting.


139-168: LGTM!

The recursive map population correctly handles nullable properties, uses computeIfAbsent for clean hub mapping, and applies proper 16-bit masking with Locale.ROOT formatting for vendor/product IDs.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java (1)

85-111: ⚠️ Potential issue | 🟠 Major

Avoid collapsing unknown parents into a synthetic 0L controller.

When getParentEntry(IOSERVICE) is null, this still registers 0L as a controller and later materializes it as a fallback MacUsbDevice (0000:0000, 0x0). That makes tree=true expose a fake top-level controller and merges every orphan subtree under the same root. If those devices need to stay visible, keep them in a separate orphan list instead of keying them under 0L.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`
around lines 85 - 111, The code currently collapses devices with null controller
into a synthetic controller id 0L and adds it to usbControllers; instead, when
controller == null do not set id to 0L or add it to usbControllers or
nameMap—treat the device as an orphan: create a separate orphan list (e.g.,
orphanUsbDevices/orphanControllerIds) and pass a sentinel (e.g.,
null/Optional/absent parent id) into addDeviceAndChildrenToMaps so children are
recorded without being grouped under 0L; keep existing behavior for real
controllers (use getParentEntry, getRegistryEntryID, getControllerIdByLocation,
nameMap, vendorIdMap/productIdMap) but ensure no MacUsbDevice is synthesized for
id 0L and adjust downstream consumers that iterate usbControllers to include the
orphan list when appropriate.
🧹 Nitpick comments (1)
oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java (1)

70-70: Conditional 0L check is now redundant with LinkedHashSet.

The switch to LinkedHashSet<Long> at line 70 already prevents duplicate entries. The explicit conditional at lines 98-100 is harmless and documents intent, but could be simplified.

♻️ Optional simplification
-                    // If controller is null, id remains 0L, acting as an anonymous catch-all
-                    // controller so that devices whose parent cannot be identified are not lost.
-                    if (id != 0L || !usbControllers.contains(0L)) {
-                        usbControllers.add(id);
-                    }
+                    // If controller is null, id remains 0L, acting as an anonymous catch-all
+                    // controller so that devices whose parent cannot be identified are not lost.
+                    // LinkedHashSet prevents duplicates, so 0L is only added once.
+                    usbControllers.add(id);

Also applies to: 96-100

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java`
at line 70, The explicit contains check around adding 0L to usbControllers is
redundant because usbControllers is a LinkedHashSet<Long>; replace the
conditional block that checks usbControllers.contains(0L) and then adds 0L with
a direct usbControllers.add(0L) call in MacUsbDeviceFFM to simplify the code
while preserving behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`:
- Around line 85-111: The code currently collapses devices with null controller
into a synthetic controller id 0L and adds it to usbControllers; instead, when
controller == null do not set id to 0L or add it to usbControllers or
nameMap—treat the device as an orphan: create a separate orphan list (e.g.,
orphanUsbDevices/orphanControllerIds) and pass a sentinel (e.g.,
null/Optional/absent parent id) into addDeviceAndChildrenToMaps so children are
recorded without being grouped under 0L; keep existing behavior for real
controllers (use getParentEntry, getRegistryEntryID, getControllerIdByLocation,
nameMap, vendorIdMap/productIdMap) but ensure no MacUsbDevice is synthesized for
id 0L and adjust downstream consumers that iterate usbControllers to include the
orphan list when appropriate.

---

Nitpick comments:
In
`@oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java`:
- Line 70: The explicit contains check around adding 0L to usbControllers is
redundant because usbControllers is a LinkedHashSet<Long>; replace the
conditional block that checks usbControllers.contains(0L) and then adds 0L with
a direct usbControllers.add(0L) call in MacUsbDeviceFFM to simplify the code
while preserving behavior.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 03ba235d-dfd7-48ca-affe-2f9248f8f909

📥 Commits

Reviewing files that changed from the base of the PR and between 6e5b86d and 45f4ee7.

📒 Files selected for processing (7)
  • CHANGELOG.md
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core/src/main/java/oshi/hardware/HardwareAbstractionLayer.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
✅ Files skipped from review due to trivial changes (2)
  • CHANGELOG.md
  • oshi-core/src/main/java/oshi/hardware/HardwareAbstractionLayer.java
📜 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). (7)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (12)
📓 Common learnings
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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.
📚 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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.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/windows/WindowsUsbDevice.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/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
📚 Learning: 2026-04-05T16:15:03.230Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxLogicalVolumeGroupJNA.java:95-97
Timestamp: 2026-04-05T16:15:03.230Z
Learning: In `LinuxLogicalVolumeGroupJNA.getLogicalVolumeGroups()` (oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxLogicalVolumeGroupJNA.java), `physicalVolumesMap.get(e.getKey())` in the final stream mapping cannot return null: both `logicalVolumesMap` and `physicalVolumesMap` are populated via `computeIfAbsent` inside the same conditional block (`!Util.isBlank(vgName) && !Util.isBlank(lvName)`), so every VG key in `logicalVolumesMap` is guaranteed to have a corresponding entry in `physicalVolumesMap`. Do not flag this `.get()` call as a potential null-return issue. The FFM version uses `getOrDefault` as extra defensiveness, not because null is possible here.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.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/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI’s Linux FFM implementations (e.g., LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the corresponding JNA udev code, do not require/flag a missing null check around `udev_enumerate_new()`/`enumerateNew()`. Per libudev semantics, `udev_enumerate_new()` returns NULL only if udev itself is broken (e.g., OOM). Since `udev_new()` is already guarded and is the successful prerequisite for enumeration, treating `udev_enumerate_new()` as non-null is consistent with the established pattern; adding a redundant null guard would be inconsistent.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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, 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.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: When reviewing OSHI Linux FFM/JNA code that uses libudev, do not flag a missing null check on `udev_enumerate_new()` if `udev_new()` has already been guarded. Per libudev semantics, `udev_enumerate_new()` returns NULL only when the udev context itself is broken (e.g., OOM), so a successful `udev_new()` implies `udev_enumerate_new()` should succeed as well. Therefore, lack of a redundant null guard on `udev_enumerate_new()` is acceptable in this pattern.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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/linux/LinuxUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
🔇 Additional comments (11)
oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java (2)

43-53: Javadoc now matches the implemented flat/tree behavior.

Nice update: the description and @param tree text are aligned with the actual return behavior and this refactor’s controller-handling intent.


84-145: Udev lifecycle handling is solid here.

The udev_new() guard plus nested try/finally unref() cleanup for context/enumerator/device objects is clean and resilient.

oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java (3)

24-29: LGTM - Clean refactor to use base class.

The class now correctly extends LinuxUsbDevice to inherit the shared getDeviceAndChildren() tree-builder. The public final modifier with private constructor is appropriate for a static utility helper that instantiates LinuxUsbDevice objects rather than itself.


45-58: LGTM - Correct flat-list implementation excluding controllers.

The Javadoc accurately describes the new behavior and the implementation correctly excludes controllers from the flat list by iterating over device.getConnectedDevices() rather than including the controller device itself. This aligns with the PR objective to consistently exclude controllers from flat device lists across all platforms.


149-154: LGTM - Correct delegation to inherited tree-builder.

The call to getDeviceAndChildren() correctly passes the controller path (String) and all required maps to the inherited method from LinuxUsbDevice, which handles the recursive tree construction.

oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java (3)

33-36: LGTM - Clean refactor to use base class.

The class now correctly extends MacUsbDevice to inherit the shared getDeviceAndChildren() tree-builder. The public final modifier with private constructor is appropriate for a static utility helper.


50-59: LGTM - Correctly excludes controllers from flat list.

The implementation now iterates over device.getConnectedDevices() to build the flat list, which correctly excludes controller nodes. This addresses the previous review feedback and aligns with the PR objective for consistent controller exclusion across platforms.


114-118: LGTM - Correct delegation to inherited tree-builder.

The call to getDeviceAndChildren() correctly passes the controller registry entry ID (Long) and all required maps to the inherited method from MacUsbDevice.

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

56-59: Please confirm the flat-list semantics here.

AbstractUsbDevice.addDevicesToList() (oshi-core/src/main/java/oshi/hardware/common/AbstractUsbDevice.java:86-91) appends the existing tree nodes, so tree=false now returns hubs/devices whose getConnectedDevices() can still be non-empty. That keeps controllers out of the top-level list, but it also means recursive consumers can double-count descendants after asking for a “flat” result. If that behavior is intentional, the Javadoc on Lines 45-48 should probably clarify that only the top-level list is flat.

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

39-60: Flat-mode filtering matches the docs now.

Tree mode returns controller roots, and flat mode only flattens each controller’s connected devices, so controllers stay out of the flat list.


174-185: Good null-guard before recursing into child devices.

The childIter != null check avoids dereferencing a missing iterator before calling next().

…c, catch-all controller comments, null guards, deduplication, and changelog

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java`:
- Around line 215-234: The loop prematurely sets found=true when either
vendor-id or device-id is found, causing the other ID to remain "0000"; update
the logic in the matchingService loop (which uses variables matchingService,
parent, vendorIdMap, productIdMap, found, and serviceIterator) so that you only
set found=true (and break) after both IDs have been located for the given id.
Concretely, read vendor-id and device-id from parent via getByteArrayProperty as
you already do, store each into vendorIdMap and productIdMap when present, and
only mark found=true (or exit the loop) when both maps contain an entry for id;
otherwise continue iterating serviceIterator.next() to check subsequent
matchingService entries.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0786fcdc-ca54-4ce7-aebe-cd322aecd81e

📥 Commits

Reviewing files that changed from the base of the PR and between 45f4ee7 and af249c1.

📒 Files selected for processing (7)
  • CHANGELOG.md
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core/src/main/java/oshi/hardware/HardwareAbstractionLayer.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
✅ Files skipped from review due to trivial changes (2)
  • CHANGELOG.md
  • oshi-core/src/main/java/oshi/hardware/HardwareAbstractionLayer.java
🚧 Files skipped from review as they are similar to previous changes (2)
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
📜 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). (1)
  • GitHub Check: Codacy Static Code Analysis
🧰 Additional context used
🧠 Learnings (15)
📓 Common learnings
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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.
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:09.616Z
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.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.
📚 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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
📚 Learning: 2026-04-06T21:48:09.616Z
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:09.616Z
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/windows/WindowsUsbDevice.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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/windows/WindowsUsbDevice.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/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.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-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI’s Linux FFM implementations (e.g., LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the corresponding JNA udev code, do not require/flag a missing null check around `udev_enumerate_new()`/`enumerateNew()`. Per libudev semantics, `udev_enumerate_new()` returns NULL only if udev itself is broken (e.g., OOM). Since `udev_new()` is already guarded and is the successful prerequisite for enumeration, treating `udev_enumerate_new()` as non-null is consistent with the established pattern; adding a redundant null guard would be inconsistent.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: When reviewing OSHI Linux FFM/JNA code that uses libudev, do not flag a missing null check on `udev_enumerate_new()` if `udev_new()` has already been guarded. Per libudev semantics, `udev_enumerate_new()` returns NULL only when the udev context itself is broken (e.g., OOM), so a successful `udev_new()` implies `udev_enumerate_new()` should succeed as well. Therefore, lack of a redundant null guard on `udev_enumerate_new()` is acceptable in this pattern.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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, 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.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-06T21:48:09.616Z
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:09.616Z
Learning: In `oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java` and `MacUsbDeviceFFM.java`, treat `id = 0L` as an intentional sentinel when `device.getParentEntry(IOSERVICE)` returns null. This catch-all value groups devices whose controller cannot be identified (even though that edge case is expected not to occur). Do not review it as a bug, do not suggest skipping those devices, and do not propose moving them to the top level without controller context—this design explicitly avoids silently dropping orphaned devices and preserves controller-grouping behavior.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.java
📚 Learning: 2026-04-05T16:15:03.230Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxLogicalVolumeGroupJNA.java:95-97
Timestamp: 2026-04-05T16:15:03.230Z
Learning: In `LinuxLogicalVolumeGroupJNA.getLogicalVolumeGroups()` (oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxLogicalVolumeGroupJNA.java), `physicalVolumesMap.get(e.getKey())` in the final stream mapping cannot return null: both `logicalVolumesMap` and `physicalVolumesMap` are populated via `computeIfAbsent` inside the same conditional block (`!Util.isBlank(vgName) && !Util.isBlank(lvName)`), so every VG key in `logicalVolumesMap` is guaranteed to have a corresponding entry in `physicalVolumesMap`. Do not flag this `.get()` call as a potential null-return issue. The FFM version uses `getOrDefault` as extra defensiveness, not because null is possible here.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceJNA.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-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/MacUsbDeviceJNA.java
🔇 Additional comments (8)
oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java (3)

24-29: Good refactoring to utility class pattern.

The class is now public final with a private no-arg constructor, correctly implementing the utility class pattern since all methods are static. Extending LinuxUsbDevice allows inheriting the shared getDeviceAndChildren method, reducing duplication.


45-46: Controller exclusion in flat mode is now correct per API contract.

The implementation now correctly excludes controllers from the flat list by iterating over device.getConnectedDevices() rather than including the controller device itself:

for (UsbDevice device : devices) {
    addDevicesToList(deviceList, device.getConnectedDevices());  // controllers excluded
}

This aligns with the PR objective to consistently exclude controllers from the flat device list across all platforms. The Javadoc at lines 45-46 correctly reflects this behavior. Based on learnings, the previous behavior that included controllers was documented as intentional, but this PR explicitly changes that to align with the HardwareAbstractionLayer API contract.

Also applies to: 49-58


61-155: LGTM - queryUsbDevices implementation.

The method has been appropriately renamed from the internal getUsbDevices() to queryUsbDevices() to avoid confusion with the public API method. The udev enumeration logic is unchanged and correctly:

  • Guards udev_new() for null (line 76)
  • Does not redundantly guard udev_enumerate_new() (consistent with established pattern per learnings)
  • Delegates tree-building to inherited getDeviceAndChildren (line 151)
  • Uses statically imported emptyList() for cleaner code
oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java (4)

2-2: Copyright year update looks good.

Standard maintenance update to reflect the current year.


8-8: Static import refactor is acceptable.

The static import of Collections.sort and its use at line 113 is a minor style change that works correctly.

Also applies to: 113-113


51-61: Flat list logic correctly excludes controllers and uses shared helper.

The refactored code properly excludes USB controllers from the flat list by iterating over controllers but only adding their getConnectedDevices() to the list via the inherited addDevicesToList(). This aligns with the Javadoc contract and the PR objective of consistent controller exclusion across platforms.


83-125: Device tree building logic is sound.

The queryDeviceAndChildren method correctly builds the hierarchical USB device tree by recursively parsing parent-child relationships and constructing WindowsUsbDevice instances. The change to use the statically imported sort() is purely stylistic.

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

51-60: Flat-mode filtering looks correct.

Flattening from each controller’s getConnectedDevices() drops the controller roots while preserving every descendant, so this now matches the updated Javadoc and expected HAL behavior.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java`:
- Line 134: Update the Javadoc return description in the OpenBsdUsbDevice class
by changing the article from "A" to "An" so the line reads "An OpenBsdUsbDevice
corresponding to this device" to fix the grammar in the method/class Javadoc.
🪄 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: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 74611f25-4d84-4c96-ba78-6f1add778208

📥 Commits

Reviewing files that changed from the base of the PR and between af249c1 and 8d7a099.

📒 Files selected for processing (13)
  • oshi-core-java25/src/main/java/oshi/ffm/linux/LinuxLibcFunctions.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core/src/main/java/oshi/SystemInfo.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • pom.xml
✅ Files skipped from review due to trivial changes (3)
  • pom.xml
  • oshi-core/src/main/java/oshi/SystemInfo.java
  • oshi-core-java25/src/main/java/oshi/ffm/linux/LinuxLibcFunctions.java
🚧 Files skipped from review as they are similar to previous changes (4)
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacHardwareAbstractionLayerJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacUsbDevice.java
📜 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
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 11, ubuntu-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 11, windows-latest
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Test JDK 11, macos-latest
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (14)
📓 Common learnings
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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.
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:09.616Z
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.
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.
📚 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: In OSHI's Linux USB device implementations (LinuxUsbDeviceFFM, and the JNA equivalent), when `getUsbDevices(boolean tree)` is called with `tree=false`, the returned flat list intentionally **includes** controller nodes — it does not exclude them. The existing Javadoc that says "returns a flat list excluding controllers" is incorrect. Do not flag the implementation for including controllers in flat mode; instead, flag only the Javadoc if it claims controllers are excluded.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
📚 Learning: 2026-04-06T21:48:09.616Z
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:09.616Z
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/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.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/unix/solaris/SolarisUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
📚 Learning: 2026-04-03T18:44:46.910Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3112
File: oshi-core/src/main/java/oshi/PlatformEnum.java:63-66
Timestamp: 2026-04-03T18:44:46.910Z
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/unix/openbsd/OpenBsdUsbDevice.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.java
📚 Learning: 2026-04-03T18:43:50.370Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3112
File: oshi-core/src/main/java/oshi/PlatformEnum.java:99-100
Timestamp: 2026-04-03T18:43:50.370Z
Learning: In oshi/oshi PlatformEnum.java, the DragonFly BSD detection uses `osName.equalsIgnoreCase("dragonflybsd")` intentionally, mirroring the exact same check in JNA's `Platform.java` source. Java's `os.name` property on DragonFly BSD is "dragonflybsd" (no space), so `startsWith("DragonFly")` is NOT the correct pattern here. Do not suggest changing it to `startsWith`.

Applied to files:

  • oshi-core/src/main/java/oshi/hardware/platform/unix/freebsd/FreeBsdUsbDevice.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/MacUsbDeviceFFM.java
  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI's Linux FFM and JNA udev code, a null guard on `udev_enumerate_new()` is intentionally absent and should not be flagged. This is consistent across all four FFM classes (LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the JNA implementation (which also does not guard `enumerateNew()` for null). Per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` (which is guarded) implies `udev_enumerate_new()` will succeed. Adding a null guard would be both redundant and inconsistent with the established pattern.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: In OSHI's Linux FFM/JNA udev code, a null guard on `udev_enumerate_new()` is unnecessary if `udev_new()` has already been guarded: per libudev semantics, `udev_enumerate_new()` only returns NULL when udev itself is broken (e.g. OOM), so a successful `udev_new()` implies `udev_enumerate_new()` will succeed as well. Do not flag missing null checks on `udev_enumerate_new()` as an issue.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.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-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:40:43.357Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:40:43.357Z
Learning: In OSHI’s Linux FFM implementations (e.g., LinuxPowerSourceFFM, LinuxHWDiskStoreFFM, LinuxLogicalVolumeGroupFFM, LinuxUsbDeviceFFM) and the corresponding JNA udev code, do not require/flag a missing null check around `udev_enumerate_new()`/`enumerateNew()`. Per libudev semantics, `udev_enumerate_new()` returns NULL only if udev itself is broken (e.g., OOM). Since `udev_new()` is already guarded and is the successful prerequisite for enumeration, treating `udev_enumerate_new()` as non-null is consistent with the established pattern; adding a redundant null guard would be inconsistent.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.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, 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.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
📚 Learning: 2026-04-05T05:39:18.791Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3117
File: oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxPowerSourceFFM.java:62-65
Timestamp: 2026-04-05T05:39:18.791Z
Learning: When reviewing OSHI Linux FFM/JNA code that uses libudev, do not flag a missing null check on `udev_enumerate_new()` if `udev_new()` has already been guarded. Per libudev semantics, `udev_enumerate_new()` returns NULL only when the udev context itself is broken (e.g., OOM), so a successful `udev_new()` implies `udev_enumerate_new()` should succeed as well. Therefore, lack of a redundant null guard on `udev_enumerate_new()` is acceptable in this pattern.

Applied to files:

  • oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java
🔇 Additional comments (9)
oshi-core/src/main/java/oshi/hardware/platform/unix/solaris/SolarisUsbDevice.java (1)

7-8: LGTM! Flattening logic correctly excludes controllers.

The refactored getUsbDevices(tree=false) path now iterates over controller devices and only adds their getConnectedDevices() to the flat list via the inherited addDevicesToList() helper. This correctly excludes controller nodes from the flat list, aligning with the HardwareAbstractionLayer API contract.

Also applies to: 46-56

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

52-62: LGTM! Consistent refactoring pattern applied.

The tree=false path correctly excludes controller devices by iterating over them and only adding their getConnectedDevices() via the inherited addDevicesToList() method.

oshi-core/src/main/java/oshi/hardware/platform/unix/openbsd/OpenBsdUsbDevice.java (1)

42-52: LGTM! Flattening logic correctly excludes controllers.

The implementation follows the consistent refactoring pattern across platforms.

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

45-55: LGTM! Consistent implementation with inherited helper.

The tree=false path correctly leverages the inherited addDevicesToList() from AbstractUsbDevice to recursively flatten the device tree while excluding controller nodes.

oshi-core-java25/src/main/java/oshi/hardware/platform/linux/LinuxUsbDeviceFFM.java (2)

26-29: LGTM! Clean utility class pattern.

The class is now final with a private no-arg constructor, extending LinuxUsbDevice to inherit the shared getDeviceAndChildren() and addDevicesToList() helpers. This correctly follows the refactoring pattern.


49-59: LGTM! Flattening logic correctly excludes controllers.

The tree=false path iterates controller devices and adds only their getConnectedDevices(), correctly excluding controller nodes from the flat list.

oshi-core-java25/src/main/java/oshi/hardware/platform/mac/MacUsbDeviceFFM.java (3)

34-37: LGTM! Clean utility class pattern.

The class is now final with a private no-arg constructor, extending MacUsbDevice to inherit shared helpers.


51-61: LGTM! Controller exclusion issue addressed.

The tree=false path now correctly excludes controller roots by iterating over them and adding only their getConnectedDevices() via the inherited addDevicesToList() method.


71-71: LGTM! Duplicate controller ID issue addressed.

Using LinkedHashSet<Long> instead of a List ensures the anonymous 0L catch-all controller (and all controller IDs) are added only once while preserving insertion order. The comment at lines 97-98 clearly documents the intentional catch-all behavior.

Also applies to: 97-99

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@dbwiddis
dbwiddis merged commit 2b4a349 into oshi:master Apr 6, 2026
14 checks passed
@dbwiddis
dbwiddis deleted the refactor-usb branch April 6, 2026 22:29
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