Javaジェネリックシステムのいくつかのコーナーケースでまだ問題があります。
私はこの方法を持っています(私は署名にのみ興味があります):
interface Extractor<RETURN_TYPE> {
public <U extends Enum<U>> RETURN_TYPE extractEnum(final Class<U> enumType);
}
(実装が時々 EnumSet を抽出し、実装が JComboBox などを抽出するインターフェースについて考えてみてください。)
実行時に取得したクラスで呼び出したいので、次のように呼び出します。
public static <RETURN_TYPE> RETURN_TYPE extractField(final Extractor<RETURN_TYPE> extractor, final Field field) {
final Class<?> type = field.getType();
if (type.isEnum())
return extractor.extractEnum(/* error here*/type.asSubclass(Enum.class));
throw new RuntimeException("the rest of the visitor is not necessary here");
}
奇妙なエラーメッセージが表示されます:互換性のないタイプが見つかりました:java.lang.Object required:RETURN_TYPE
タイプの「t」の前で、呼び出しの開始ブラケットの直後の場合のメッセージの場所。
非ジェネリックなコンテキストから呼び出すと、動作します:
Integer extractField(final Extractor<Integer> extractor, final Field field) {
final Class<?> type = field.getType();
if (type.isEnum())
return extractor.extractEnum(type.asSubclass(Enum.class));
throw new RuntimeException("the rest of the visitor is not necessary here");
}
誰かがこの問題の説明と解決策を持っていますか?
これで遊びたい人のための完全なファイルは次のとおりです。
public class Blah {
interface Extractor<RETURN_TYPE> {
public <U extends Enum<U>> RETURN_TYPE extractEnum(final Class<U> enumType);
}
public static <RETURN_TYPE> RETURN_TYPE extractField(final Extractor<RETURN_TYPE> extractor, final Field field) {
final Class<?> type = field.getType();
if (type.isEnum())
return extractor.extractEnum(/* error here*/type.asSubclass(Enum.class));
throw new RuntimeException("the rest of the visitor is not necessary here");
}
public static Integer extractField(final Extractor<Integer> extractor, final Field field) {
final Class<?> type = field.getType();
if (type.isEnum())
return extractor.extractEnum(type.asSubclass(Enum.class));
throw new RuntimeException("the rest of the visitor is not necessary here");
}
}
前もって感謝します、
ニコ