加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何在泛型边界上获取TYPE_USE注释

发布时间:2020-12-15 01:08:26 所属栏目:Java 来源:网络整理
导读:我有这个案子: public class SomeClass 而我正试图在绑定的T.上获得@ A2注释 这就是我所看到的,假设myMethod是SomeClass.class.getDeclaredMethod(“myMethod”).为了便于阅读,删除了类型转换. myMethod.getGenericReturnType().getAnnotations()返回@ A1(

我有这个案子:

public class SomeClass

而我正试图在绑定的T.上获得@ A2注释
这就是我所看到的,假设myMethod是SomeClass.class.getDeclaredMethod(“myMethod”).为了便于阅读,删除了类型转换.

> myMethod.getGenericReturnType().getAnnotations()返回@ A1(正如预期的那样,我猜)
> myMethod.getGenericReturnType().getBounds()[0] .getAnnotations()什么都不返回(??不应该是@ A2 ??)
> myMethod.getAnnotatedReturnType().getAnnotations()返回@ A3(我猜是正如预期的那样)
> myMethod.getAnnotatedReturnType().getAnnotatedBounds()[0] .getAnnotations()不返回任何内容(我猜是例外)

好像@ A2迷路了……这听起来不合理.我做错了什么或这确实是一些奇怪的行为?

更新:
它确实是一个JDK错误,我报告了它,它被接受为JDK-8191466

最佳答案
除了TypeVariable之外,可以通过AnnotatedType层次结构检索所有类型注释,其注释可以通过AnnotatedTypeVariable.getType()检索,AnnotatedTypeVariable.getType()将是TypeVariable的实例,它现在扩展AnnotatedElement并且恰好与Method返回的对象相同. getGenericReturnType().

因此,您可以检索所有信息,例如

AnnotatedType art = myMethod.getAnnotatedReturnType();
System.out.print(
    Arrays.toString(art.getAnnotations())+" "+art.getType().getTypeName()+" -> ");
final boolean typeVariable = art instanceof AnnotatedTypeVariable;
if(typeVariable) System.out.print('<');
System.out.print(Arrays.toString(((AnnotatedElement)art.getType()).getAnnotations())+" ");
System.out.print(art.getType().getTypeName());
if(typeVariable) {
    AnnotatedTypeVariable atv = (AnnotatedTypeVariable)art;
    AnnotatedType[] annotatedBounds = atv.getAnnotatedBounds();
    if(annotatedBounds.length>0) {
        System.out.print(" extends ");
        for(AnnotatedType aBound: annotatedBounds) {
            System.out.print(Arrays.toString(aBound.getAnnotations())+" ");
            System.out.print(aBound.getType().getTypeName()+",");
        }
    }
    System.out.println(">");
}

将打印

[@A3()] S -> <[@A1()] S extends [@A2()] T,>

当您调用((AnnotatedTypeVariable)myMethod.getAnnotatedReturnType()).getAnnotatedBounds()[0] .getAnnotations()不提供@A2时,您应该重新检查A2是否真正具有@Retention(RetentionPolicy.RUNTIME)@Target(ElementType. TYPE_USE).

当然,这是仅处理您特定方法的“简化”版本.

要处理所有可能的情况,您将需要更多的instanceofs,处理Type和AnnotatedType的这些析取类型层次结构,尤其是在处理带注释的参数化类型和带注释的通用数组类型时.

如上所述,TypeVariable是不同的,因为它扩展了AnnotatedElement并且还具有getAnnotatedBounds()方法.因此,在这种特定情况下,处理该方法的另一种方法是

List

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读