Community
Participate
Working Groups
Build ID: I20090806-1400 Steps To Reproduce: 1. Eclipse doesn't report an unsafe cast with the following snippet (from http://www.dzone.com/links/two_java_typesystem_holes.html) The cast in getOtherElement should generates an unsafe cast warning. class Outer<E> { Inner inner; class Inner { E e; E getOtherElement(Object other) { if (!(other instanceof Outer.Inner)) { throw new IllegalArgumentException(String.valueOf(other)); } Inner that = (Inner) other; return that.e; } } public static void main(String[] args) { Outer<String> s = new Outer<String>(); s.inner = s.new Inner(); s.inner.e = "hello"; Outer<Integer> i = new Outer<Integer>(); i.inner = i.new Inner(); i.inner.e = 1234; String producesClassCast = s.inner.getOtherElement(i.inner); } } More information: javac 1.6 doesn't raise this warning too, but javac 1.7 does. Rémi
Is this really an unsafe cast ? Does the instanceof check above the cast, not mean its 'safe' ?
(In reply to comment #1) > Is this really an unsafe cast ? > > Does the instanceof check above the cast, not mean its 'safe' ? Hi Kent, No, JLS says that a cast is safe if it's a reified type, i.e a type that the VM knows. Inner is equivalent to Outer<E>.Inner, so it's not a reified type so an unsafe cast warning is required. BTW, here instanceof is done on a raw type, it should be written: if (!(other instanceof Outer<?>.Inner)) Because generics aren't reified in Java, the VM will not check the type argument of E. Rémi
Created attachment 148682 [details] Proposed patch and testcase
Released in HEAD for 3.6M3
Created attachment 148685 [details] Proposed patch and testcase Actually prefer this change instead
Released latest patch in HEAD for 3.6M3
Verified for 3.6M3 using build I20091025-2000