Skip to content

Hoist the NetworkIF enumeration loop into AbstractNetworkIF - #3455

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:dedup-networkif-enumeration
Jul 13, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:dedup-networkif-enumeration

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jul 13, 2026 •

Copy link
Copy Markdown
Member

Summary

First slice of Item 6 (NetworkIF) in the cross-OS dedup audit. Every platform's getNetworks(boolean) hand-rolled the identical enumeration loop — walk getNetworkInterfaces(includeLocal), construct the platform-specific NetworkIF for each, and log+skip any that throw InstantiationException. Only the constructed type differed (and, for Mac/AIX, some pre-queried bulk data).

SolarisNetworkIF already proved the fix with a local NetworkIFFactory + getNetworks(boolean, factory) helper. This promotes that helper up to AbstractNetworkIF and adopts it across all ~14 implementations.

Changes

  • AbstractNetworkIF: add the NetworkIFFactory functional interface + shared getNetworks(boolean, factory) (using the existing LOG and getNetworkInterfaces).
  • Simple platforms (Linux, Windows, BSD, NetBSD, native-free) delegate with a constructor ref: return getNetworks(includeLocal, LinuxNetworkIFJNA::new);
  • Bulk-data platforms (Mac, AIX) query once and capture it in the factory lambda: return getNetworks(includeLocal, ni -> new MacNetworkIfJNA(ni, data));
  • Solaris: local factory/helper removed; inherits the hoisted one (JNA/FFM callers unchanged).
  • Consistency fix: LinuxNetworkIFFFM and LinuxNetworkIfNF logged e.toString() while everyone else logged e.getMessage(); the shared helper uses getMessage() for all. Now-orphaned LOG fields dropped.

Net −132 lines (13 files); ~14 copies of the loop → one helper.

Behavior

No user-facing change — same enumeration, same skip-on-failure semantics; only that one debug-log line is unified.

Testing

  • spotless / checkstyle / forbiddenapis / javadoc: clean
  • Clean full-reactor install: BUILD SUCCESS, 0 tests failed
  • NativeComparisonTest NetworkIF JNA==FFM comparison: passes on macOS

Follow-ups (queued, separate PRs)

The fatter per-platform native updateAttributes/model logic is left for follow-ups (they need native-call abstraction, not a mechanical hoist): the Linux udev model lookup, and the Mac/Windows/AIX stat queries. Note WindowsNetworkIfJNA.updateAttributes has a Vista-gated legacy path (MIB_IFROW2 vs MIB_IFROW) — a target for the seam + differential-legacy-test pattern (cf. #3454) when that PR lands.

Summary by CodeRabbit

  • Improvements
    • Simplified network-interface discovery across supported platforms.
    • Network interfaces that cannot be initialized are now skipped gracefully, allowing other available interfaces to be returned.
    • Local-interface inclusion behavior remains supported.
  • Maintenance
    • Standardized network-interface handling and reduced duplicated processing across platform implementations.

Every platform's getNetworks(boolean) hand-rolled the same loop: enumerate
getNetworkInterfaces(includeLocal), construct the platform-specific NetworkIF
for each, and log+skip any that fail to instantiate. Only the constructed type
(and, for Mac/Aix, some pre-queried bulk data) differed.

SolarisNetworkIF already had a local NetworkIFFactory + getNetworks(boolean,
factory) helper; promote it to AbstractNetworkIF and adopt it everywhere. The
simple platforms delegate with a constructor reference; Mac and AIX capture
their pre-queried data (interface stats) in the factory lambda. Solaris's local
copy is removed in favor of the inherited one.

Along the way this unifies an inconsistency: LinuxNetworkIFFFM and
LinuxNetworkIfNF logged e.toString() while every other platform logged
e.getMessage(); the shared helper uses getMessage() for all. Now-orphaned LOG
fields are dropped.

No user-facing change. NetworkIF JNA==FFM comparison passes.
@coderabbitai

coderabbitai Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 25ab61ac-bb32-44e0-995d-d5c966e61a2c

📥 Commits

Reviewing files that changed from the base of the PR and between f13e966 and 6632792.

📒 Files selected for processing (13)
  • oshi-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxNetworkIfNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdNetworkIF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdNetworkIF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/solaris/SolarisNetworkIF.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/linux/LinuxNetworkIFFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/mac/MacNetworkIfFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/aix/AixNetworkIFFFM.java
  • oshi-core-ffm/src/main/java/oshi/hardware/platform/windows/WindowsNetworkIfFFM.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxNetworkIFJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacNetworkIfJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/aix/AixNetworkIFJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsNetworkIfJNA.java
💤 Files with no reviewable changes (1)
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/solaris/SolarisNetworkIF.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (12)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 26, windows-latest
  • GitHub Check: Test JDK 21, windows-latest
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 21, macos-latest
  • GitHub Check: Test JDK 26, macos-latest
  • GitHub Check: Test JDK 25, FreeBSD (+ JNA==FFM)
  • GitHub Check: Test JDK 25, OpenBSD (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, OpenIndiana (+ JNA==FFM)
🧰 Additional context used
🧠 Learnings (3)
📚 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/WindowsNetworkIfJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/unix/aix/AixNetworkIFJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/linux/LinuxNetworkIFJNA.java
  • oshi-core/src/main/java/oshi/hardware/platform/mac/MacNetworkIfJNA.java
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.

Applied to files:

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

Applied to files:

  • oshi-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/netbsd/NetBsdNetworkIF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/linux/nativefree/LinuxNetworkIfNF.java
  • oshi-common/src/main/java/oshi/hardware/common/platform/unix/BsdNetworkIF.java
🔇 Additional comments (12)
oshi-common/src/main/java/oshi/hardware/common/AbstractNetworkIF.java (1)

148-181: LGTM!

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

30-32: LGTM!

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

33-35: LGTM!

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

33-35: LGTM!

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

12-13: LGTM!

Also applies to: 76-76

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

12-14: LGTM!

Also applies to: 68-69

oshi-core-ffm/src/main/java/oshi/hardware/platform/unix/aix/AixNetworkIFFFM.java (1)

11-13: LGTM!

Also applies to: 24-25, 43-43

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

13-14: LGTM!

Also applies to: 46-46

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

10-11: LGTM!

Also applies to: 27-28, 69-69

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

8-10: LGTM!

Also applies to: 28-29, 64-65

oshi-core/src/main/java/oshi/hardware/platform/unix/aix/AixNetworkIFJNA.java (1)

11-13: LGTM!

Also applies to: 27-28, 46-46

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

7-8: LGTM!

Also applies to: 53-53


📝 Walkthrough

Walkthrough

Network interface enumeration now uses a shared factory-based helper in AbstractNetworkIF. Common, FFM, and JNA platform implementations delegate construction to that helper and remove duplicated list management and instantiation logging.

Changes

Network interface enumeration

Layer / File(s) Summary
Shared factory and common-platform migration
oshi-common/src/main/java/oshi/hardware/common/...
AbstractNetworkIF adds factory-based enumeration and centralized InstantiationException handling; common platform implementations delegate to it, including removal of Solaris’s duplicate helper.
FFM platform delegation
oshi-core-ffm/src/main/java/oshi/hardware/platform/...
Linux, macOS, AIX, and Windows FFM implementations use factory-aware enumeration helpers and remove local list construction and logging.
JNA platform delegation
oshi-core/src/main/java/oshi/hardware/platform/...
Linux, macOS, AIX, and Windows JNA implementations delegate interface construction and remove duplicated imports, logger fields, and exception handling.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • oshi/oshi#3148: Adds a MacNetworkIF base class supporting the factory-based construction pattern.
  • oshi/oshi#3368: Directly overlaps with moving the factory helper between SolarisNetworkIF and AbstractNetworkIF.

Poem

A bunny hops through interfaces bright,
Factories build each link just right.
Failed creations quietly rest,
While shared helpers do their best.
Less repeated code—what a delight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.93% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: moving shared NetworkIF enumeration logic into AbstractNetworkIF.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.65%. Comparing base (f13e966) to head (6632792).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...n/java/oshi/hardware/common/AbstractNetworkIF.java 75.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3455      +/-   ##
============================================
- Coverage     74.68%   74.65%   -0.03%     
  Complexity       76       76              
============================================
  Files           584      584              
  Lines         21923    21862      -61     
  Branches       3679     3671       -8     
============================================
- Hits          16373    16321      -52     
+ Misses         3508     3495      -13     
- Partials       2042     2046       +4     
Flag Coverage Δ
freebsd 26.91% <38.88%> (+0.07%) ⬆️
linux 31.55% <50.00%> (+0.05%) ⬆️
macos 32.06% <55.55%> (+0.03%) ⬆️
openbsd 26.38% <38.88%> (+0.06%) ⬆️
solaris 28.46% <33.33%> (+0.07%) ⬆️
windows 39.90% <44.44%> (+0.09%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit 04942e0 into oshi:master Jul 13, 2026
20 checks passed
@dbwiddis
dbwiddis deleted the dedup-networkif-enumeration branch July 13, 2026 00:24
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