My initial question was an exact duplicate of this one; that is, why is it that this interface has a runtime retention policy.
But the accepted answer does not satisfy me at all, for two reasons:
@Documented
has (I believe) nothing to do with it (although why @Documented
has a runtime retention policy is a mystery to me as well);Comparable
as the answer mentions, but also Runnable
etc), this does not prevent them from being used as "substitutes" (for instance, you can perfecty well use a DirectoryStream.Filter
as a substitute to a Predicate
if all you do is filter on Path
, for instance).But still, it has this retention. Which means that it has to influence the JVM behavior somehow. How?
These functional interfaces allow you to the passcode to a function in form of lambda expression and enable the creation of powerful methods which can operate on those codes like the filter() method which accepts a Predicate and allows you to pass a code that accepts one argument and return boolean.
Functional interfaces are included in Java SE 8 with Lambda expressions and Method references in order to make code more readable, clean, and straightforward. Functional interfaces are interfaces that ensure that they include precisely only one abstract method.
A functional interface is a special kind of interface with exactly one abstract method in which lambda expression parameters and return types are matched. It provides target types for lambda expressions and method references.
An interface which has Single Abstract Method can be called as Functional Interface. Runnable, Comparator,Cloneable are some of the examples for Functional Interface.
I've found the thread in core-libs-dev mailing list which discusses the retention of @FunctionalInterface
annotation. The main point mentioned here is to allow third-party tools to use this information for code analysis/validation and to allow non-Java JVM languages to map correctly their lambdas to functional interfaces. Some excerpts:
Joe Darcy (original committer of @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.
So seems that it's not used by JVM itself, it's just an additional possibility for third-party tools. Making the annotation runtime-visible is not a big cost, so seems there were no strong reasons not to do this.
The only requirement for annotations with retention policy runtime is
Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively. (https://docs.oracle.com/javase/7/docs/api/java/lang/annotation/RetentionPolicy.html#RUNTIME)
Now this has some consequences on runtime behaviour, since the class loader must load these annoations and the VM must keep these annotations in memory for reflective access (for example by third party libraries).
There is however no requirement for the VM to act on such annotations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With