spring-projects / spring-security

Spring Security
http://spring.io/projects/spring-security
Apache License 2.0
8.81k stars 5.9k forks source link

SEC-1908: Remove bounded wildcard return types #2127

Open spring-projects-issues opened 12 years ago

spring-projects-issues commented 12 years ago

Mikhail Mazursky (Migrated from SEC-1908) said:

I was updating spring 3.0.x to 3.1.0 and one of my tests (with Mockito) showed a compilation error. The problem i encountered was the same as described here 1:

LdapAuthoritiesPopulator authoritiesPopulator = mock(LdapAuthoritiesPopulator.class);

when(authoritiesPopulator.getGrantedAuthorities(userData, USERNAME)).thenReturn(AuthorityUtils.NO_AUTHORITIES);

This is not type safe but that problem can be worked around by:

doReturn(AuthorityUtils.NO_AUTHORITIES).when(authoritiesPopulator).getGrantedAuthorities(userData, USERNAME);

The question is should the LdapAuthoritiesPopulator interface's method getGrantedAuthorities have a bounded wildcard return type or not? I think it's better to remove wildcard as it gives nothing to the calling code.

Spring 3.0.x have it as:

Collection getGrantedAuthorities(DirContextOperations userData, String username);

and 3.1.0 as:

Collection<? extends GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username);

The same problem with GrantedAuthoritiesMapper, GrantedAuthoritiesContainer, Attributes2GrantedAuthoritiesMapper, WebSpherePreAuthenticatedWebAuthenticationDetailsSource and possibly some other classes/ifaces.

Interesting reading on the topic 2.

Methods with bounded wildcard return types

PUBLIC

spring-projects-issues commented 12 years ago

Luke Taylor said:

This was requested in SEC-1550, to cater for classes which use custom GrantedAuthority implementations.

spring-projects-issues commented 12 years ago

Mikhail Mazursky said:

From the mentioned issue i still can't see any benefit to return bounded wildcad types. When i use ??void test(List<? super T> arg)?? method signature that allows me to safely pass lists of child types (U extends T) to method. What's the point to return bounded wildcard types? As you said in those issue ??You can still only read the upper bound of the wildcard from the collection??. Wildcard only helps to avoid in-method cast from ??List?? to ??List?? but it's 100% safe as everything that is ??Authority?? can be used as ??GrantedAuthority??.