gnodet opened a new pull request, #13877:
URL: https://github.com/apache/maven/pull/13877
## Summary
Fix `InjectorImpl` to return an empty collection when `getInstance()` is
called
with `Key<List<T>>` or `Key<Map<String,T>>` and no bindings are registered
for
the element type `T`.
## Root Cause
`InjectorImpl.doGetCompiledBinding()` handles `List<T>` and `Map<String,T>`
keys
by calling `getBindings(elementType)`. When this returns `null` (no bindings
registered at all for that type), the code fell through to the generic
`throw new DIException("No binding to construct an instance for key ...")`.
This violated the documented contract of `Lookup.lookupList()` and
`Lookup.lookupMap()`, which both state _"The collection may be empty if no
components are found"_ — an absent type should yield an empty collection, not
an exception.
The `ApiRunner` implementation worked around this by catching `DIException`
and
returning `List.of()`. However, that catch was too broad (it would also
suppress
genuine provisioning errors) and masked the real bug in `InjectorImpl`. It
also
used a raw anonymous `Key<List<T>>(){}` subclass whose type parameter is
erased
at runtime, so lookups never found the correct bindings anyway.
## Fix
**`InjectorImpl`**: when `getBindings(elementType)` returns `null` in the
`List<T>` path, return an empty list supplier instead of falling through to
the
exception. Same for `Map<String,T>`.
**`ApiRunner`**: replace the anonymous `new Key<List<T>>() {}` (type-erased)
with `Key.ofType(Types.parameterizedType(List.class, type))` (correctly
parameterized). Remove the `try/catch (DIException)` workaround — no longer
needed.
## Impact
- `Lookup.lookupList()` and `Lookup.lookupMap()` now consistently return
empty
collections when no bindings are registered, on all `Lookup`
implementations.
- `ExtensibleEnumRegistries` and `DefaultTypeRegistry`, which both call
`lookupList()`, are now robust when no `TypeProvider` is registered.
- No behavioral regression: code that relied on the `DIException` to detect
"nothing registered" was already broken (it was catching a contract
violation).
_Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]