Bug 69542 - [1.5] ByteCode differs from javac ByteCode in behaviour
Summary: [1.5] ByteCode differs from javac ByteCode in behaviour
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1 M1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-07-07 18:15 EDT by Karsten Becker CLA
Modified: 2005-01-11 11:02 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Karsten Becker CLA 2004-07-07 18:15:36 EDT
Hi,
The following Code:
	static class Container<T>{
	    private T val;
	    public T getVal() {
	        return val;
	    }
	    public void setVal(T val) {
	        this.val = val;
	    }
	}
	public static void badMethod(Container<?> param){
	    Container x=param;
	    x.setVal("BAD");
	}
	public static void main(String[] args) {
	    Container<Integer> cont=new Container<Integer>();
	    cont.setVal(new Integer(0));
	    badMethod(cont);
	    System.out.println(cont.getVal()); //ClassCastException
}
leads to:
Exception in thread "main" java.lang.ClassCastException: java.lang.String
	at SpecialTest.main(SpecialTest.java:57)
when executed within Eclipse.
But JVM just prints: BAD
Comment 1 Philipe Mulet CLA 2004-07-08 11:54:07 EDT
Looks like javac doesn't perform the checkcast, and thus doesn't notice the 
problem.
If replacing line:
	    System.out.println(cont.getVal()); //ClassCastException
with:
	    Integer someVal = cont.getVal();

Then it will generate the checkcast, and thus raise the ClassCastException.
Comment 2 Philipe Mulet CLA 2004-07-08 12:09:15 EDT
We should be more permissive in generic cast emission. If the type expectation 
is lower, we should not check as much.
e.g.
Integer val1 = cont.getVal();  // checkcast Integer
Number val2 = cont.getVal();  // checkcast Number
Object val3 = cont.getVal();  // no checkcast 
Comment 3 Philipe Mulet CLA 2004-07-09 17:29:47 EDT
Tuned generic cast to be less strict so as to only match expectation.
Added regression tests GenericTypeTest#test232-239.

Fixed