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 2023/05/22 19:19:39 UTC

[maven] branch master updated: [MNG-7780] DefaultArtifact.equals throws NullPointerException if o.version is null (#1108)

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 29c0a95b0 [MNG-7780] DefaultArtifact.equals throws NullPointerException if o.version is null (#1108)
29c0a95b0 is described below

commit 29c0a95b0bd105380bb749abfc1a3b188a347005
Author: huazhongming <cr...@gmail.com>
AuthorDate: Tue May 23 03:19:33 2023 +0800

    [MNG-7780] DefaultArtifact.equals throws NullPointerException if o.version is null (#1108)
    
    Signed-off-by: crazyhzm <cr...@apache.org>
---
 .../org/apache/maven/artifact/DefaultArtifact.java | 51 ++++++----------------
 .../apache/maven/artifact/DefaultArtifactTest.java | 12 +++++
 2 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java b/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java
index 379c60117..fe84f4b4b 100644
--- a/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java
+++ b/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java
@@ -19,11 +19,7 @@
 package org.apache.maven.artifact;
 
 import java.io.File;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import org.apache.maven.artifact.handler.ArtifactHandler;
 import org.apache.maven.artifact.metadata.ArtifactMetadata;
@@ -287,44 +283,25 @@ public class DefaultArtifact implements Artifact {
         return sb.toString();
     }
 
-    public int hashCode() {
-        int result = 17;
-        result = 37 * result + groupId.hashCode();
-        result = 37 * result + artifactId.hashCode();
-        result = 37 * result + type.hashCode();
-        if (version != null) {
-            result = 37 * result + version.hashCode();
-        }
-        result = 37 * result + (classifier != null ? classifier.hashCode() : 0);
-        return result;
-    }
-
+    @Override
     public boolean equals(Object o) {
-        if (o == this) {
+        if (this == o) {
             return true;
         }
-
-        if (!(o instanceof Artifact)) {
-            return false;
-        }
-
-        Artifact a = (Artifact) o;
-
-        if (!a.getGroupId().equals(groupId)) {
-            return false;
-        } else if (!a.getArtifactId().equals(artifactId)) {
-            return false;
-        } else if (!a.getVersion().equals(version)) {
+        if (o == null || getClass() != o.getClass()) {
             return false;
-        } else if (!a.getType().equals(type)) {
-            return false;
-        } else {
-            return a.getClassifier() == null
-                    ? classifier == null
-                    : a.getClassifier().equals(classifier);
         }
+        DefaultArtifact that = (DefaultArtifact) o;
+        return Objects.equals(groupId, that.groupId)
+                && Objects.equals(artifactId, that.artifactId)
+                && Objects.equals(type, that.type)
+                && Objects.equals(classifier, that.classifier)
+                && Objects.equals(version, that.version);
+    }
 
-        // We don't consider the version range in the comparison, just the resolved version
+    @Override
+    public int hashCode() {
+        return Objects.hash(groupId, artifactId, type, classifier, version);
     }
 
     public String getBaseVersion() {
diff --git a/maven-artifact/src/test/java/org/apache/maven/artifact/DefaultArtifactTest.java b/maven-artifact/src/test/java/org/apache/maven/artifact/DefaultArtifactTest.java
index 7235d1723..21dd53767 100644
--- a/maven-artifact/src/test/java/org/apache/maven/artifact/DefaultArtifactTest.java
+++ b/maven-artifact/src/test/java/org/apache/maven/artifact/DefaultArtifactTest.java
@@ -140,4 +140,16 @@ class DefaultArtifactTest {
         assertNull(artifact.getVersion());
         assertNull(artifact.getBaseVersion());
     }
+
+    @Test
+    void testMNG7780() throws Exception {
+        VersionRange vr = VersionRange.createFromVersionSpec("[1.0,2.0)");
+        artifact = new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
+        assertNull(artifact.getVersion());
+        assertNull(artifact.getBaseVersion());
+
+        DefaultArtifact nullVersionArtifact =
+                new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
+        assertEquals(artifact, nullVersionArtifact);
+    }
 }