多かれ少なかれ次のような方法があります。ただし、現在、実行時に関数 bla() で返される List を返しList<Bar>
ます。
私は両方を作る方法を探しています
List<Interface> = troubleFuction(foo, bar.getCLass());;
と
List<Bar> = troubleFuction(foo, bar.getCLass());;
可能。基本的に、インターフェイスと互換性のあるリストを返すようにしたいのですが、これにより次のエラーが発生します
List<capture#3-of ? extends Bar>
*タイプの不一致: からに変換できませんList<Interface>*
この戻り値の型を可能にする方法はありますか、または実行時の消去によりこれが不可能になりますか
public <T1 extends Interface, T2 extends Interface> List<"problem"> troubleFunction( T1 in, Class<T2> clazz) {
return in.doStuffWhichGeneratesAlistOF(clazz)
}
public void bla() {
Foo foo = new Foo(); // implements interface
Bar bar = new Bar(); // implements interface
List<Interface> ifaces = toubleFuction(foo, bar.getCLass());
List<Bar> mustAlsoWork = toubleFuction(foo, bar.getCLass());
}
編集:既存のコードベースの多くでは、メソッドは次のように呼び出されます
List<Bar> result = troubleFunction(List<Interface> list, Bar.class);
したがって、この戻り値の型は互換性を維持する必要があります (書き換え/リファクタリングはオプションではありません)。
<? super Bar>
基本的に、メソッドが次のように呼び出された場合に List を返すようにしたい
troublefunction(foo, Bar.class);
と
List<? super Foo>
呼ばれるとき
troublefunction(foo, Bar.class);