You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/09/01 09:16:22 UTC

(maven) branch fix-apache-maven-12986-maven-4-mavensession-look created (now d4bdd42014)

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

gnodet pushed a change to branch fix-apache-maven-12986-maven-4-mavensession-look
in repository https://gitbox.apache.org/repos/asf/maven.git


      at d4bdd42014 Fix #12986: Throw descriptive UnsupportedOperationException instead of NPE in MavenSession.lookup()

This branch includes the following new commits:

     new d4bdd42014 Fix #12986: Throw descriptive UnsupportedOperationException instead of NPE in MavenSession.lookup()

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(maven) 01/01: Fix #12986: Throw descriptive UnsupportedOperationException instead of NPE in MavenSession.lookup()

Posted by gn...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch fix-apache-maven-12986-maven-4-mavensession-look
in repository https://gitbox.apache.org/repos/asf/maven.git

commit d4bdd42014164861c51adf27ccb416e02b617bf4
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Tue Sep 1 11:16:08 2026 +0200

    Fix #12986: Throw descriptive UnsupportedOperationException instead of NPE in MavenSession.lookup()
    
    Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
---
 .../org/apache/maven/execution/MavenSession.java   |  12 ++
 .../apache/maven/execution/MavenSessionTest.java   | 127 +++++++++++++++++++++
 2 files changed, 139 insertions(+)

diff --git a/impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java b/impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
index 400e1653cc..3d2539f07c 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
@@ -472,23 +472,35 @@ public PlexusContainer getContainer() {
         return container;
     }
 
+    private void checkContainer() {
+        if (container == null) {
+            throw new UnsupportedOperationException("PlexusContainer is not available in this session. "
+                    + "Plugins should use JSR 330 (@Inject) injection instead of "
+                    + "MavenSession.lookup(). The MavenSession Plexus lookup methods are deprecated.");
+        }
+    }
+
     @Deprecated
     public Object lookup(String role) throws ComponentLookupException {
+        checkContainer();
         return container.lookup(role);
     }
 
     @Deprecated
     public Object lookup(String role, String roleHint) throws ComponentLookupException {
+        checkContainer();
         return container.lookup(role, roleHint);
     }
 
     @Deprecated
     public List<Object> lookupList(String role) throws ComponentLookupException {
+        checkContainer();
         return container.lookupList(role);
     }
 
     @Deprecated
     public Map<String, Object> lookupMap(String role) throws ComponentLookupException {
+        checkContainer();
         return container.lookupMap(role);
     }
 
diff --git a/impl/maven-core/src/test/java/org/apache/maven/execution/MavenSessionTest.java b/impl/maven-core/src/test/java/org/apache/maven/execution/MavenSessionTest.java
new file mode 100644
index 0000000000..ed2ab67235
--- /dev/null
+++ b/impl/maven-core/src/test/java/org/apache/maven/execution/MavenSessionTest.java
@@ -0,0 +1,127 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.execution;
+
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+import org.eclipse.aether.RepositorySystemSession;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Tests for {@link MavenSession}.
+ */
+class MavenSessionTest {
+
+    /**
+     * Creates a MavenSession without a PlexusContainer (Maven 4 style).
+     */
+    private MavenSession createSessionWithoutContainer() {
+        RepositorySystemSession repoSession = mock(RepositorySystemSession.class);
+        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
+        MavenExecutionResult result = new DefaultMavenExecutionResult();
+        return new MavenSession(repoSession, request, result);
+    }
+
+    /**
+     * Creates a MavenSession with a PlexusContainer (Maven 3 style).
+     */
+    @SuppressWarnings("deprecation")
+    private MavenSession createSessionWithContainer(PlexusContainer container) {
+        RepositorySystemSession repoSession = mock(RepositorySystemSession.class);
+        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
+        MavenExecutionResult result = new DefaultMavenExecutionResult();
+        return new MavenSession(container, repoSession, request, result);
+    }
+
+    @Test
+    @SuppressWarnings("deprecation")
+    void testLookupWithoutContainerThrowsUnsupportedOperationException() {
+        MavenSession session = createSessionWithoutContainer();
+
+        UnsupportedOperationException exception =
+                assertThrows(UnsupportedOperationException.class, () -> session.lookup("role"));
+        assertEquals(
+                "PlexusContainer is not available in this session. "
+                        + "Plugins should use JSR 330 (@Inject) injection instead of "
+                        + "MavenSession.lookup(). The MavenSession Plexus lookup methods are deprecated.",
+                exception.getMessage());
+    }
+
+    @Test
+    @SuppressWarnings("deprecation")
+    void testLookupWithRoleHintWithoutContainerThrowsUnsupportedOperationException() {
+        MavenSession session = createSessionWithoutContainer();
+
+        assertThrows(UnsupportedOperationException.class, () -> session.lookup("role", "hint"));
+    }
+
+    @Test
+    @SuppressWarnings("deprecation")
+    void testLookupListWithoutContainerThrowsUnsupportedOperationException() {
+        MavenSession session = createSessionWithoutContainer();
+
+        assertThrows(UnsupportedOperationException.class, () -> session.lookupList("role"));
+    }
+
+    @Test
+    @SuppressWarnings("deprecation")
+    void testLookupMapWithoutContainerThrowsUnsupportedOperationException() {
+        MavenSession session = createSessionWithoutContainer();
+
+        assertThrows(UnsupportedOperationException.class, () -> session.lookupMap("role"));
+    }
+
+    @Test
+    @SuppressWarnings("deprecation")
+    void testGetContainerWithoutContainerReturnsNull() {
+        MavenSession session = createSessionWithoutContainer();
+
+        assertNull(session.getContainer());
+    }
+
+    @Test
+    @SuppressWarnings("deprecation")
+    void testLookupWithContainerDelegates() throws ComponentLookupException {
+        PlexusContainer container = mock(PlexusContainer.class);
+        Object expected = new Object();
+        when(container.lookup("role")).thenReturn(expected);
+
+        MavenSession session = createSessionWithContainer(container);
+
+        assertEquals(expected, session.lookup("role"));
+    }
+
+    @Test
+    @SuppressWarnings("deprecation")
+    void testLookupWithRoleHintWithContainerDelegates() throws ComponentLookupException {
+        PlexusContainer container = mock(PlexusContainer.class);
+        Object expected = new Object();
+        when(container.lookup("role", "hint")).thenReturn(expected);
+
+        MavenSession session = createSessionWithContainer(container);
+
+        assertEquals(expected, session.lookup("role", "hint"));
+    }
+}