Преглед изворни кода

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
Phillip Webb пре 5 година
родитељ
комит
ec6a4cb3f0

+ 12 - 15
acl/src/main/java/org/springframework/security/acls/domain/AbstractPermission.java

@@ -52,38 +52,35 @@ 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 rhs = (Permission) arg0;
-
-		return (this.mask == rhs.getMask());
+		Permission other = (Permission) obj;
+		return (this.mask == other.getMask());
 	}
 
 	@Override
-	public final int getMask() {
+	public final int hashCode() {
 		return this.mask;
 	}
 
-	@Override
-	public String getPattern() {
-		return AclFormattingUtils.printBinary(this.mask, this.code);
-	}
-
 	@Override
 	public final String toString() {
 		return this.getClass().getSimpleName() + "[" + getPattern() + "=" + this.mask + "]";
 	}
 
 	@Override
-	public final int hashCode() {
+	public final int getMask() {
 		return this.mask;
 	}
 
+	@Override
+	public String getPattern() {
+		return AclFormattingUtils.printBinary(this.mask, this.code);
+	}
+
 }

+ 8 - 8
cas/src/main/java/org/springframework/security/cas/web/authentication/DefaultServiceAuthenticationDetails.java

@@ -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();

+ 7 - 9
core/src/main/java/org/springframework/security/authentication/jaas/JaasGrantedAuthority.java

@@ -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 + "]";

+ 5 - 6
core/src/main/java/org/springframework/security/util/InMemoryResource.java

@@ -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;
+	}
+
 }

+ 0 - 1
etc/checkstyle/checkstyle-suppressions.xml

@@ -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" />

+ 8 - 8
messaging/src/test/java/org/springframework/security/messaging/context/AuthenticationPrincipalArgumentResolverTests.java

@@ -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;
+		}
+
 	}
 
 }

+ 4 - 4
test/src/main/java/org/springframework/security/test/web/servlet/setup/SecurityMockMvcConfigurer.java

@@ -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

+ 7 - 11
web/src/main/java/org/springframework/security/web/access/intercept/RequestKey.java

@@ -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);

+ 7 - 9
web/src/main/java/org/springframework/security/web/authentication/switchuser/SwitchUserGrantedAuthority.java

@@ -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 + "]";

+ 14 - 17
web/src/main/java/org/springframework/security/web/csrf/LazyCsrfTokenRepository.java

@@ -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;

+ 14 - 14
web/src/main/java/org/springframework/security/web/util/OnCommittedResponseWrapper.java

@@ -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() + "]";

+ 8 - 8
web/src/test/java/org/springframework/security/web/method/annotation/AuthenticationPrincipalArgumentResolverTests.java

@@ -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;
+		}
+
 	}
 
 }