If I take out the @SuppressWarnings("unchecked")
from this code, I get:
Unchecked cast: 'java.lang.Class<capture<?>>' to 'java.lang.Class<? extends com.megacorp.security.User>'
See how the constructor I grab a reference to is used later in the code. Can I refactor it or do I just have to live with it?
private final Constructor<? extends User> constructor;
public LdapUserDetailsMapper(String userClassName)
throws ClassNotFoundException, NoSuchMethodException {
@SuppressWarnings("unchecked")
Class<? extends User> userClass =
(Class<? extends User>) Class.forName(userClassName);
constructor = userClass.getConstructor(String.class, Collection.class);
if (constructor == null) {
throw new IllegalArgumentException(String.format(
"Couldn't find user constructor for [%s]", userClassName));
}
}
@Override
public UserDetails mapUserFromContext(String login,
Collection<? extends GrantedAuthority> authorities) {
//Extract basic user info
User user;
try {
user = constructor.newInstance(login, mergedAuthorities);
return user;
} catch (IllegalAccessException
| InstantiationException
| InvocationTargetException e) {
throw new RuntimeException(
String.format("Cannot instantiate new User [%s]", login),
e);
}
}