为什么是不可能在Java扩展注解? [英] Why is not possible to extend annotations in Java?

查看:490
本文介绍了为什么是不可能在Java扩展注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么在Java注解没有继承,就像Java类。我认为这将是非常有用的。

I don't understand why there is no inheritance in Java annotations, just as Java classes. I think it would be very useful.

例如:我想知道,如果给定的注解是一个验证器。有了继承,我可以通过条件反射超导航知道这个标注的延伸ValidatorAnnotation。否则,我怎么能做到这一点?

For example: I want to know if a given annotation is a validator. With inheritance, I could reflexively navigate through superclasses to know if this annotation extends a ValidatorAnnotation. Otherwise, how can I achieve this?

所以,任何人都可以给我一个理由,为什么这样的设计决策?

So, can anyone give me a reason why this design decision?

推荐答案

关于为什么没有这样设计的,你可以找到的 JSR 175 设计常见问题,在那里说:

About the reason why it wasn't designed that way you can find the answer in the JSR 175 Design FAQ, where it says:

为什么不支持注释子类型(其中一个注释类型扩展其他)?

Why don’t you support annotation subtyping (where one annotation type extends another)?

据注释类型复杂
  系统,并使得它更
  难写专用工具。

It complicates the annotation type system, and makes it much more difficult to write "Specific Tools".

...

专用工具 - 即查询程序
  众所周知注释类型的任意
  外部程序。存根发电机,
  例如,属于这一类。
  这些程序会读取带注释
  类,而要将其装入
  虚拟机,但是将加载
  注释接口。

"Specific Tools" — Programs that query known annotation types of arbitrary external programs. Stub generators, for example, fall into this category. These programs will read annotated classes without loading them into the virtual machine, but will load annotation interfaces.

所以,是的,我想,其原因是它只是吻。无论如何,这似乎这个问题(以及许多其他人)都被看到的 JSR 308 <作为零件/ A>,你甚至可以找到一个替代编译器已经马蒂亚斯肯开发此功能

So, yes I guess, the reason is it just KISS. Anyway, it seems this issue (along with many others) are being looked into as part of JSR 308, and you can even find an alternative compiler with this functionality already developed by Mathias Ricken.

这篇关于为什么是不可能在Java扩展注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆