Say I have following sample application, I'm using java 8.
I have following class that has a child class:
public class GrantedAuthority { }
public class GrantedAuthoritySubClass extends GrantedAuthority { }
Following class is where the tricky part happens:
public class UserDetails {
Collection<? extends GrantedAuthority> authorities;
public Collection<? extends GrantedAuthority> getAuthorities(){
return authorities;
}
}
My main method:
public class Main {
public static void main(String[] args) {
UserDetails u = new UserDetails();
List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
list.add(new GrantedAuthoritySubClass());
u.getAuthorities().addAll(list);
}
}
When I run this I get the following error:
Error:(12, 35) java: incompatible types:
java.util.List<GrantedAuthority>
cannot be converted tojava.util.Collection<? extends capture#1 of ? extends GrantedAuthority>
My problem is, how I can pass a collection of GrantedAuthoritySubClass
Objects to the collection returned by u.getAuthorities()
. I can't figure it out and there are so many compilation errors attached to various ways I've tried.