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

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


The following commit(s) were added to refs/heads/maven-4.0.x by this push:
     new 2fcd9f2f44 [backport maven-4.0.x] Fix InjectorImpl to return empty 
collections for unbound List/Map types (#13879)
2fcd9f2f44 is described below

commit 2fcd9f2f44a230b9cb3b85652dfdce882397c653
Author: Guillaume Nodet <[email protected]>
AuthorDate: Sat Nov 7 14:14:00 2026 +0100

    [backport maven-4.0.x] Fix InjectorImpl to return empty collections for 
unbound List/Map types (#13879)
---
 .../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 fd4a01866b..1b0f39b135 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 d11da4172d..42b2b8a72a 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
@@ -76,6 +76,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.di.SessionScope;
@@ -439,12 +440,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