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 cc6ce0bfe8 fix(di): bind session services as priority-based fallbacks 
to avoid ambiguous bindings with plugin @Provides
cc6ce0bfe8 is described below

commit cc6ce0bfe8f06446ec11c353f5328c567e94c583
Author: Guillaume Nodet <[email protected]>
AuthorDate: Thu Nov 12 20:13:15 2026 +0100

    fix(di): bind session services as priority-based fallbacks to avoid 
ambiguous bindings with plugin @Provides
    
    - Add `bindSupplier(Class, Supplier, int priority)` overload to `Injector` 
and override it in `InjectorImpl`, delegating the existing zero-arg overload to 
it; add `SESSION_SERVICE_PRIORITY = -10` constant to `Injector` (@since 4.2.0)
    - Use the priority overload in `DefaultMavenPluginManager.loadV4Mojo` so 
session services are registered at priority -10, making them explicit fallbacks 
that yield to any plugin `@Provides` binding at the default priority 0
    - Fixes `DIException: Ambiguous bindings` when a plugin (e.g. 
maven-compiler-plugin 4.x) declares its own `@Provides` for session services 
that Maven core also injects since #11103
    - Add two `BindingSelectionTest` tests: supplier at 
SESSION_SERVICE_PRIORITY is overridden by a `@Provides` method; supplier at 
positive priority wins over one at priority 0
---
 .../plugin/internal/DefaultMavenPluginManager.java |  3 +-
 .../main/java/org/apache/maven/di/Injector.java    | 36 ++++++++++++++++++++++
 .../org/apache/maven/di/impl/InjectorImpl.java     |  7 ++++-
 .../apache/maven/di/impl/BindingSelectionTest.java | 24 +++++++++++++++
 4 files changed, 68 insertions(+), 2 deletions(-)

diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
 
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
index 110ceac14e..254aecb101 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
@@ -592,7 +592,8 @@ protected BindingStyler getBindingStyler() {
             injector.bindInstance(Log.class, log);
 
             Map<Class<? extends Service>, Supplier<? extends Service>> 
services = sessionV4.getAllServices();
-            services.forEach((itf, svc) -> 
injector.bindSupplier((Class<Service>) itf, (Supplier<Service>) svc));
+            services.forEach((itf, svc) -> injector.bindSupplier(
+                    (Class<Service>) itf, (Supplier<Service>) svc, 
Injector.SESSION_SERVICE_PRIORITY));
 
             mojo = mojoInterface.cast(injector.getInstance(
                     
Key.of(pluginRealm.loadClass(mojoDescriptor.getImplementation()), 
mojoDescriptor.getRoleHint())));
diff --git a/impl/maven-di/src/main/java/org/apache/maven/di/Injector.java 
b/impl/maven-di/src/main/java/org/apache/maven/di/Injector.java
index b53749c595..6b46bd6eab 100644
--- a/impl/maven-di/src/main/java/org/apache/maven/di/Injector.java
+++ b/impl/maven-di/src/main/java/org/apache/maven/di/Injector.java
@@ -53,6 +53,16 @@
  */
 public interface Injector {
 
+    /**
+     * Priority assigned to session services injected by Maven core into V4 
mojos.
+     * <p>
+     * A negative value ensures that these fallback bindings are overridden by 
any
+     * plugin-provided {@code @Provides} method (which defaults to priority 
{@code 0}).
+     *
+     * @since 4.2.0
+     */
+    int SESSION_SERVICE_PRIORITY = -10;
+
     /**
      * Creates a new Injector instance with default settings.
      *
@@ -147,6 +157,32 @@ static Injector create() {
     @Nonnull
     <T> Injector bindSupplier(@Nonnull Class<T> cls, @Nonnull Supplier<T> 
supplier);
 
+    /**
+     * Binds a supplier to a class with an explicit priority.
+     * <p>
+     * A lower priority value means the binding will be overridden by any 
binding with a higher priority.
+     * Use a negative priority (e.g. {@code -10}) to register a fallback 
binding that yields to
+     * plugin-provided {@code @Provides} methods (which default to priority 
{@code 0}).
+     *
+     * <p>
+     * <b>Implementation note:</b> The default implementation ignores {@code 
priority} for
+     * backward compatibility with existing {@code Injector} implementations.
+     * Implementors SHOULD override this method to honor the priority 
parameter.
+     * </p>
+     *
+     * @param <T> the type of the instance
+     * @param cls the class to bind to
+     * @param supplier the supplier to use for injection
+     * @param priority the binding priority
+     * @return this injector instance for method chaining
+     * @throws NullPointerException if {@code cls} or {@code supplier} is null
+     * @since 4.2.0
+     */
+    @Nonnull
+    default <T> Injector bindSupplier(@Nonnull Class<T> cls, @Nonnull 
Supplier<T> supplier, int priority) {
+        return bindSupplier(cls, supplier);
+    }
+
     /**
      * Performs field and method injection on an existing instance.
      * <p>
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 accb142cc6..7e874e4420 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
@@ -162,8 +162,13 @@ public <U> Injector bindInstance(@Nonnull Class<U> clazz, 
@Nonnull U instance) {
 
     @Override
     public <U> Injector bindSupplier(@Nonnull Class<U> clazz, @Nonnull 
Supplier<U> supplier) {
+        return bindSupplier(clazz, supplier, 0);
+    }
+
+    @Override
+    public <U> Injector bindSupplier(@Nonnull Class<U> clazz, @Nonnull 
Supplier<U> supplier, int priority) {
         Key<?> key = Key.of(clazz, ReflectionUtils.qualifierOf(clazz));
-        Binding<U> binding = Binding.toSupplier(supplier).withKey(key);
+        Binding<U> binding = 
Binding.toSupplier(supplier).withKey(key).prioritize(priority);
         return doBind(key, binding);
     }
 
diff --git 
a/impl/maven-di/src/test/java/org/apache/maven/di/impl/BindingSelectionTest.java
 
b/impl/maven-di/src/test/java/org/apache/maven/di/impl/BindingSelectionTest.java
index 4a96f22024..e9d6570c07 100644
--- 
a/impl/maven-di/src/test/java/org/apache/maven/di/impl/BindingSelectionTest.java
+++ 
b/impl/maven-di/src/test/java/org/apache/maven/di/impl/BindingSelectionTest.java
@@ -120,6 +120,30 @@ void 
rebindingTheSameSupplierDoesNotDuplicateItsProviders() {
         assertEquals(List.of("instance"), injector.getInstance(new 
Key<List<String>>() {}));
     }
 
+    @Test
+    void supplierWithLowerPriorityIsOverriddenByProvidesMethod() {
+        // A binding registered with SESSION_SERVICE_PRIORITY (negative) must 
lose
+        // to a @Provides method at the default priority (0).
+        // Use NamedService::new for the low-priority supplier so the 
assertion is not tautological:
+        // if priority selection fails and the supplier wins, the result is 
NamedService, not DefaultService.
+        Injector injector = Injector.create()
+                .bindSupplier(Service.class, NamedService::new, 
Injector.SESSION_SERVICE_PRIORITY)
+                .bindImplicit(DefaultAndNamed.class);
+
+        // The @Provides method in DefaultAndNamed wins over the low-priority 
supplier — no ambiguity.
+        assertInstanceOf(DefaultService.class, 
injector.getInstance(Service.class));
+    }
+
+    @Test
+    void supplierWithHigherPriorityWinsOverDefaultBindSupplier() {
+        // A binding at positive priority wins over one at priority 0.
+        Injector injector = Injector.create()
+                .bindSupplier(Service.class, DefaultService::new, 0)
+                .bindSupplier(Service.class, NamedService::new, 
PREFERRED_PRIORITY);
+
+        assertInstanceOf(NamedService.class, 
injector.getInstance(Service.class));
+    }
+
     @Test
     void differentProgrammaticFactoriesRemainAmbiguous() {
         InjectorImpl injector = new InjectorImpl();

Reply via email to