Bug 109098

Summary: [1.5][compiler] Should insert generic cast in presence of multi-bounded types
Product: [Eclipse Project] JDT Reporter: Philipe Mulet <philippe_mulet>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 3.1   
Target Milestone: 3.2 M2   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Philipe Mulet CLA 2005-09-08 16:05:13 EDT
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();
	}
}
Comment 1 Philipe Mulet CLA 2005-09-08 16:05:47 EDT
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");
	}
}
Comment 2 Philipe Mulet CLA 2005-09-08 16:06:43 EDT
Affected types are: type variables with multipe bounds (directly or indirectly),
captures and intersection types.
Comment 3 Philipe Mulet CLA 2005-09-09 13:26:47 EDT
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.
Comment 4 Philipe Mulet CLA 2005-09-09 13:27:37 EDT
Added GenericTypeTest#test821-test825
Comment 5 Philipe Mulet CLA 2005-09-27 04:35:24 EDT
Closing