Skip to content

8311170: Simplify and modernize equals and hashCode in security area #14738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -153,6 +153,7 @@ public Collection<PKCS10Attribute> getAttributes() {
* @return true if all the entries match that of the Other,
* false otherwise.
*/
@Override
public boolean equals(Object other) {
if (this == other)
return true;
@@ -166,26 +167,24 @@ public boolean equals(Object other) {
int len = attrs.length;
if (len != map.size())
return false;
PKCS10Attribute thisAttr, otherAttr;
PKCS10Attribute thisAttr;
String key;
for (int i=0; i < len; i++) {
otherAttr = attrs[i];
for (PKCS10Attribute otherAttr : attrs) {
key = otherAttr.getAttributeId().toString();

thisAttr = map.get(key);
if (thisAttr == null)
return false;
if (! thisAttr.equals(otherAttr))
if (!thisAttr.equals(otherAttr))
return false;
}
return true;
}

/**
* Returns a hashcode value for this PKCS10Attributes.
*
* @return the hashcode value.
* {@return a hashcode value for this PKCS10Attributes}
*/
@Override
public int hashCode() {
return map.hashCode();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -1119,10 +1119,7 @@ public PermissionEntry(String permission,
*/
@Override
public int hashCode() {
int retval = permission.hashCode();
if (name != null) retval ^= name.hashCode();
if (action != null) retval ^= action.hashCode();
return retval;
return Objects.hash(permission, name, action);
}

@Override
@@ -1133,29 +1130,9 @@ public boolean equals(Object obj) {
if (! (obj instanceof PermissionEntry that))
return false;

if (this.permission == null) {
if (that.permission != null) return false;
} else {
if (!this.permission.equals(that.permission)) return false;
}

if (this.name == null) {
if (that.name != null) return false;
} else {
if (!this.name.equals(that.name)) return false;
}

if (this.action == null) {
if (that.action != null) return false;
} else {
if (!this.action.equals(that.action)) return false;
}

if (this.signedBy == null) {
return that.signedBy == null;
} else {
return this.signedBy.equals(that.signedBy);
}
return Objects.equals(this.permission, that.permission)
&& Objects.equals(this.name, that.name)
&& Objects.equals(this.signedBy, that.signedBy);
}

public void write(PrintWriter out) {
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -226,10 +226,6 @@ public KeyIdentifier getKeyIdentifier() {
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

if (this == obj) {
return true;
}
@@ -242,9 +238,7 @@ public boolean equals(Object obj) {
}

/**
* Returns the hash code value for this {@code ResponderId}
*
* @return the hash code value for this {@code ResponderId}
* {@return the hash code value for this {@code ResponderId}}
*/
@Override
public int hashCode() {
24 changes: 6 additions & 18 deletions src/java.base/share/classes/sun/security/x509/EDIPartyName.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -170,30 +170,18 @@ public String getPartyName() {
*
* @return true if the two names match
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof EDIPartyName))
return false;
String otherAssigner = ((EDIPartyName)other).assigner;
if (this.assigner == null) {
if (otherAssigner != null)
return false;
} else {
if (!(this.assigner.equals(otherAssigner)))
return false;
}
String otherParty = ((EDIPartyName)other).party;
if (this.party == null) {
return otherParty == null;
} else {
return this.party.equals(otherParty);
}
return Objects.equals(this.assigner, ((EDIPartyName)other).assigner)
&& Objects.equals(this.party, ((EDIPartyName)other).party);
}

/**
* Returns the hash code value for this EDIPartyName.
*
* @return a hash code value.
* {@return the hash code value for this EDIPartyName}
*/
@Override
public int hashCode() {
if (myhash == -1) {
myhash = 37 + (party == null ? 1 : party.hashCode());
18 changes: 6 additions & 12 deletions src/java.base/share/classes/sun/security/x509/GeneralSubtree.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -152,27 +152,21 @@ public String toString() {
* @param other GeneralSubtree to compare to this
* @return true if match
*/
@Override
public boolean equals(Object other) {
if (!(other instanceof GeneralSubtree otherGS))
return false;
if (this.name == null) {
if (otherGS.name != null) {
return false;
}
} else {
if (!((this.name).equals(otherGS.name)))
return false;
}
if (!Objects.equals(this.name, otherGS.name))
return false;
if (this.minimum != otherGS.minimum)
return false;
return this.maximum == otherGS.maximum;
}

/**
* Returns the hash code for this GeneralSubtree.
*
* @return a hash code value.
* {@return the hash code for this GeneralSubtree}
*/
@Override
public int hashCode() {
if (myhash == -1) {
myhash = 17;
16 changes: 5 additions & 11 deletions src/java.base/share/classes/sun/security/x509/X500Name.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -395,6 +395,7 @@ public boolean isEmpty() {
* Calculates a hash code value for the object. Objects
* which are equal will also have the same hashcode.
*/
@Override
public int hashCode() {
return getRFC2253CanonicalName().hashCode();
}
@@ -404,6 +405,7 @@ public int hashCode() {
*
* @return true iff the names are identical.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
@@ -416,17 +418,9 @@ public boolean equals(Object obj) {
return this.canonicalDn.equals(other.canonicalDn);
}
// quick check that number of RDNs and AVAs match before canonicalizing
int n = this.names.length;
if (n != other.names.length) {
if (!Arrays.equals(this.names, other.names,
Comparator.comparingInt(n -> n.assertion.length)))
return false;
}
for (int i = 0; i < n; i++) {
RDN r1 = this.names[i];
RDN r2 = other.names[i];
if (r1.assertion.length != r2.assertion.length) {
return false;
}
}
// definite check via canonical form
String thisCanonical = this.getRFC2253CanonicalName();
String otherCanonical = other.getRFC2253CanonicalName();