2

Java 1.6u32 を使用しています。レガシーコードでは、次の方法があります。

static <T> T lookup(Class<? super T> interfaceClass){...}

その使用法には次の 3 種類があります。

ConcreteX x = lookup(InterfaceX.class);
InterfaceX x = lookup(InterfaceX.class);
lookup(InterfaceX.class).someMethodOfInterfaceX();

問題は、ant ビルド スクリプトを使用すると 3 番目のスクリプトがコンパイルされることですが、Intellij Idea でプロジェクトをビルドすると失敗します。エラーメッセージは次のとおりです。

... cannot find symbol:
symbol: method someMethodOfInterfaceX()
location: class java.lang.Object

InterfaceX に明示的にキャストすると、IDEA ビルドが成功します。

((InterfaceX)lookup(InterfaceX.class)).someMethodOfInterfaceX();

コードが間違っていて ant でコンパイルすべきではないのでしょうか、それとも IDEA コンパイラに何か問題がありますか?

編集: - エラーの再現に使用されるクラス:

public class ErasureProblem {
    public static void main(String[] args) {
        ImplementationX n = lookup(InterfaceX.class);
        lookup(InterfaceX.class).m();
    }

    static <T extends Object> T lookup(Class<? super T> cls){
        return null;
    }
}

public class ImplementationX implements InterfaceX {
    @Override
    public Integer m() {
        return 0;
    }
}

public interface InterfaceX {
    Integer m();
}
4

0 に答える 0