Use consistent equals/hashCode/toString order

Ensure that `equals` `hashCode` and `toString` methods always appear in
the same order. This aligns with the style used in Spring Framework.

Issue gh-8945
This commit is contained in:
Phillip Webb 2020-07-29 20:03:19 -07:00 committed by Rob Winch
parent 612fb22a7f
commit ec6a4cb3f0
12 changed files with 95 additions and 111 deletions

View File

@ -52,18 +52,25 @@ public abstract class AbstractPermission implements Permission {
}
@Override
public final boolean equals(Object arg0) {
if (arg0 == null) {
public final boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!(arg0 instanceof Permission)) {
if (!(obj instanceof Permission)) {
return false;
}
Permission other = (Permission) obj;
return (this.mask == other.getMask());
}
Permission rhs = (Permission) arg0;
@Override
public final int hashCode() {
return this.mask;
}
return (this.mask == rhs.getMask());
@Override
public final String toString() {
return this.getClass().getSimpleName() + "[" + getPattern() + "=" + this.mask + "]";
}
@Override
@ -76,14 +83,4 @@ public abstract class AbstractPermission implements Permission {
return AclFormattingUtils.printBinary(this.mask, this.code);
}
@Override
public final String toString() {
return this.getClass().getSimpleName() + "[" + getPattern() + "=" + this.mask + "]";
}
@Override
public final int hashCode() {
return this.mask;
}
}

View File

@ -67,14 +67,6 @@ final class DefaultServiceAuthenticationDetails extends WebAuthenticationDetails
return this.serviceUrl;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + this.serviceUrl.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
@ -87,6 +79,14 @@ final class DefaultServiceAuthenticationDetails extends WebAuthenticationDetails
return this.serviceUrl.equals(that.getServiceUrl());
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + this.serviceUrl.hashCode();
return result;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();

View File

@ -53,27 +53,25 @@ public final class JaasGrantedAuthority implements GrantedAuthority {
return this.role;
}
@Override
public int hashCode() {
int result = this.principal.hashCode();
result = 31 * result + this.role.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof JaasGrantedAuthority) {
JaasGrantedAuthority jga = (JaasGrantedAuthority) obj;
return this.role.equals(jga.role) && this.principal.equals(jga.principal);
}
return false;
}
@Override
public int hashCode() {
int result = this.principal.hashCode();
result = 31 * result + this.role.hashCode();
return result;
}
@Override
public String toString() {
return "Jaas Authority [" + this.role + "," + this.principal + "]";

View File

@ -62,18 +62,17 @@ public class InMemoryResource extends AbstractResource {
return new ByteArrayInputStream(this.source);
}
@Override
public int hashCode() {
return 1;
}
@Override
public boolean equals(Object res) {
if (!(res instanceof InMemoryResource)) {
return false;
}
return Arrays.equals(this.source, ((InMemoryResource) res).source);
}
@Override
public int hashCode() {
return 1;
}
}

View File

@ -3,7 +3,6 @@
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<suppress files=".*" checks="SpringMethodOrder" />
<suppress files=".*" checks="SpringMethodVisibility" />
<suppress files=".*" checks="SpringTernary" />
<suppress files=".*" checks="WhitespaceAfter" />

View File

@ -275,14 +275,6 @@ public class AuthenticationPrincipalArgumentResolverTests {
this.property = toCopy.property;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.property == null) ? 0 : this.property.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
@ -306,6 +298,14 @@ public class AuthenticationPrincipalArgumentResolverTests {
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.property == null) ? 0 : this.property.hashCode());
return result;
}
}
}

View File

@ -143,13 +143,13 @@ final class SecurityMockMvcConfigurer extends MockMvcConfigurerAdapter {
}
@Override
public int hashCode() {
return getDelegate().hashCode();
public boolean equals(Object obj) {
return getDelegate().equals(obj);
}
@Override
public boolean equals(Object obj) {
return getDelegate().equals(obj);
public int hashCode() {
return getDelegate().hashCode();
}
@Override

View File

@ -46,32 +46,28 @@ public class RequestKey {
return this.method;
}
@Override
public int hashCode() {
int result = this.url.hashCode();
result = 31 * result + (this.method != null ? this.method.hashCode() : 0);
return result;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RequestKey)) {
return false;
}
RequestKey key = (RequestKey) obj;
if (!this.url.equals(key.url)) {
return false;
}
if (this.method == null) {
return key.method == null;
}
return this.method.equals(key.method);
}
@Override
public int hashCode() {
int result = this.url.hashCode();
result = 31 * result + (this.method != null ? this.method.hashCode() : 0);
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(this.url.length() + 7);

View File

@ -59,27 +59,25 @@ public final class SwitchUserGrantedAuthority implements GrantedAuthority {
return this.role;
}
@Override
public int hashCode() {
int result = this.role.hashCode();
result = 31 * result + this.source.hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof SwitchUserGrantedAuthority) {
SwitchUserGrantedAuthority swa = (SwitchUserGrantedAuthority) obj;
return this.role.equals(swa.role) && this.source.equals(swa.source);
}
return false;
}
@Override
public int hashCode() {
int result = this.role.hashCode();
result = 31 * result + this.source.hashCode();
return result;
}
@Override
public String toString() {
return "Switch User Authority [" + this.role + "," + this.source + "]";

View File

@ -129,28 +129,12 @@ public final class LazyCsrfTokenRepository implements CsrfTokenRepository {
return this.delegate.getToken();
}
@Override
public String toString() {
return "SaveOnAccessCsrfToken [delegate=" + this.delegate + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.delegate == null) ? 0 : this.delegate.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
SaveOnAccessCsrfToken other = (SaveOnAccessCsrfToken) obj;
@ -165,6 +149,19 @@ public final class LazyCsrfTokenRepository implements CsrfTokenRepository {
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.delegate == null) ? 0 : this.delegate.hashCode());
return result;
}
@Override
public String toString() {
return "SaveOnAccessCsrfToken [delegate=" + this.delegate + "]";
}
private void saveTokenIfNecessary() {
if (this.tokenRepository == null) {
return;

View File

@ -283,13 +283,13 @@ public abstract class OnCommittedResponseWrapper extends HttpServletResponseWrap
}
@Override
public int hashCode() {
return this.delegate.hashCode();
public boolean equals(Object obj) {
return this.delegate.equals(obj);
}
@Override
public boolean equals(Object obj) {
return this.delegate.equals(obj);
public int hashCode() {
return this.delegate.hashCode();
}
@Override
@ -529,16 +529,6 @@ public abstract class OnCommittedResponseWrapper extends HttpServletResponseWrap
this.delegate.close();
}
@Override
public int hashCode() {
return this.delegate.hashCode();
}
@Override
public boolean equals(Object obj) {
return this.delegate.equals(obj);
}
@Override
public void print(boolean b) throws IOException {
trackContentLength(b);
@ -658,6 +648,16 @@ public abstract class OnCommittedResponseWrapper extends HttpServletResponseWrap
this.delegate.setWriteListener(writeListener);
}
@Override
public boolean equals(Object obj) {
return this.delegate.equals(obj);
}
@Override
public int hashCode() {
return this.delegate.hashCode();
}
@Override
public String toString() {
return getClass().getName() + "[delegate=" + this.delegate.toString() + "]";

View File

@ -279,14 +279,6 @@ public class AuthenticationPrincipalArgumentResolverTests {
this.property = toCopy.property;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.property == null) ? 0 : this.property.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
@ -310,6 +302,14 @@ public class AuthenticationPrincipalArgumentResolverTests {
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.property == null) ? 0 : this.property.hashCode());
return result;
}
}
}