Problems with ElementType.TYPE_USE and reflection
Kasper Nielsen
kasperni at gmail.com
Tue Nov 6 18:48:01 UTC 2018
Hi,
I'm trying to understand exactly how ElementType.TYPE_USE should work.
I'm asking because the following program only prints that s has one 2
annotation.
I believe it should either print 3 or be flagged at compilation time.
public class TestClass {
@X
@Y
@Z
String s;
public static void main(String[] args) throws Exception {
System.out.println(TestClass.class.getDeclaredField("s").getAnnotations().length);
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface X {}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface Y {}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD, ElementType.TYPE_USE })
@interface Z {}
}
ElementType.TYPE_USE alone isn't enough for the annotation information to
be retained at runtime.
/Kasper
More information about the core-libs-dev
mailing list