I don't get the behaviour of following code:
https://gist.github.com/tomaszalusky/3e3777b4fd0c6096f3f707bb19b50b52 - see embedded:
import java.lang.reflect.*;
import java.util.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class AnnotationOnTypeArgument {
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD,ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Anno {
}
interface Nested<T> {
}
Toplevel<@Anno Integer> toplevel;
Nested<@Anno Integer> nested;
public static void main(String[] args) throws Exception {
print(AnnotationOnTypeArgument.class.getDeclaredField("toplevel"));
print(AnnotationOnTypeArgument.class.getDeclaredField("nested"));
}
private static void print(Field field) {
AnnotatedType annotatedType = field.getAnnotatedType();
AnnotatedParameterizedType annotatedParameterizedType = (AnnotatedParameterizedType)annotatedType;
ParameterizedType parameterizedType = (ParameterizedType)annotatedParameterizedType.getType();
AnnotatedType argType = annotatedParameterizedType.getAnnotatedActualTypeArguments()[0];
System.out.printf("field %s%ntype=%s%nannotatedType=%s%nannotations=%s%ntype=%s%n%n",
field.getName(), parameterizedType, argType, Arrays.asList(argType.getDeclaredAnnotations()), argType.getType());
}
}
interface Toplevel<T> {
}
EDIT: the actual result is:
field toplevel
type=Toplevel<java.lang.Integer>
annotatedType=sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@1540e19d
annotations=[@AnnotationOnTypeArgument$Anno()]
type=class java.lang.Integer
field nested
type=AnnotationOnTypeArgument.AnnotationOnTypeArgument$Nested<java.lang.Integer>
annotatedType=sun.reflect.annotation.AnnotatedTypeFactory$AnnotatedTypeBaseImpl@677327b6
annotations=[]
type=class java.lang.Integer
Why the array of declared annotations on type argument is empty when surrounding type is nested? I expect to have one element just as for top level type. I would appreciate any explanation based on JLS.
Happens consistently on JDK8u101 (http://compilejava.net), older JDK8 and Eclipse.
Thanks!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…