为什么泛型类型参数上的注释对于嵌套类型不可见



我不明白以下代码的行为:https://gist.github.com/tomaszalusky/3e3777b4fd0c6096f3f707bb19b50b52 -参见嵌入:

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> {
}

编辑:实际结果是:

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

为什么当周围的类型嵌套时,类型参数上声明的注释数组是空的?我希望有一个元素,就像顶级类型一样。

如果有任何基于JLS的解释,我将不胜感激。

在JDK8u101 (http://compilejava.net),旧的JDK8和Eclipse上一致发生。

谢谢!

这是OpenJDK中的一个错误,我已经报告了这个问题,我希望它能被修复。类型注释在实践中并没有真正使用得那么多,它似乎并不是一个优先事项。正如Holger所提到的,这是AnnotatedTypeFactory实现中的一个混淆。

你可以使用Byte Buddy来正确解析类文件元数据:

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());
}

这将给出您期望的输出:

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()]

我花了一些时间进行调试,annotatedParameterizedType变量似乎仍然在其allOnSameTargetTypeAnnotations字段中包含对Anno注释的引用。

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

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 

但是位置深度有所不同,AnnotatedTypeFactory的getnotatedactualtypearguments()方法包含一个TypeAnnotation.isSameLocationInfo()比较,作为向注释映射添加元素的先决条件,在嵌套情况下该比较将变为false,因此最终没有添加元素

我也没有找到任何关于它的文档。也许你在这里发现了一个问题

最新更新