[MNG-7780] DefaultArtifact.equals throws NullPointerException if o.version is null (#1108)

Signed-off-by: crazyhzm <crazyhzm@apache.org>
This commit is contained in:
huazhongming 2023-05-23 03:19:33 +08:00 committed by GitHub
parent 3f0f165242
commit 29c0a95b0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 37 deletions

View File

@ -19,11 +19,7 @@
package org.apache.maven.artifact; package org.apache.maven.artifact;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.artifact.metadata.ArtifactMetadata;
@ -287,44 +283,25 @@ public class DefaultArtifact implements Artifact {
return sb.toString(); return sb.toString();
} }
public int hashCode() { @Override
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;
}
public boolean equals(Object o) { public boolean equals(Object o) {
if (o == this) { if (this == o) {
return true; return true;
} }
if (o == null || getClass() != o.getClass()) {
if (!(o instanceof Artifact)) {
return false; return false;
} }
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);
}
Artifact a = (Artifact) o; @Override
public int hashCode() {
if (!a.getGroupId().equals(groupId)) { return Objects.hash(groupId, artifactId, type, classifier, version);
return false;
} else if (!a.getArtifactId().equals(artifactId)) {
return false;
} else if (!a.getVersion().equals(version)) {
return false;
} else if (!a.getType().equals(type)) {
return false;
} else {
return a.getClassifier() == null
? classifier == null
: a.getClassifier().equals(classifier);
}
// We don't consider the version range in the comparison, just the resolved version
} }
public String getBaseVersion() { public String getBaseVersion() {

View File

@ -140,4 +140,16 @@ class DefaultArtifactTest {
assertNull(artifact.getVersion()); assertNull(artifact.getVersion());
assertNull(artifact.getBaseVersion()); 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);
}
} }