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


##########
impl/maven-logging/src/main/java/org/apache/maven/logging/impl/DefaultLogEvent.java:
##########
@@ -0,0 +1,273 @@
+/*
+ * 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.logging.impl;
+
+import java.time.Instant;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.maven.api.logging.LogEvent;
+import org.apache.maven.api.logging.LogLevel;
+
+/**
+ * Immutable implementation of {@link LogEvent} with lazy message formatting.
+ * <p>
+ * When constructed with a {@code messageTemplate} and {@code messageArgs},
+ * the formatted {@link #message()} is computed lazily on first access.
+ * When constructed with a pre-formatted message, it is returned as-is.
+ *
+ * @since 4.2.0
+ */
+public final class DefaultLogEvent implements LogEvent {
+
+    private static final String PLACEHOLDER = "{}";
+
+    private final Instant timestamp;
+    private final LogLevel level;
+    private final String message;
+    private final String loggerName;
+    private final Throwable throwable;
+    private final String sourceClassName;
+    private final String sourceMethodName;
+    private final long threadId;
+    private final long sequenceNumber;
+    private final String threadName;
+    private final String projectId;
+    private final String mojoId;
+    private final Map<String, Object> keyValues;
+    private final String messageTemplate;
+    private final List<Object> messageArgs;
+
+    private volatile String formattedMessage;
+
+    @SuppressWarnings("checkstyle:ParameterNumber")
+    public DefaultLogEvent(
+            Instant timestamp,
+            LogLevel level,
+            String message,
+            String loggerName,
+            Throwable throwable,
+            String sourceClassName,
+            String sourceMethodName,
+            long threadId,
+            long sequenceNumber,
+            String threadName,
+            String projectId,
+            String mojoId,
+            Map<String, Object> keyValues,
+            String messageTemplate,
+            Object[] messageArgs) {
+        this(
+                timestamp,
+                level,
+                message,
+                loggerName,
+                throwable,
+                sourceClassName,
+                sourceMethodName,
+                threadId,
+                sequenceNumber,
+                threadName,
+                projectId,
+                mojoId,
+                keyValues,
+                messageTemplate,
+                messageArgs != null ? List.copyOf(Arrays.asList(messageArgs)) 
: null);

Review Comment:
   ⚠️ **`List.copyOf()` throws `NullPointerException` when any element is 
`null`**
   
   `List.copyOf(Arrays.asList(arr))` is 
[documented](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html#copyOf(java.util.Collection))
 to reject null elements. Callers such as `DefaultLogEventBuilder.log(String, 
Object)` pass `new Object[]{arg}` where `arg` can legitimately be `null`:
   
   ```java
   logger.info("value is {}", null);
   // → List.copyOf(Arrays.asList(new Object[]{null})) → NPE
   ```
   
   This turns a routine log call into an unexpected exception — silent loss of 
the log event at the call site.
   
   `Collections.unmodifiableList(Arrays.asList(messageArgs))` preserves null 
elements and is structurally immutable — same safety guarantees, no NPE. (Needs 
`import java.util.Collections;`.)
   
   ```suggestion
                   messageArgs != null ? 
Collections.unmodifiableList(Arrays.asList(messageArgs)) : null);
   ```



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