gnodet commented on code in PR #12146:
URL: https://github.com/apache/maven/pull/12146#discussion_r3293766526


##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLifecycleRegistry.java:
##########
@@ -122,6 +122,11 @@ public DefaultLifecycleRegistry(List<LifecycleProvider> 
providers) {
         }
     }
 
+    static String getImplementationVersion(Class<?> clazz) {
+        Package packageInfo = clazz.getPackage();
+        return packageInfo != null ? packageInfo.getImplementationVersion() : 
null;
+    }

Review Comment:
   The identical `getImplementationVersion` helper is copy-pasted verbatim into 
four separate classes (`DefaultReportingConverter`, both 
`DefaultSuperPomProvider` variants, and here). Consider extracting it into a 
shared utility to avoid the duplication. If the compat module intentionally 
avoids depending on impl internals, the duplication there may be acceptable, 
but the two `impl/` modules could still share one copy.



##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLifecycleRegistry.java:
##########
@@ -88,7 +88,7 @@ public class DefaultLifecycleRegistry implements 
LifecycleRegistry {
     private static final String MAVEN_PLUGINS = "org.apache.maven.plugins:";
 
     public static final String DEFAULT_LIFECYCLE_MODELID = 
"org.apache.maven:maven-core:"
-            + 
DefaultLifecycleRegistry.class.getPackage().getImplementationVersion()
+            + getImplementationVersion(DefaultLifecycleRegistry.class)

Review Comment:
   `DEFAULT_LIFECYCLE_MODELID` is a `static final` constant whose value feeds 
directly into `DEFAULT_LIFECYCLE_INPUT_LOCATION` on the next line. Before this 
PR, when `getPackage()` returned `null`, the class failed at load time with an 
`ExceptionInInitializerError` (wrapping an NPE).
   
   With this PR, the class loads successfully, but the model id will contain 
the literal string `"null"` via string concatenation (e.g. 
`"org.apache.maven:maven-core:null:default-lifecycle-bindings"`), which may 
silently produce confusing diagnostics later. The other three call sites have 
the same characteristic.
   
   It might be worth returning a fallback like `"unknown"` instead of `null`, 
so the concatenated model id remains parseable/debuggable:
   
   ```suggestion
               + getImplementationVersion(DefaultLifecycleRegistry.class)
   ```
   
   (the suggestion is a no-op to keep this visible -- the actual change would 
be in `getImplementationVersion` to return `"unknown"` instead of `null`)



##########
compat/maven-model-builder/src/test/java/org/apache/maven/model/plugin/DefaultReportingConverterTest.java:
##########
@@ -0,0 +1,31 @@
+/*
+ * 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.model.plugin;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class DefaultReportingConverterTest {
+
+    @Test
+    void returnsNullImplementationVersionWhenClassHasNoPackage() {
+        
assertNull(DefaultReportingConverter.getImplementationVersion(int.class));

Review Comment:
   Using `int.class` (a primitive type whose `getPackage()` returns `null`) is 
a clever way to trigger the null-package path. However, it would strengthen the 
test to also verify the case where `getPackage()` is non-null but 
`getImplementationVersion()` itself returns `null` -- which is the far more 
common scenario in IDE and test-classpath environments (classes loaded without 
a JAR manifest). A regular class from the test classpath would exercise that 
path.
   
   Also, all four test classes are identical except for the class under test -- 
a parameterized test or a shared test helper could reduce the boilerplate.



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