java - 为什么Java中不能扩展注解?

标签 java inheritance annotations

我不明白为什么Java注释中没有继承,就像Java类一样。我认为这会非常有用。

例如:我想知道给定的注释是否是 validator 。通过继承,我可以反射性地浏览父类(super class)来了解此注释是否扩展了 ValidatorAnnotation 。否则我怎样才能实现这个目标?

那么,有人能给我这个设计决定的理由吗?

最佳答案

关于为什么没有这样设计的原因你可以在 JSR 175 中找到答案设计常见问题解答,其中显示:

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 的一部分进行研究。 ,您甚至可以找到 Mathias Ricken 已经开发的具有此功能的替代编译器.

关于java - 为什么Java中不能扩展注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61706312/

相关文章:

java - 基于命令行参数创建子类对象的最佳方法

Java - 向继承特定接口(interface)的所有类添加功能

java - 使用 JAX-RS 继承

java - 使用 Spring 注解读取文件属性

java - 使用注释处理器创建具有特定注释的类列表

java - Maven 属性文件插件在写入时替换属性文件的全部内容

java - 从java设置xslt隐藏值

java - 如何从 Guava 范围类获取正确的字符串

java - 尝试使用 @RunWith(SpringRunner.class) 时初始化错误

php - Symfony2 ParamConverter 不与 @Annotation 关联(任何文件更改后必须清除缓存)