Community
Participate
Working Groups
Using latest (3.3M3), the compiler reports an error compiling this code: [ import java.lang.annotation.Target; import java.lang.annotation.Retention; import static java.lang.annotation.RetentionPolicy.*; import static java.lang.annotation.ElementType.*; import java.lang.reflect.*; @Target(CONSTRUCTOR) @Retention(RUNTIME) @interface Annot { String value(); } public class X { @Annot("SUCCESS") public X() { } static String getAnnotationValue(Constructor constructor) { Annot annotation = constructor.getAnnotation(Annot.class); return (annotation != null) ? annotation.value() : null; } static void displayConstructorAnnotationValues(Class type) { for (Constructor constructor : type.getConstructors()) { System.out.println(getAnnotationValue(constructor)); } } public static void main(String[] args) { displayConstructorAnnotationValues(X.class); } } ] ---------- 1. ERROR in D:\tests_sources\X.java (at line 19) Annot annotation = constructor.getAnnotation(Annot.class); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type mismatch: cannot convert from Annotation to Annot ---------- 1 problem (1 error) javac 1.5.0_09 and 1.6b104 compile it fine and at runtime returns: "SUCCESS"
This looks like the returned type for: constructor.getAnnotation(Annot.class) is Annotation instead of Annot.
Feels like raw conversion would be more selective by not altering all type parameters (only those carried by the generic type being converted, not params from a generic method).
I take back my previous comment. Nothing seems to have changed since both javac 1.5 and 1.6 agree with us in rejecting the following: public class X<T> { <T> T foo(Class<T> c) { return null; } static <T> T staticFoo(Class<T> c) { return null; } void bar() { X x = null; x = x.foo(X.class); // rawified x = staticFoo(X.class); } }
Also see bug 106744
*** This bug has been marked as a duplicate of bug 106744 ***