Skip to content

Commit e4615fa

Browse files
Bae Jihongrwinch
authored andcommitted
Fix compiler warnings in spring-security-acl
- Use asSubclass() in AclClassIdUtils to avoid a unchecked cast warning - Replace raw Map type with Map<?, ?> unbounded wildcard to avoid raw type warnings - Use ArgumentMatchers to avoid a unchecked cast warning - Suppress an unavoidable unchecked warning in reflection-based test code Closes gh-18413 Signed-off-by: Bae Jihong <[email protected]>
1 parent 20493ef commit e4615fa

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

acl/src/main/java/org/springframework/security/acls/jdbc/AclClassIdUtils.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ private boolean hasValidClassIdType(ResultSet resultSet) {
8787
}
8888
}
8989

90-
private <T extends Serializable> @Nullable Class<T> classIdTypeFrom(ResultSet resultSet) throws SQLException {
90+
private @Nullable Class<? extends Serializable> classIdTypeFrom(ResultSet resultSet) throws SQLException {
9191
try {
9292
return classIdTypeFrom(resultSet.getString(DEFAULT_CLASS_ID_TYPE_COLUMN_NAME));
9393
}
@@ -97,17 +97,21 @@ private boolean hasValidClassIdType(ResultSet resultSet) {
9797
}
9898
}
9999

100-
private <T extends Serializable> @Nullable Class<T> classIdTypeFrom(String className) {
100+
private @Nullable Class<? extends Serializable> classIdTypeFrom(String className) {
101101
if (className == null) {
102102
return null;
103103
}
104104
try {
105-
return (Class) Class.forName(className);
105+
return Class.forName(className).asSubclass(Serializable.class);
106106
}
107107
catch (ClassNotFoundException ex) {
108108
log.debug("Unable to find class id type on classpath", ex);
109109
return null;
110110
}
111+
catch (ClassCastException ex) {
112+
log.debug("Class id type is not a Serializable type", ex);
113+
return null;
114+
}
111115
}
112116

113117
private <T> boolean canConvertFromStringTo(Class<T> targetType) {

acl/src/test/java/org/springframework/security/acls/domain/AclImplTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ public void maskPermissionGrantingStrategy() {
478478
}
479479

480480
@Test
481+
@SuppressWarnings("unchecked")
481482
public void hashCodeWithoutStackOverFlow() throws Exception {
482483
Sid sid = new PrincipalSid("pSid");
483484
ObjectIdentity oid = new ObjectIdentityImpl("type", 1);

acl/src/test/java/org/springframework/security/acls/jdbc/JdbcAclServiceTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.api.BeforeEach;
3030
import org.junit.jupiter.api.Test;
3131
import org.junit.jupiter.api.extension.ExtendWith;
32+
import org.mockito.ArgumentMatchers;
3233
import org.mockito.Mock;
3334
import org.mockito.junit.jupiter.MockitoExtension;
3435

@@ -46,7 +47,6 @@
4647
import static org.assertj.core.api.Assertions.assertThat;
4748
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4849
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
49-
import static org.mockito.ArgumentMatchers.any;
5050
import static org.mockito.ArgumentMatchers.anyList;
5151
import static org.mockito.ArgumentMatchers.anyString;
5252
import static org.mockito.ArgumentMatchers.eq;
@@ -109,7 +109,8 @@ public void findOneChildren() {
109109
List<ObjectIdentity> result = new ArrayList<>();
110110
result.add(new ObjectIdentityImpl(Object.class, "5577"));
111111
Object[] args = { "1", "org.springframework.security.acls.jdbc.JdbcAclServiceTests$MockLongIdDomainObject" };
112-
given(this.jdbcOperations.query(anyString(), any(RowMapper.class), eq(args))).willReturn(result);
112+
given(this.jdbcOperations.query(anyString(), ArgumentMatchers.<RowMapper<ObjectIdentity>>any(), eq(args)))
113+
.willReturn(result);
113114
ObjectIdentity objectIdentity = new ObjectIdentityImpl(MockLongIdDomainObject.class, 1L);
114115
List<ObjectIdentity> objectIdentities = this.aclService.findChildren(objectIdentity);
115116
assertThat(objectIdentities).hasSize(1);

acl/src/test/java/org/springframework/security/acls/jdbc/SpringCacheBasedAclCacheTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ public void constructorRejectsNullParameters() {
8080
assertThatIllegalArgumentException().isThrownBy(() -> new SpringCacheBasedAclCache(null, null, null));
8181
}
8282

83-
@SuppressWarnings("rawtypes")
8483
@Test
8584
public void cacheOperationsAclWithoutParent() {
8685
Cache cache = getCache();
87-
Map realCache = (Map) cache.getNativeCache();
86+
Map<?, ?> realCache = (Map<?, ?>) cache.getNativeCache();
8887
ObjectIdentity identity = new ObjectIdentityImpl(TARGET_CLASS, 100L);
8988
AclAuthorizationStrategy aclAuthorizationStrategy = new AclAuthorizationStrategyImpl(
9089
new SimpleGrantedAuthority("ROLE_OWNERSHIP"), new SimpleGrantedAuthority("ROLE_AUDITING"),
@@ -116,11 +115,10 @@ public void cacheOperationsAclWithoutParent() {
116115
assertThat(realCache).isEmpty();
117116
}
118117

119-
@SuppressWarnings("rawtypes")
120118
@Test
121119
public void cacheOperationsAclWithParent() throws Exception {
122120
Cache cache = getCache();
123-
Map realCache = (Map) cache.getNativeCache();
121+
Map<?, ?> realCache = (Map<?, ?>) cache.getNativeCache();
124122
Authentication auth = new TestingAuthenticationToken("user", "password", "ROLE_GENERAL");
125123
auth.setAuthenticated(true);
126124
SecurityContextHolder.getContext().setAuthentication(auth);

0 commit comments

Comments
 (0)