This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new cd2072be9f Fix InjectorImpl to return empty collections for unbound 
List/Map types (#13878)
cd2072be9f is described below

commit cd2072be9f4040cf599cb9d79a7bd43035a99f8d
Author: Guillaume Nodet <[email protected]>
AuthorDate: Sat Nov 7 11:19:41 2026 +0100

    Fix InjectorImpl to return empty collections for unbound List/Map types 
(#13878)
    
    * Fix InjectorImpl to return empty list/map when no bindings exist for the 
type
    
    When getInstance() is called with a Key<List<T>> or Key<Map<String,T>> and 
no
    bindings are registered for the element type T, InjectorImpl was falling 
through
    to the generic 'No binding to construct an instance' DIException. This 
violated
    the contract of Lookup.lookupList() and Lookup.lookupMap(), which both 
document
    that they 'may return an empty collection if no components are found'.
    
    The correct semantics for collection lookups is: an unknown type means zero
    instances, not an error. Only a specific binding that fails to instantiate, 
or
    an ambiguous/cyclic binding, should propagate as an exception.
    
    Fix: return an empty List.of() / Map.of() supplier when getBindings() 
returns
    null for the requested element type in the List and Map branches.
    
    Also fix ApiRunner.lookupList/lookupMap to pass a properly parameterized Key
    (using Key.ofType + Types.parameterizedType) instead of a raw anonymous 
subclass
    whose type parameter is erased at runtime. With the InjectorImpl fix in 
place,
    no try/catch is needed: an unregistered type now returns an empty collection
    cleanly.
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
    
    * Add Javadoc to InjectorImpl.doGetCompiledBinding documenting collection 
semantics
    
    Document the different resolution rules for scalar, List<T> and 
Map<String,T>
    keys, in particular that an absent type yields an empty collection rather 
than
    a DIException.
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Sonnet 4.6 <[email protected]>
---
 .../org/apache/maven/di/impl/InjectorImpl.java     | 30 ++++++++++++++++++++++
 .../apache/maven/impl/standalone/ApiRunner.java    |  5 ++--
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git 
a/impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java 
b/impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java
index c53183642e..3d0cab7326 100644
--- a/impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java
+++ b/impl/maven-di/src/main/java/org/apache/maven/di/impl/InjectorImpl.java
@@ -252,6 +252,26 @@ public <Q> Supplier<Q> getCompiledBinding(Dependency<Q> 
dep) {
         };
     }
 
+    /**
+     * Resolves a compiled binding supplier for the given dependency.
+     *
+     * <p>The resolution rules differ by key type:
+     * <ul>
+     *   <li><b>Scalar types</b>: a single binding must exist; ambiguous 
bindings throw
+     *       {@link DIException}. If no binding exists and the dependency is 
optional,
+     *       returns a supplier of {@code null}; otherwise throws {@link 
DIException}.</li>
+     *   <li><b>{@code List<T>}</b>: returns all bindings for {@code T}, 
sorted by priority.
+     *       If no bindings are registered for {@code T}, returns an empty 
list —
+     *       this is not an error condition.</li>
+     *   <li><b>{@code Map<String, T>}</b>: returns all named bindings for 
{@code T} keyed
+     *       by their qualifier. If no bindings are registered for {@code T}, 
returns an
+     *       empty map — this is not an error condition.</li>
+     * </ul>
+     *
+     * @param dep the dependency to resolve
+     * @return a supplier that produces the bound instance(s)
+     * @throws DIException if a required scalar binding is absent or ambiguous
+     */
     public <Q> Supplier<Q> doGetCompiledBinding(Dependency<Q> dep) {
         Key<Q> key = dep.key();
         Set<Binding<Q>> res = getBindings(key);
@@ -285,6 +305,11 @@ public <Q> Supplier<Q> doGetCompiledBinding(Dependency<Q> 
dep) {
                 //noinspection unchecked
                 return () -> (Q) list(list, Supplier::get);
             }
+            // No bindings registered for this type — an empty list is the 
correct result,
+            // not an error. Callers such as Lookup.lookupList() document "may 
be empty if
+            // no components found" and must not receive a DIException for 
absent bindings.
+            //noinspection unchecked
+            return () -> (Q) List.of();
         }
         if (key.getRawType() == Map.class) {
             Key<?> k = key.getTypeParameter(0);
@@ -303,6 +328,11 @@ public <Q> Supplier<Q> doGetCompiledBinding(Dependency<Q> 
dep) {
                 //noinspection unchecked
                 return () -> (Q) map(map, Supplier::get);
             }
+            // No bindings registered for this type — an empty map is the 
correct result.
+            if (k.getRawType() == String.class) {
+                //noinspection unchecked
+                return () -> (Q) Map.of();
+            }
         }
         if (dep.optional()) {
             return () -> null;
diff --git 
a/impl/maven-impl/src/main/java/org/apache/maven/impl/standalone/ApiRunner.java 
b/impl/maven-impl/src/main/java/org/apache/maven/impl/standalone/ApiRunner.java
index e48acca057..c999a7c08a 100644
--- 
a/impl/maven-impl/src/main/java/org/apache/maven/impl/standalone/ApiRunner.java
+++ 
b/impl/maven-impl/src/main/java/org/apache/maven/impl/standalone/ApiRunner.java
@@ -77,6 +77,7 @@
 import org.apache.maven.di.Key;
 import org.apache.maven.di.impl.DIException;
 import org.apache.maven.di.impl.InjectorImpl;
+import org.apache.maven.di.impl.Types;
 import org.apache.maven.impl.AbstractSession;
 import org.apache.maven.impl.InternalSession;
 import org.apache.maven.impl.MavenRepositoryListener;
@@ -455,12 +456,12 @@ public <T> Optional<T> lookupOptional(Class<T> type, 
String name) {
 
             @Override
             public <T> List<T> lookupList(Class<T> type) {
-                return injector.getInstance(new Key<List<T>>() {});
+                return 
injector.getInstance(Key.ofType(Types.parameterizedType(List.class, type)));
             }
 
             @Override
             public <T> Map<String, T> lookupMap(Class<T> type) {
-                return injector.getInstance(new Key<Map<String, T>>() {});
+                return 
injector.getInstance(Key.ofType(Types.parameterizedType(Map.class, 
String.class, type)));
             }
         };
     }

Reply via email to