7

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!

0

2 Answers 2

6

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()]
3
  • Rafael, you mean the bug had not been known before? And could you please specify URL of reported bug? Oracle bug database search engine seems somewhat unhealthy at the moment. Oct 13, 2016 at 20:06
  • 1
    I was trying to find it but I could not. I reported it via the web form so my name is not on the issue. I have reported about 20 bugs that relate to type annotations so its nontrivial, sorry. They are quite buggy, unfortunately. This report should be about one year old by now. Oct 13, 2016 at 21:51
  • 1
    Hi Rafael, can you please try to find the bug report again? I am trying to figure out if I should file a report for a similar issue.
    – Tillerino
    Mar 9, 2017 at 15:41
5

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

1
  • 2
    I think, we can trace it back to AnnotatedTypeFactory, line 105 where nested parametrized types are unconditionally treated as (non-static) inner classes (unlike reifiable types, seven lines earlier). In fact, asking a ParametertrizedType whether it is static, is not straight-forward…
    – Holger
    Oct 10, 2016 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.