作者:jerry827900 | 来源:互联网 | 2023-06-03 20:19
我最初的问题是thisone的完全重复;也就是说,为什么这个接口有一个运行时保留策略.但是接受的答案根本不能满足我,原因有两个:这个接口是@Documented的事实(我相信)与
我最初的问题是this one的完全重复;也就是说,为什么这个接口有一个运行时保留策略.
但是接受的答案根本不能满足我,原因有两个:
>这个接口是@Documented的事实(我相信)与它无关(尽管为什么@Documented有一个运行时保留策略对我来说也是个谜);
>尽管在Java 8之前Java中存在许多“将是”功能接口(可比较提及,但Runnable等),这并不妨碍它们被用作“替代品”(例如,你可以很好地完善它们)如果您所做的只是在Path上过滤,则使用DirectoryStream.Filter作为Predicate的替代.
但是,它仍有这种保留.这意味着它必须以某种方式影响JVM行为.怎么样?
解决方法:
我在core-libs-dev邮件列表中找到了thread,它列出了@FunctionalInterface注释的保留.这里提到的要点是允许第三方工具使用此信息进行代码分析/验证,并允许非Java JVM语言将其lambda正确映射到功能接口.一些摘录:
Joe Darcy(@FunctionalInterface的原始提交者):
We intentionally made this annotation have runtime retention to
allow it to also be queried to various tools at runtime, etc.
Brian Goetz
There is a benefit for languages other than Java, that can use this as a means to determine whether the interface is suitable for passing to the SAM conversion machinery. The JDK support for lambda conversion is available to other languages as well.
所以似乎JVM本身并没有使用它,它只是第三方工具的另一种可能性.使注释运行时可见并不是一个很大的成本,所以似乎没有强有力的理由不这样做.