gnodet-bot commented on code in PR #13539:
URL: https://github.com/apache/maven/pull/13539#discussion_r4293029639


##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/EventSpyImpl.java:
##########
@@ -52,22 +61,46 @@ public void onEvent(Object arg) throws Exception {
             EventType eventType = convert(ee.getType());
             Collection<Listener> listeners = session.getListeners();
             if (!listeners.isEmpty()) {
-                org.apache.maven.api.ExecutionEvent event = new 
DefaultEvent(session, ee, eventType);
-                for (Listener listener : listeners) {
-                    try {
-                        listener.onEvent(event);
-                    } catch (RuntimeException e) {
-                        LOGGER.warn(
-                                "Failed to notify listener {} about {}",
-                                listener.getClass().getName(),
-                                eventType,
-                                e);
-                    }
+                notify(listeners, new DefaultEvent(session, ee, eventType), 
eventType);
+            }
+        } else if (arg instanceof DependencyResolutionRequest request) {
+            pendingResolution.set(request);
+            InternalMavenSession session = 
InternalMavenSession.from(request.getRepositorySession());
+            Collection<Listener> listeners = session.getListeners();
+            if (!listeners.isEmpty()) {
+                DependencyResolutionEvent event =
+                        new DefaultDependencyResolutionEvent(session, 
session.getProject(request.getMavenProject()));

Review Comment:
   ⚠️ **`@Nonnull` contract violation — STARTED path:** 
`session.getProject(request.getMavenProject())` is declared `@Nullable` (see 
`InternalMavenSession.getProject`) and returns `null` when the project has no 
basedir. Passing it directly to the `DefaultDependencyResolutionEvent` 
constructor violates `DependencyResolutionEvent.project()`'s `@Nonnull` 
contract and will cause a `NullPointerException` at any listener call site that 
calls `event.project()`.
   
   Consider skipping the event dispatch when the project is null, or explicitly 
handling the null case:
   ```suggestion
                   Project project = 
session.getProject(request.getMavenProject());
                   if (project == null) {
                       return;
                   }
                   DependencyResolutionEvent event =
                           new DefaultDependencyResolutionEvent(session, 
project);
   ```



##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependencyResolutionEvent.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.internal.impl;
+
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.Dependency;
+import org.apache.maven.api.DependencyResolutionEvent;
+import org.apache.maven.api.DependencyResolutionEventType;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Session;
+import org.apache.maven.project.DependencyResolutionResult;
+
+/**
+ * Maven 4 view of a dependency resolution, created from the legacy request 
and result.
+ */
+class DefaultDependencyResolutionEvent implements DependencyResolutionEvent {
+    private final InternalMavenSession session;
+    private final Project project;
+    private final DependencyResolutionEventType type;
+    private final List<Dependency> resolved;
+    private final List<Dependency> unresolved;
+    private final Exception exception;
+
+    /** Creates the event fired before the dependencies of the project are 
resolved. */
+    DefaultDependencyResolutionEvent(InternalMavenSession session, Project 
project) {

Review Comment:
   💡 **Defense-in-depth:** Add `Objects.requireNonNull(project, "project")` in 
both constructors. Since `project()` is declared `@Nonnull`, the constructor 
should fail fast with a clear message rather than storing a silent `null` that 
will cause a cryptic NPE later at the listener call site.
   
   ```suggestion
       DefaultDependencyResolutionEvent(InternalMavenSession session, Project 
project) {
           this.session = Objects.requireNonNull(session, "session");
           this.project = Objects.requireNonNull(project, "project");
   ```



##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependencyResolutionEvent.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.internal.impl;
+
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.Dependency;
+import org.apache.maven.api.DependencyResolutionEvent;
+import org.apache.maven.api.DependencyResolutionEventType;
+import org.apache.maven.api.Project;
+import org.apache.maven.api.Session;
+import org.apache.maven.project.DependencyResolutionResult;
+
+/**
+ * Maven 4 view of a dependency resolution, created from the legacy request 
and result.
+ */
+class DefaultDependencyResolutionEvent implements DependencyResolutionEvent {
+    private final InternalMavenSession session;
+    private final Project project;
+    private final DependencyResolutionEventType type;
+    private final List<Dependency> resolved;
+    private final List<Dependency> unresolved;
+    private final Exception exception;
+
+    /** Creates the event fired before the dependencies of the project are 
resolved. */
+    DefaultDependencyResolutionEvent(InternalMavenSession session, Project 
project) {
+        this.session = session;
+        this.project = project;
+        this.type = DependencyResolutionEventType.STARTED;
+        this.resolved = List.of();
+        this.unresolved = List.of();
+        this.exception = null;
+    }
+
+    /** Creates the event fired once the dependencies of the project have been 
resolved or have failed to. */
+    DefaultDependencyResolutionEvent(InternalMavenSession session, Project 
project, DependencyResolutionResult result) {

Review Comment:
   💡 **Defense-in-depth (3-arg constructor):** Same `Objects.requireNonNull` 
guard needed here.
   
   ```suggestion
       DefaultDependencyResolutionEvent(InternalMavenSession session, Project 
project, DependencyResolutionResult result) {
           this.session = Objects.requireNonNull(session, "session");
           this.project = Objects.requireNonNull(project, "project");
   ```



##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/EventSpyImpl.java:
##########
@@ -52,22 +61,46 @@ public void onEvent(Object arg) throws Exception {
             EventType eventType = convert(ee.getType());
             Collection<Listener> listeners = session.getListeners();
             if (!listeners.isEmpty()) {
-                org.apache.maven.api.ExecutionEvent event = new 
DefaultEvent(session, ee, eventType);
-                for (Listener listener : listeners) {
-                    try {
-                        listener.onEvent(event);
-                    } catch (RuntimeException e) {
-                        LOGGER.warn(
-                                "Failed to notify listener {} about {}",
-                                listener.getClass().getName(),
-                                eventType,
-                                e);
-                    }
+                notify(listeners, new DefaultEvent(session, ee, eventType), 
eventType);
+            }
+        } else if (arg instanceof DependencyResolutionRequest request) {
+            pendingResolution.set(request);
+            InternalMavenSession session = 
InternalMavenSession.from(request.getRepositorySession());
+            Collection<Listener> listeners = session.getListeners();
+            if (!listeners.isEmpty()) {
+                DependencyResolutionEvent event =
+                        new DefaultDependencyResolutionEvent(session, 
session.getProject(request.getMavenProject()));
+                notify(listeners, event, event.type());
+            }
+        } else if (arg instanceof DependencyResolutionResult result) {
+            DependencyResolutionRequest request = pendingResolution.get();
+            pendingResolution.remove();
+            if (request != null) {
+                InternalMavenSession session = 
InternalMavenSession.from(request.getRepositorySession());
+                Collection<Listener> listeners = session.getListeners();
+                if (!listeners.isEmpty()) {
+                    DependencyResolutionEvent event = new 
DefaultDependencyResolutionEvent(
+                            session, 
session.getProject(request.getMavenProject()), result);

Review Comment:
   ⚠️ **`@Nonnull` contract violation — RESULT path:** Same issue. 
`session.getProject(request.getMavenProject())` can return `null`, violating 
`DependencyResolutionEvent.project()`'s `@Nonnull` contract.
   
   ```suggestion
                       Project project = 
session.getProject(request.getMavenProject());
                       if (project == null) {
                           return;
                       }
                       DependencyResolutionEvent event = new 
DefaultDependencyResolutionEvent(
                               session, project, result);
   ```



-- 
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]

Reply via email to