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!
This is a bug in the OpenJDK, I have reported this a while ago and I hope it to be fixed. Type annotations are not really used that much in practice and it does not seem a priority. As Holger mentioned, this is a mixup in the implementation of the AnnotatedTypeFactory
.
You can use Byte Buddy which parses the class file meta data correctly:
public static void main(String[] args) throws Exception {
TypeDescription type = TypePool.Default.ofClassPath()
.describe(AnnotationOnTypeArgument.class.getName())
.resolve();
print(type.getDeclaredFields().filter(named("toplevel")).getOnly());
print(type.getDeclaredFields().filter(named("nested")).getOnly());
}
private static void print(FieldDescription field) {
System.out.printf("field %s%ntype=%s%nannotations=%s%ntype=%s%n%n",
field.getName(),
field.getType(),
field.getType().getTypeArguments().get(0),
field.getType().getTypeArguments().get(0).getDeclaredAnnotations());
}
This gives you the expected output:
field toplevel
type=net.bytebuddy.Toplevel<java.lang.Integer>
annotations=class java.lang.Integer
type=[@net.bytebuddy.AnnotationOnTypeArgument$Anno()]
field nested
type=net.bytebuddy.AnnotationOnTypeArgument.net.bytebuddy.AnnotationOnTypeArgument$Nested<java.lang.Integer>
annotations=class java.lang.Integer
type=[@net.bytebuddy.AnnotationOnTypeArgument$Anno()]
I gave some time to debugging and the annotatedParameterizedType variable seems to still contain the reference to the Anno annotation in its allOnSameTargetTypeAnnotations field for the nested case too
annotatedParameterizedType = {AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@539}
type = {ParameterizedTypeImpl@540} "com.sample.Toplevel<java.lang.Integer>"
decl = {Field@538} "com.sample.Toplevel com.sample.AnnotationOnTypeArgument.toplevel"
location = {TypeAnnotation$LocationInfo@543}
depth = 0
locations = {TypeAnnotation$LocationInfo$Location[0]@549}
allOnSameTargetTypeAnnotations = {TypeAnnotation[1]@544}
0 = {TypeAnnotation@551} "@com.sample.AnnotationOnTypeArgument$Anno() with Targetnfo: FIELD: -2, -2 on base declaration: com.sample.Toplevel com.sample.AnnotationOnTypeArgument.toplevel"
annotations = {LinkedHashMap@546} size = 0
vs
annotatedParameterizedType = {AnnotatedTypeFactory$AnnotatedParameterizedTypeImpl@602}
type = {ParameterizedTypeImpl@603} "com.sample.AnnotationOnTypeArgument.com.sample.AnnotationOnTypeArgument$Nested<java.lang.Integer>"
decl = {Field@601} "com.sample.AnnotationOnTypeArgument$Nested com.sample.AnnotationOnTypeArgument.nested"
location = {TypeAnnotation$LocationInfo@606}
depth = 1
locations = {TypeAnnotation$LocationInfo$Location[1]@611}
allOnSameTargetTypeAnnotations = {TypeAnnotation[1]@607}
0 = {TypeAnnotation@612} "@com.sample.AnnotationOnTypeArgument$Anno() with Targetnfo: FIELD: -2, -2 on base declaration: com.sample.AnnotationOnTypeArgument$Nested com.sample.AnnotationOnTypeArgument.nested"
annotations = {LinkedHashMap@608} size = 0
However there is a difference in the location depth and the upcoming getAnnotatedActualTypeArguments() method of AnnotatedTypeFactory contains a TypeAnnotation.isSameLocationInfo() comparison as a precondition to add elements to the annotations map, which will become false in the nested case, thus eventually no element is being added
I did not find no documentation on it either. Perhaps you have found an issue here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With