当类型是嵌套的通用接口时,TYPE_USE注释会丢失

Til*_*ino 10 java reflection annotations

当带注释的类型是嵌套的通用接口时,似乎无法通过反射访问TYPE_USE注释.

请注意以下示例:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;

public class LostAnnotation {
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.TYPE_USE)
  public @interface SomeTypeAnnotation {
  }

  @SomeTypeAnnotation Map<String, String> map;
  @SomeTypeAnnotation Entry<String, String> entry;

  public static @SomeTypeAnnotation Entry<String, String> someMethod(
      @SomeTypeAnnotation Map<String, String> map,
      @SomeTypeAnnotation Entry<String, String> entry) {
    return null;
  }

  public static void main(String[] args) throws Exception {
    Class<LostAnnotation> clazz = LostAnnotation.class;
    Method method = clazz.getMethod("someMethod", Map.class, Entry.class);
    AnnotatedType[] types = method.getAnnotatedParameterTypes();

    print("map field", clazz.getDeclaredField("map").getAnnotatedType());
    print("map parameter", types[0]);

    print("entry field", clazz.getDeclaredField("entry").getAnnotatedType());
    print("entry parameter", types[1]);
    print("entry return type", method.getAnnotatedReturnType());
  }

  static void print(String title, AnnotatedType type) {
    System.out.printf("%s: %s%n", title, Arrays.asList(type.getAnnotations()));
  }
}
Run Code Online (Sandbox Code Playgroud)

上述代码的预期输出

map field: [@LostAnnotation$SomeTypeAnnotation()]
map parameter: [@LostAnnotation$SomeTypeAnnotation()]
entry field: [@LostAnnotation$SomeTypeAnnotation()]
entry parameter: [@LostAnnotation$SomeTypeAnnotation()]
entry return type: [@LostAnnotation$SomeTypeAnnotation()]
Run Code Online (Sandbox Code Playgroud)

但是,上面代码的实际输出

map field: [@LostAnnotation$SomeTypeAnnotation()]
map parameter: [@LostAnnotation$SomeTypeAnnotation()]
entry field: []
entry parameter: []
entry return type: []
Run Code Online (Sandbox Code Playgroud)

Map界面的每次使用中正确检索注释.但是,在Entry接口的每次使用时,无论是字段,返回类型还是参数,注释都会丢失.我有这种情况的唯一解释是,Entry接口被嵌套在Map界面.

我在win64上的最新oracle JDK(8u121)上运行了上面的例子.我做错了什么或者这可能是个错误吗?

我的Annotation嵌套是为了便于阅读.使其成为顶级界面不会改变任何东西.

rdl*_*pes 5

这已经在SO中被发现:为什么嵌套类型的泛型类型参数的注释不可见?

答案是已提交错误,但优先级较低,因为这些情况不会经常出现.

不过,您可以使用ByteBuddy来解析字节码并检索带注释的类型.

根据我的经验,ASM也可以工作,我怀疑任何字节码解析器都可以解决这个bug.