Community
Participate
Working Groups
Build 3.1.0 The spec says that generics require cast insertion in exactly 2 situations (see discussion in 5.2 Assignment conversion, p. 95). However, on following example, it seems a cast to Runnable needs to be inserted, as does javac. import java.io.Serializable; public class X<T extends Serializable & Runnable> { void foo(T t) { t.run(); } }
Actually, any expression yielding a type variable as its type (possibly resulting from capture as well) is to be checked before used, in case some type assumptions would need to be enforced. e.g. import java.io.Serializable; public class X<T extends Serializable & Runnable, V extends T> { void foo(T t) { Runnable r1 = t; bar(t); } void bar(Runnable r) {} public static void main(String[] args) { new X<A, A>().foo(new A()); } } class A implements Serializable, Runnable { public void run() { System.out.println("AA"); } }
Affected types are: type variables with multipe bounds (directly or indirectly), captures and intersection types.
Actually, maybe casts are not mandated. Bytecode verifiers seem to defer the check until runtime (similar to standard situation arising from binary compatibility problems). So even though the bytecode looks troublesome, at runtime the proper object types are available and no cast is required.
Added GenericTypeTest#test821-test825
Closing