Skip to content

Commit

Permalink
[MNG-7780] DefaultArtifact.equals throws NullPointerException if o.ve…
Browse files Browse the repository at this point in the history
…rsion is null (#1108)

Signed-off-by: crazyhzm <[email protected]>
  • Loading branch information
CrazyHZM authored May 22, 2023
1 parent 3f0f165 commit 29c0a95
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -287,44 +283,25 @@ public String toString() {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ void testNonResolvedVersionRangeConsistentlyYieldsNullVersions() throws Exceptio
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);
}
}

0 comments on commit 29c0a95

Please sign in to comment.