Java 6では、次のものを使用できました。
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);
}
}
これらのリポジトリを使用すると:
public interface Repository{
List<User> findAll();
}
public interface RepositoryUS extends Repository{}
public interface RepositoryUK extends Repository{}
これらを使用する場合:
RepositoryUK uk = ...
RepositoryUS us = ...
この行はJava6でコンパイルされますが、Java7では失敗します(エラーはシンボルを見つけることができません-コンパイラがクラスObjectでfindAll()を探すため)
List<User> users = getCountrySpecificComponent(uk, us).findAll();
これはJava7でコンパイルされます
List<User> users = ((Repository)getCountrySpecificComponent(uk, us)).findAll();
これはかなり珍しいユースケースであることを私は知っていますが、この変更の理由はありますか?または、コンパイラに少し「賢い」ことを伝える方法はありますか?