In Java 6, I was able to use:
public static <T, UK extends T, US extends T> T getCountrySpecificComponent(UK uk, US us) {
Country country = CountryContext.getCountry();
if (country == Country.US) {
return us;
} else if (country == Country.UK) {
return uk;
} else {
throw new IllegalStateException("Unhandled country returned: "+country);
}
}
With these repositories:
public interface Repository{
List<User> findAll();
}
public interface RepositoryUS extends Repository{}
public interface RepositoryUK extends Repository{}
When using these:
RepositoryUK uk = ...
RepositoryUS us = ...
This line compiles in Java6, but does not work in Java7 (the error cannot find the character - because the compiler is looking for findAll () for the class object)
List<User> users = getCountrySpecificComponent(uk, us).findAll();
Compiles in Java 7
List<User> users = ((Repository)getCountrySpecificComponent(uk, us)).findAll();
I know this is a rather unusual use case, but is there a reason for this change? Or is there a way to tell the compiler a little smarter?
source
share