Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does @FunctionalInterface influence the JVM's runtime behavior?

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:

  • the fact that this interface is @Documented has (I believe) nothing to do with it (although why @Documented has a runtime retention policy is a mystery to me as well);
  • even though many "would be" functional interfaces existed in Java prior to Java 8 (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?

like image 241
fge Avatar asked Feb 19 '16 02:02

fge


People also ask

What is the use of functional interface in real time?

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.

What are the benefits of functional interfaces?

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.

What is the purpose of functional interface?

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.

Is cloneable a functional interface?

An interface which has Single Abstract Method can be called as Functional Interface. Runnable, Comparator,Cloneable are some of the examples for Functional Interface.


2 Answers

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.

like image 175
Tagir Valeev Avatar answered Nov 14 '22 22:11

Tagir Valeev


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.

like image 32
Thomas Kläger Avatar answered Nov 15 '22 00:11

Thomas Kläger