Skip to content

Commit bcd4dcc

Browse files
limvikjzheaux
authored andcommitted
Refactor equals method
Using the accessor method for fields instead of directly access
1 parent 8df8d40 commit bcd4dcc

File tree

4 files changed

+11
-14
lines changed

4 files changed

+11
-14
lines changed

core/src/main/java/org/springframework/security/authentication/jaas/JaasGrantedAuthority.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ public boolean equals(Object obj) {
5858
if (this == obj) {
5959
return true;
6060
}
61-
if (obj instanceof JaasGrantedAuthority) {
62-
JaasGrantedAuthority jga = (JaasGrantedAuthority) obj;
63-
return this.role.equals(jga.role) && this.principal.equals(jga.principal);
61+
if (obj instanceof JaasGrantedAuthority jga) {
62+
return this.role.equals(jga.getAuthority()) && this.principal.equals(jga.getPrincipal());
6463
}
6564
return false;
6665
}

core/src/main/java/org/springframework/security/core/authority/SimpleGrantedAuthority.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,8 +50,8 @@ public boolean equals(Object obj) {
5050
if (this == obj) {
5151
return true;
5252
}
53-
if (obj instanceof SimpleGrantedAuthority) {
54-
return this.role.equals(((SimpleGrantedAuthority) obj).role);
53+
if (obj instanceof SimpleGrantedAuthority sga) {
54+
return this.role.equals(sga.getAuthority());
5555
}
5656
return false;
5757
}

ldap/src/main/java/org/springframework/security/ldap/userdetails/LdapAuthority.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -113,14 +113,13 @@ public boolean equals(Object obj) {
113113
if (this == obj) {
114114
return true;
115115
}
116-
if (!(obj instanceof LdapAuthority)) {
116+
if (!(obj instanceof LdapAuthority other)) {
117117
return false;
118118
}
119-
LdapAuthority other = (LdapAuthority) obj;
120-
if (!this.dn.equals(other.dn)) {
119+
if (!this.dn.equals(other.getDn())) {
121120
return false;
122121
}
123-
return this.role.equals(other.role);
122+
return this.role.equals(other.getAuthority());
124123
}
125124

126125
@Override

web/src/main/java/org/springframework/security/web/authentication/switchuser/SwitchUserGrantedAuthority.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ public boolean equals(Object obj) {
6464
if (this == obj) {
6565
return true;
6666
}
67-
if (obj instanceof SwitchUserGrantedAuthority) {
68-
SwitchUserGrantedAuthority swa = (SwitchUserGrantedAuthority) obj;
69-
return this.role.equals(swa.role) && this.source.equals(swa.source);
67+
if (obj instanceof SwitchUserGrantedAuthority swa) {
68+
return this.role.equals(swa.getAuthority()) && this.source.equals(swa.getSource());
7069
}
7170
return false;
7271
}

0 commit comments

Comments
 (0)