8311170: Simplify and modernize equals and hashCode in security area

Reviewed-by: djelinski, rriggs, valeriep
This commit is contained in:
Pavel Rappo 2023-08-09 12:34:40 +00:00
parent e9f751ab16
commit 19ae62ae2c
96 changed files with 567 additions and 951 deletions

View file

@ -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
@ -764,6 +764,7 @@ public final class AccessControlContext {
* and has the same set of {@code ProtectionDomain} objects as this context,
* {@code false} otherwise.
*/
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
@ -940,22 +941,20 @@ public final class AccessControlContext {
/**
* Returns the hash code value for this context. The hash code
* is computed by exclusive or-ing the hash code of all the protection
* domains in the context together.
*
* @return a hash code value for this context.
* {@return the hash code value for this context}
* The hash code is computed by exclusive or-ing the hash code of all the
* protection domains in the context together.
*/
@Override
public int hashCode() {
int hashCode = 0;
if (context == null)
return hashCode;
for (int i =0; i < context.length; i++) {
if (context[i] != null)
hashCode ^= context[i].hashCode();
for (ProtectionDomain protectionDomain : context) {
if (protectionDomain != null)
hashCode ^= protectionDomain.hashCode();
}
return hashCode;

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 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
@ -98,16 +98,15 @@ public final class AllPermission extends Permission {
* @param obj the object we are testing for equality with this object.
* @return true if {@code obj} is an {@code AllPermission}, false otherwise.
*/
@Override
public boolean equals(Object obj) {
return (obj instanceof AllPermission);
}
/**
* Returns the hash code value for this object.
*
* @return a hash code value for this object.
* {@return the hash code value for this object}
*/
@Override
public int hashCode() {
return 1;
}

View file

@ -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
@ -217,12 +217,10 @@ public abstract class BasicPermission extends Permission
/**
* Returns the hash code value for this object.
* {@return the hash code value for this object}
* The hash code used is the hash code of the name, that is,
* {@code getName().hashCode()}, where {@code getName} is
* from the {@code Permission} superclass.
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -27,6 +27,7 @@ package java.security;
import java.io.*;
import java.security.cert.CertPath;
import java.util.Objects;
/**
* This class encapsulates information about a code signer.
@ -98,19 +99,14 @@ public final class CodeSigner implements Serializable {
}
/**
* Returns the hash code value for this code signer.
* {@return the hash code value for this code signer}
* The hash code is generated using the signer's certificate path and the
* timestamp, if present.
*
* @return a hash code value for this code signer.
*/
@Override
public int hashCode() {
if (myhash == -1) {
if (timestamp == null) {
myhash = signerCertPath.hashCode();
} else {
myhash = signerCertPath.hashCode() + timestamp.hashCode();
}
myhash = signerCertPath.hashCode() + Objects.hashCode(timestamp);
}
return myhash;
}
@ -126,25 +122,15 @@ public final class CodeSigner implements Serializable {
* @return {@code true} if the objects are considered equal,
* {@code false} otherwise.
*/
@Override
public boolean equals(Object obj) {
if ((!(obj instanceof CodeSigner that))) {
return false;
}
if (this == that) {
if (this == obj) {
return true;
}
Timestamp thatTimestamp = that.getTimestamp();
if (timestamp == null) {
if (thatTimestamp != null) {
return false;
}
} else {
if ((!timestamp.equals(thatTimestamp))) {
return false;
}
}
return signerCertPath.equals(that.getSignerCertPath());
return obj instanceof CodeSigner other
&& Objects.equals(timestamp, other.getTimestamp())
&& signerCertPath.equals(other.getSignerCertPath());
}
/**

View file

@ -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
@ -130,16 +130,11 @@ public class CodeSource implements java.io.Serializable {
}
/**
* Returns the hash code value for this object.
*
* @return a hash code value for this object.
* {@return the hash code value for this object}
*/
@Override
public int hashCode() {
if (location != null)
return location.hashCode();
else
return 0;
return Objects.hashCode(location);
}
/**

View file

@ -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
@ -336,6 +336,7 @@ public abstract class Identity implements Principal, Serializable {
*
* @see #identityEquals
*/
@Override
public final boolean equals(Object identity) {
if (identity == this) {
return true;
@ -478,10 +479,9 @@ public abstract class Identity implements Principal, Serializable {
}
/**
* Returns a hashcode for this {@code Identity}.
*
* @return a hashcode for this {@code Identity}.
* {@return the hashcode for this {@code Identity}}
*/
@Override
public int hashCode() {
return name.hashCode();
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
@ -196,17 +196,13 @@ public final class PKCS12Attribute implements KeyStore.Entry.Attribute {
if (this == obj) {
return true;
}
if (!(obj instanceof PKCS12Attribute)) {
return false;
}
return Arrays.equals(encoded, ((PKCS12Attribute) obj).encoded);
return obj instanceof PKCS12Attribute other
&& Arrays.equals(encoded, other.encoded);
}
/**
* Returns the hashcode for this {@code PKCS12Attribute}.
* {@return the hashcode for this {@code PKCS12Attribute}}
* The hash code is computed from its DER encoding.
*
* @return the hash code
*/
@Override
public int hashCode() {

View file

@ -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
@ -138,7 +138,7 @@ public abstract class Permission implements Guard, java.io.Serializable {
*
* @return {@code true} if both {@code Permission} objects are equivalent.
*/
@Override
public abstract boolean equals(Object obj);
/**
@ -161,7 +161,7 @@ public abstract class Permission implements Guard, java.io.Serializable {
*
* @return a hash code value for this object.
*/
@Override
public abstract int hashCode();
/**

View file

@ -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
@ -50,6 +50,7 @@ public interface Principal {
* @return {@code true} if the {@code Principal} passed in is the same as
* that encapsulated by this {@code Principal}, and {@code false} otherwise.
*/
@Override
boolean equals(Object another);
/**
@ -60,10 +61,9 @@ public interface Principal {
String toString();
/**
* Returns a hashcode for this {@code Principal}.
*
* @return a hashcode for this {@code Principal}.
* {@return a hashcode for this {@code Principal}}
*/
@Override
int hashCode();
/**

View file

@ -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
@ -240,8 +240,7 @@ public class SecureClassLoader extends ClassLoader {
@Override
public int hashCode() {
String locationNoFrag = cs.getLocationNoFragString();
return locationNoFrag != null ? locationNoFrag.hashCode() : 0;
return Objects.hashCode(cs.getLocationNoFragString());
}
@Override

View file

@ -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
@ -35,6 +35,7 @@ import java.util.Hashtable;
import java.lang.reflect.*;
import java.security.cert.*;
import java.util.List;
import java.util.Objects;
/**
* The {@code UnresolvedPermission} class is used to hold Permissions that
@ -349,23 +350,13 @@ implements java.io.Serializable
}
// check name
if (this.name == null) {
if (that.name != null) {
return false;
}
} else if (!this.name.equals(that.name)) {
if (!Objects.equals(this.name, that.name)) {
return false;
}
// check actions
if (this.actions == null) {
if (that.actions != null) {
return false;
}
} else {
if (!this.actions.equals(that.actions)) {
return false;
}
if (!Objects.equals(this.actions, that.actions)) {
return false;
}
// check certs
@ -404,18 +395,11 @@ implements java.io.Serializable
}
/**
* Returns the hash code value for this object.
*
* @return a hash code value for this object.
* {@return the hash code value for this object}
*/
@Override
public int hashCode() {
int hash = type.hashCode();
if (name != null)
hash ^= name.hashCode();
if (actions != null)
hash ^= actions.hashCode();
return hash;
return Objects.hash(type, name, actions);
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -177,19 +177,20 @@ public abstract class CertPath implements Serializable {
* @return true if the specified object is equal to this certification path,
* false otherwise
*/
@Override
public boolean equals(Object other) {
if (this == other)
return true;
return other instanceof CertPath that
&& that.getType().equals(this.type)
&& this.type.equals(that.getType())
&& this.getCertificates().equals(that.getCertificates());
}
/**
* Returns the hashcode for this certification path. The hash code of
* a certification path is defined to be the result of the following
* calculation:
* {@return the hashcode value for this certification path}
* The hash code of a certification path is defined to be the result of
* the following calculation:
* <pre>{@code
* hashCode = path.getType().hashCode();
* hashCode = 31*hashCode + path.getCertificates().hashCode();
@ -198,9 +199,8 @@ public abstract class CertPath implements Serializable {
* {@code path1.hashCode()==path2.hashCode()} for any two certification
* paths, {@code path1} and {@code path2}, as required by the
* general contract of {@code Object.hashCode}.
*
* @return the hashcode value for this certification path
*/
@Override
public int hashCode() {
int hashCode = type.hashCode();
hashCode = 31*hashCode + getCertificates().hashCode();

View file

@ -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
@ -104,6 +104,7 @@ public abstract class Certificate implements java.io.Serializable {
* @return true iff the encoded forms of the two certificates
* match, false otherwise.
*/
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
@ -122,11 +123,10 @@ public abstract class Certificate implements java.io.Serializable {
}
/**
* Returns a hashcode value for this certificate from its
* encoded form.
*
* @return the hashcode value.
* {@return the hashcode value for this certificate from its
* encoded form}
*/
@Override
public int hashCode() {
int h = hash;
if (h == -1) {

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 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
@ -100,10 +100,8 @@ public final class URICertStoreParameters implements CertStoreParameters {
}
/**
* Returns a hash code value for this parameters object.
* {@return a hash code value for this parameters object}
* The hash code is generated using the URI supplied at construction.
*
* @return a hash code value for this parameters object.
*/
@Override
public int hashCode() {

View file

@ -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
@ -127,6 +127,7 @@ public abstract class X509CRL extends CRL implements X509Extension {
* @return true iff the encoded forms of the two CRLs
* match, false otherwise.
*/
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
@ -145,19 +146,15 @@ public abstract class X509CRL extends CRL implements X509Extension {
}
/**
* Returns a hashcode value for this CRL from its
* encoded form.
*
* @return the hashcode value.
* {@return a hashcode value for this CRL from its
* encoded form}
*/
@Override
public int hashCode() {
int retval = 0;
try {
byte[] crlData = X509CRLImpl.getEncodedInternal(this);
for (int i = 1; i < crlData.length; i++) {
retval += crlData[i] * i;
}
return retval;
return Arrays.hashCode(crlData);
} catch (CRLException e) {
return retval;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2020, 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
@ -26,6 +26,7 @@
package java.security.cert;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Date;
import javax.security.auth.x500.X500Principal;
@ -83,6 +84,7 @@ public abstract class X509CRLEntry implements X509Extension {
* @return true iff the encoded forms of the two CRL entries
* match, false otherwise.
*/
@Override
public boolean equals(Object other) {
if (this == other)
return true;
@ -92,34 +94,23 @@ public abstract class X509CRLEntry implements X509Extension {
byte[] thisCRLEntry = this.getEncoded();
byte[] otherCRLEntry = ((X509CRLEntry)other).getEncoded();
if (thisCRLEntry.length != otherCRLEntry.length)
return false;
for (int i = 0; i < thisCRLEntry.length; i++)
if (thisCRLEntry[i] != otherCRLEntry[i])
return false;
return Arrays.equals(thisCRLEntry, otherCRLEntry);
} catch (CRLException ce) {
return false;
}
return true;
}
/**
* Returns a hashcode value for this CRL entry from its
* encoded form.
*
* @return the hashcode value.
* {@return the hashcode value for this CRL entry from its
* encoded form}
*/
@Override
public int hashCode() {
int retval = 0;
try {
byte[] entryData = this.getEncoded();
for (int i = 1; i < entryData.length; i++)
retval += entryData[i] * i;
return Arrays.hashCode(this.getEncoded());
} catch (CRLException ce) {
return(retval);
return 0;
}
return(retval);
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -26,6 +26,7 @@ package java.security.spec;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Objects;
/**
* This immutable class defines an elliptic curve (EC)
@ -215,6 +216,7 @@ public class ECFieldF2m implements ECField {
* of ECFieldF2m and both {@code m} and the reduction
* polynomial match, false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
@ -226,15 +228,12 @@ public class ECFieldF2m implements ECField {
}
/**
* Returns a hash code value for this characteristic 2
* finite field.
* @return a hash code value.
* {@return the hash code value for this characteristic 2 finite field}
*/
@Override
public int hashCode() {
int value = m << 5;
value += (rp==null? 0:rp.hashCode());
// no need to involve ks here since ks and rp
// should be equivalent.
return value;
return m << 5 + Objects.hashCode(rp);
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -79,6 +79,7 @@ public class ECFieldFp implements ECField {
* @return true if {@code obj} is an instance
* of ECFieldFp and the prime value match, false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
@ -87,9 +88,9 @@ public class ECFieldFp implements ECField {
}
/**
* Returns a hash code value for this prime finite field.
* @return a hash code value.
* {@return a hash code value for this prime finite field}
*/
@Override
public int hashCode() {
return p.hashCode();
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 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
@ -93,6 +93,7 @@ public class ECPoint {
* @return true if {@code obj} is an instance of
* ECPoint and the affine coordinates match, false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (this == POINT_INFINITY) return false;
@ -103,9 +104,9 @@ public class ECPoint {
}
/**
* Returns a hash code value for this elliptic curve point.
* @return a hash code value.
* {@return the hash code value for this elliptic curve point}
*/
@Override
public int hashCode() {
if (this == POINT_INFINITY) return 0;
return x.hashCode() << 5 + y.hashCode();