实现 java.lang.reflect.Proxy 的注释背后的基本原理是什么?
它不是语言规范所要求的,并且似乎是特定于供应商的。
关于注释如何为注释/类的用户工作是否有任何好处或更好的保证?或者只是某些 JDK 类被重用来帮助实现注释,因为它“方便”?
另请参阅这篇 IKVM 博客文章。
It is not required by the language specification and seems to be vendor-specific.
Are there any benefits or better guarantees about how will annotations work for the user of the annotation/class? Or is it just that some JDK class got reused to help implementing annotations because it was "convenient"?
See also this IKVM blog post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注释是接口。同时,它们至少应该具有一些属性,这些属性应该由注释处理器读取。实现这种二元性的最简单方法是在运行时动态代理注释(或者可能是类加载时间,不确定)。
Annotations are interfaces. At the same time, they supposed to have some properties at least, which should be read by annotation processors. The easiest way to achieve this duality is to dynamically proxify the annotations at runtime (or may be classloading time, not sure).
另一种方法是为每个注释类型编译(或以其他方式生成)一个新的实现类,或者重新发明另一个类似代理的机制。
由于实现非常简单(并且并非真正特定于每种注释类型),因此将 Proxy 对象与通用 InvocableHandler 实现一起使用是一个非常好的主意。
The alternative would be to compile (or somehow else generate) a new implementation class for each annotation type, or reinvent another Proxy-like mechanism.
Since the implementation is quite simple (and not really specific to each annotation type), using Proxy objects together with a generic InvocationHandler implementation is a quite good idea.