On Wed, 2 Sep 2026 19:00:08 GMT, Alan Bateman <[email protected]> wrote:
>> ## Summary
>> - Return an empty `ServiceLoader` when the service type is an array class
>> (e.g. `Object[].class`)
>> - Fixes misleading `ServiceConfigurationError` ("service type not accessible
>> to unnamed module") for invalid service types
>> - Array classes can never have registered service providers, so an empty
>> loader is the correct result
>> - Matches behavior observed on Eclipse OpenJ9 (see JDK-8379812)
>>
>> - [x] I confirm that I make this contribution in accordance with the
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>>
>> ## Problem
>> `ServiceLoader.loadInstalled()` with an array service type (e.g. from
>> `MethodType.genericMethodType(1, true).parameterType(1)`) throws
>> `ServiceConfigurationError` because module access checks in `checkCaller()`
>> fail for array types.
>>
>> ## Solution
>> - Detect array service types via `Class.isArray()` before `checkCaller()`
>> - Return a dedicated empty `ServiceLoader` whose iterator/stream yield no
>> providers
>> - Apply to all public `load*` entry points
>>
>> ## Test plan
>> - [x] Added `test/jdk/java/util/ServiceLoader/ArrayServiceTypeTest.java`
>> - [ ] `make run-test
>> TEST=jdk/java/util/ServiceLoader/ArrayServiceTypeTest.java`
>> - [ ] `make run-test TEST=jdk/java/util/ServiceLoader` (regression)
>> - [ ] Manual repro from JDK-8379812 (`temp.java`) completes without exception
>
> Please see my comment in the JBS issue from March, it is still TBD if these
> cases should be specified to throw (SCE or IAE) as it is nonsensical to
> invoke ServiceLoader with an array class.
>
> A useful contribution would be to look into how frameworks or libraries end
> up accidentally calling ServiceLoader.load with an array class, and how they
> behave. This goes to the question as to if returning an empty iterator (or
> stream) will mask a bug elsewhere. The advantage of throwing IAE is that it
> helps find the frameworks or library bug quickly.
Hi @AlanBateman ,
Thanks for the comment — I’ve updated the patch to throw
IllegalArgumentException instead of returning an empty loader.
I did a quick search and couldn’t find real frameworks passing an array class
to ServiceLoader; it seems like a fuzzing/differential-testing edge case. I
agree IAE is better here since an empty loader could hide a real caller bug.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/32515#issuecomment-5530253837