Java® Language Specification((§4.8) wrote:The type of a constructor (§8.8), instance method (§8.4, §9.4), or non-static field (§8.3) M of a raw type C that is not inherited from its superclasses or superinterfaces is the raw type that corresponds to the erasure of its type in the generic declaration corresponding to C.
Greg Charles wrote:Yes, that's a good way of putting it. Why isn't the compiler more picky in the first example?
Greg Charles wrote:What about the second example causes it to become more picky?
Greg Charles wrote:And, what about the third example makes it not picky again?
Greg Charles wrote:Another interesting point: the unchecked cast warnings you note for the first example are on lines 5 and 7, the casts to T. However, the ClassCastException if it happens, happens on line 15. I can't think of a case where the CCE could actually occur where the warnings say it might.
However, the ClassCastException (CCE) if it happens...
JLS §4.8 wrote:A non-generic class or interface type is not a raw type.
JLS §4.6 wrote:The type parameters of a constructor or method (§8.4.4), and the return type (§8.4.5) of a method, also undergo erasure if the constructor or method's signature is erased.
Greg Charles wrote:At least, I don't see anything in the JLS (Justice League of Siberia) that says when type erasure is applied to a parametrized class, it must also be extended to unrelated generics used in instance methods
Moreover, I can't see any reason why this would be true.
Greg wrote:...The casts and the compiler warnings are on lines 5 and 7, but the ClassCastException (aka CCE) happens on line 15, where there is no cast...
The compiler works as per the rules described in the java® Language Specification,so it's not a bug it's just obeying the rules.
Greg wrote:My contention is this shows there is a bug in the Java compiler.
I think you missed it have a look on it→Generics erasure for conctructor,instance method,non-static field when using a Raw Type..
Greg wrote:At least, I don't see anything in the JLS (
Justice League of Siberia) that says when type erasure is applied to a parametrized class, it must also be extended to unrelated generics used in instance methods.