AnnotatedElement.getAnnotation()方法为什么会编译出错呢?

se134789836178 2019-08-14 09:30:29
以下是Effective Java中的一段代码:

static Annotation getAnnotation(AnnotatedElement element, String annotationTypeName) {
Class<?> annotationType = null; // Unbounded type token
try {
annotationType = Class.forName(annotationTypeName);
} catch (Exception ex) {
throw new IllegalArgumentException(ex);
}
return element.getAnnotation(annotationType.asSubclass(Annotation.class));
}

但是在我的eclipse里为什么会编译出错呢?我的jdk版本是1.7

附上代码图片和出错信息:



Bound mismatch: The generic method getAnnotation(Class<T>) of type AnnotatedElement is not applicable for the arguments (Class<capture#13-of ? extends Annotation>).
The inferred type capture#13-of ? extends Annotation is not a valid substitute for the bounded parameter <T extends Annotation>
...全文
377 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
se134789836178 2019-08-16
  • 打赏
  • 举报
回复
问题已解决,是jdk版本的问题,在jdk1.8下编译成功
qybao 2019-08-14
  • 打赏
  • 举报
回复
getAnnotation参数泛型不对
getAnnotation需要的参数是Class<capture#13-of ? extends Annotation>,但你传的参数是Class<T>
你可以强行转换一下试试
return element. getAnnotation((Class<capture#13-of ? extends Annotation>)annotationType.asSubclass(Annotation.class));

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧