Community
Participate
Working Groups
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
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.
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
Tuned generic cast to be less strict so as to only match expectation. Added regression tests GenericTypeTest#test232-239. Fixed