Repository navigation
Introduce common WmiQueryExecutor interface and WmiUtil - #3266
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (8)
🚧 Files skipped from review as they are similar to previous changes (7)
📜 Recent review details⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
🧰 Additional context used🧠 Learnings (1)📚 Learning: 2026-04-12T19:22:09.716ZApplied to files:
🔇 Additional comments (1)
📝 WalkthroughWalkthroughThis PR adds a common Windows WMI abstraction: type constants, WmiQuery/WmiResult/WmiQueryExecutor contracts, WmiUtil typed accessors, platform adapters for JNA/FFM, and a Win32DiskDrive helper that uses the new query executor. ChangesCommon WMI Query Abstraction
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (4)
oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQuery.java (1)
35-39: ⚡ Quick winConsider validating constructor parameters.
The constructor accepts three parameters but does not validate them for null. If callers pass null values, errors will occur later during query execution rather than at construction time.
🛡️ Proposed validation
public WmiQuery(String nameSpace, String wmiClassName, Class<T> propertyEnum) { + if (nameSpace == null || wmiClassName == null || propertyEnum == null) { + throw new IllegalArgumentException("WmiQuery parameters cannot be null"); + } this.nameSpace = nameSpace; this.wmiClassName = wmiClassName; this.propertyEnum = propertyEnum; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQuery.java` around lines 35 - 39, Validate constructor parameters in WmiQuery by checking nameSpace, wmiClassName, and propertyEnum for null and throwing a clear exception (e.g., NullPointerException or IllegalArgumentException) if any are null; update the WmiQuery(String nameSpace, String wmiClassName, Class<T> propertyEnum) constructor to perform these non-null checks and assign only after validation so callers fail fast with a descriptive message identifying the missing parameter.oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiUtil.java (2)
87-93: ⚡ Quick wingetDateString returns empty string for UNIX_EPOCH.
Lines 89-91 return an empty string when the parsed datetime equals
Constants.UNIX_EPOCH. This is consistent with the pattern of returning empty strings for null values, but the Javadoc at line 85 says "The stored value if non-null, an empty-string otherwise" which doesn't mention the epoch case. Consider updating the Javadoc to clarify this behavior.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiUtil.java` around lines 87 - 93, Update the Javadoc for WmiUtil.getDateString to explicitly state that it returns the stored value if non-null and non-epoch, and returns an empty string for null or when the parsed datetime equals Constants.UNIX_EPOCH; reference the method name getDateString and the helper getDateTime plus Constants.UNIX_EPOCH so reviewers can locate the logic that treats the epoch as an empty value.
50-59: ⚡ Quick winAdd validation to handle empty property enums in queryToString.
The method accesses
props[0]at line 53 without verifying the enum has constants. If aWmiQueryis instantiated with an empty enum (wheregetEnumConstants()returns an empty array), this will throwArrayIndexOutOfBoundsException. Although all existing enums in the codebase contain at least one constant, this defensive check would prevent potential issues from future code changes.Suggested fix location
public static <T extends Enum<T>> String queryToString(WmiQuery<T> query) { T[] props = query.getPropertyEnum().getEnumConstants(); if (props.length == 0) { throw new IllegalArgumentException("Property enum must contain at least one constant"); } StringBuilder sb = new StringBuilder("SELECT "); sb.append(props[0].name()); for (int i = 1; i < props.length; i++) { sb.append(',').append(props[i].name()); } sb.append(" FROM ").append(query.getWmiClassName()); return sb.toString(); }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiUtil.java` around lines 50 - 59, The method queryToString in WmiUtil reads props[0] without checking that the enum returned by query.getPropertyEnum().getEnumConstants() is non-empty; add a defensive check that the array length is > 0 and throw a clear IllegalArgumentException (or similar) if empty so we avoid ArrayIndexOutOfBoundsException; update queryToString to retrieve T[] props = query.getPropertyEnum().getEnumConstants(), validate props.length > 0, then proceed to build the SELECT... FROM using props[0].name() and the existing loop.oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiResult.java (1)
21-28: ⚡ Quick winConsider documenting behavior for invalid indices.
The
getValuemethod does not specify what happens whenindexis out of bounds (e.g., negative or >=getResultCount()). Documenting the expected behavior (throw exception, return null, etc.) would help implementers and callers understand the contract.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiResult.java` around lines 21 - 28, Update the Javadoc for WmiResult#getValue(T property, int index) to document behavior for invalid indices: state that index must be in the range 0 .. getResultCount()-1 and that passing a negative index or an index >= getResultCount() will result in an IndexOutOfBoundsException (or explicitly state null-return behavior if you prefer that contract instead), and mention that callers should use getResultCount() to validate indices before calling getValue.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQuery.java`:
- Around line 35-39: Validate constructor parameters in WmiQuery by checking
nameSpace, wmiClassName, and propertyEnum for null and throwing a clear
exception (e.g., NullPointerException or IllegalArgumentException) if any are
null; update the WmiQuery(String nameSpace, String wmiClassName, Class<T>
propertyEnum) constructor to perform these non-null checks and assign only after
validation so callers fail fast with a descriptive message identifying the
missing parameter.
In `@oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiResult.java`:
- Around line 21-28: Update the Javadoc for WmiResult#getValue(T property, int
index) to document behavior for invalid indices: state that index must be in the
range 0 .. getResultCount()-1 and that passing a negative index or an index >=
getResultCount() will result in an IndexOutOfBoundsException (or explicitly
state null-return behavior if you prefer that contract instead), and mention
that callers should use getResultCount() to validate indices before calling
getValue.
In `@oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiUtil.java`:
- Around line 87-93: Update the Javadoc for WmiUtil.getDateString to explicitly
state that it returns the stored value if non-null and non-epoch, and returns an
empty string for null or when the parsed datetime equals Constants.UNIX_EPOCH;
reference the method name getDateString and the helper getDateTime plus
Constants.UNIX_EPOCH so reviewers can locate the logic that treats the epoch as
an empty value.
- Around line 50-59: The method queryToString in WmiUtil reads props[0] without
checking that the enum returned by query.getPropertyEnum().getEnumConstants() is
non-empty; add a defensive check that the array length is > 0 and throw a clear
IllegalArgumentException (or similar) if empty so we avoid
ArrayIndexOutOfBoundsException; update queryToString to retrieve T[] props =
query.getPropertyEnum().getEnumConstants(), validate props.length > 0, then
proceed to build the SELECT... FROM using props[0].name() and the existing loop.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: a095e3d3-7c12-4a6f-8da2-291cfd98d73b
📒 Files selected for processing (8)
oshi-common/src/main/java/oshi/driver/common/windows/wmi/Win32DiskDrive.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiConstants.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQuery.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQueryExecutor.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiResult.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiUtil.javaoshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WmiQueryExecutorFFM.javaoshi-core/src/main/java/oshi/util/platform/windows/WmiQueryExecutorJNA.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 21, macos-latest
- GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
- GitHub Check: Test JDK 11, windows-latest
- GitHub Check: Test JDK 11, macos-latest
- GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
- GitHub Check: JMH Benchmarks, ubuntu-latest
- GitHub Check: JMH Benchmarks, macos-latest
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (1)
📚 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/driver/common/windows/wmi/WmiQueryExecutor.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiResult.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/Win32DiskDrive.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQuery.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiConstants.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiUtil.java
🔇 Additional comments (5)
oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiConstants.java (1)
1-40: LGTM!oshi-core/src/main/java/oshi/util/platform/windows/WmiQueryExecutorJNA.java (1)
1-88: LGTM!oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WmiQueryExecutorFFM.java (1)
1-86: LGTM!oshi-common/src/main/java/oshi/driver/common/windows/wmi/Win32DiskDrive.java (1)
52-55: ⚡ Quick winCOM initialization is properly handled by all callers. The Javadoc requirement that "User should have already initialized COM" at line 49 is correctly enforced and followed throughout the codebase. All call sites either explicitly initialize COM or receive handlers that are pre-initialized. This is consistent with the parallel implementations in Win32DiskDriveJNA and Win32DiskDriveFFM, which use the same pattern.
oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQueryExecutor.java (1)
43-45: 💤 Low valueThe static
createInstance()method is already well-documented; no changes required.The interface already includes comprehensive Javadoc (lines 35-42) that clearly explains the factory contract: it directs callers to platform-specific implementations (
WmiQueryExecutorJNAfor JNA,WmiQueryExecutorFFMfor FFM). This static method serves as a documented entry point that defines the factory pattern contract for the interface. Keeping it provides valuable documentation of the expected usage pattern and is consistent with the architecture.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3266 +/- ##
============================================
- Coverage 74.36% 74.15% -0.22%
Complexity 73 73
============================================
Files 401 404 +3
Lines 16039 16070 +31
Branches 2555 2556 +1
============================================
- Hits 11928 11916 -12
- Misses 2626 2661 +35
- Partials 1485 1493 +8
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
bb0c321 to
5c8376e
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@oshi-core/src/main/java/oshi/util/platform/windows/WmiQueryExecutorJNA.java`:
- Around line 25-27: Constructor WmiQueryExecutorJNA and public methods that
accept a query need fail-fast null checks: add explicit null validation (e.g.,
Objects.requireNonNull or if (x==null) throw new NullPointerException("...")) in
the WmiQueryExecutorJNA(WmiQueryHandler handler) constructor for parameter
handler and in the public query-taking method(s) (the executeQuery/other method
around lines 54-57 that accepts String query) to validate query and throw a
clear NPE message; reference the WmiQueryExecutorJNA class, its constructor
WmiQueryExecutorJNA(WmiQueryHandler handler), and the public method that accepts
String query when applying these checks.
🪄 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: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 646724f2-003f-4c51-af56-72042052b069
📒 Files selected for processing (8)
oshi-common/src/main/java/oshi/driver/common/windows/wmi/Win32DiskDrive.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiConstants.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQuery.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQueryExecutor.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiResult.javaoshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiUtil.javaoshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WmiQueryExecutorFFM.javaoshi-core/src/main/java/oshi/util/platform/windows/WmiQueryExecutorJNA.java
🚧 Files skipped from review as they are similar to previous changes (7)
- oshi-common/src/main/java/oshi/driver/common/windows/wmi/Win32DiskDrive.java
- oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQuery.java
- oshi-core-ffm/src/main/java/oshi/ffm/util/platform/windows/WmiQueryExecutorFFM.java
- oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiQueryExecutor.java
- oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiResult.java
- oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiConstants.java
- oshi-common/src/main/java/oshi/driver/common/windows/wmi/WmiUtil.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 (+ JNA==FFM)
- 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 11, macos-latest
- GitHub Check: JMH Benchmarks, macos-latest
- GitHub Check: JMH Benchmarks, ubuntu-latest
- GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
- GitHub Check: JMH Benchmarks, windows-latest
- GitHub Check: Analyze (java)
5c8376e to
77125f0
Compare
Add shared WMI infrastructure to oshi-common: - WmiQuery<T>: common query class (namespace, className, propertyEnum) - WmiResult<T>: common result interface (getValue, getResultCount, etc.) - WmiQueryExecutor: common handler interface (queryWMI methods) - WmiConstants: CIM/VT type constants - WmiUtil: typed result extraction helpers (getString, getUint32, etc.) Add platform adapters: - WmiQueryExecutorJNA wraps WmiQueryHandler with JnaWmiResult adapter - WmiQueryExecutorFFM wraps WmiQueryHandlerFFM with FfmWmiResult adapter Migrate Win32DiskDrive.queryDiskDrive as proof of concept. Follow-up PRs will migrate the remaining 20 driver classes and their callers to use the common types.
77125f0 to
282df4f
Compare
Partial fix for #3257
Adds shared WMI infrastructure to
oshi-commonenabling consolidation of the 21 duplicated JNA/FFM WMI driver pairs.New in oshi-common:
WmiQuery<T>— common query classWmiResult<T>— common result interfaceWmiQueryExecutor— common handler interfaceWmiConstants— CIM/VT type constantsWmiUtil— typed result extraction helpers (getString, getUint32, getUint64, etc.)New adapters:
WmiQueryExecutorJNA(oshi-core) — wrapsWmiQueryHandlerWmiQueryExecutorFFM(oshi-core-ffm) — wrapsWmiQueryHandlerFFMProof of concept:
Win32DiskDrive.queryDiskDrive(WmiQueryExecutor)added to the common base classThe existing
WmiQueryHandlerclass and its extensibility pattern (used inoshi-demofor COM init customization) are preserved unchanged. A follow-up PR will migrate the remaining 20 drivers and their callers.Summary by CodeRabbit
Refactor
New Features