简体   繁体   English

Java泛型Eclipse编译器的bug?

[英]Java generics Eclipse compiler bug?

My question is a follow up of an other guy's question: Unbounded wildcard passed to method 我的问题是对另一个人的问题的跟进: 无限的通配符传递给方法

He was interested why the following code compiled: 他有兴趣为什么编译以下代码:

public class ColTest {
    static<T> T wildSub(ArrayList<? extends T> holder, T arg){
        T t=holder.get(0);
        return t;
    }

    public static void main(String[] args) {
        ArrayList<?> list=new ArrayList<Long>(Arrays.asList(2L,3L,7L));
        Long lng=1L;
        ColTest.wildSub(list, lng);
    }
}

We came to a conclusion that the trick was that compiler inferred the ? 我们得出结论,诀窍是编译器推断了? as an Object and made the following Long argument pass thanks to the trivial inheritance of Object->Long. 作为一个Object,由于Object-> Long的简单继承,使得下面的Long参数传递。

The code does compile using Sun/Oracle javac (I use 1.6.0_26-b03), but does not compile in Eclipse (I use Helios) where it displays the following compilation error: 代码使用Sun / Oracle javac编译(我使用1.6.0_26-b03),但不在Eclipse(我使用Helios)中编译,它显示以下编译错误:

The method wildSub(ArrayList<? extends T>, T) in the type ColTest is not applicable for the arguments (ArrayList<capture#2-of ?>, Long)

My question is: 我的问题是:

Is this a bug in the Java compiler implementation Eclipse uses or some kind of ambiguity in Java 'generics inference algorithm' specification that is valid and just implemented differently by Eclipse? 这是Eclipse使用的Java编译器实现中的一个错误,还是Java的“泛型推理算法”规范中的某种歧义,它是有效的,只是Eclipse实现的不同?

It appears to be an Eclipse bug. 它似乎是一个Eclipse bug。

T should be inferred to as Object, per 15.12.2.7. 根据15.12.2.7,T应推断为Object。

15.12.2.8 also has a catch-all clause: "Any remaining type variables that have not yet been inferred are then inferred to have type Object" 15.12.2.8还有一个catch-all子句:“然后推断出任何尚未推断的剩余类型变量都具有Object类型”

with T=Object, per 15.12.2.2, the method is applicable . 根据T =对象,按照15.12.2.2,该方法适用

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12 http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM