Community
Participate
Working Groups
Cheetah02 The following program is demonstrating a curious behavior in Javac: public class X { public static void main(String[] args) { X x = new X(); Class<? extends X> c1 = x.getClass(); // OK String s = "hello"; Class<? extends X> c2 = s.getClass(); // KO } } X.java:6: incompatible types found : java.lang.Class<? extends java.lang.String> required: java.lang.Class<? extends X> Class<? extends X> c2 = s.getClass(); ^ 1 error Officially the type of Object.getClass() is: Class<? extends Object>. It seems that the compiler is automatically transforming it into: Class<? extends X> for the invocation of x.getClass(). This is what is exposed by the subsequent invocation with a String... Is Object#getClass() specially handled by the compiler ?
See javac related bug: http://developer.java.sun.com/developer/bugParade/bugs/5004321.html
Added regression tests: GenericTypeTest#test128&129.
Fixed.