By darcy on Feb 21, 2013
As part of Project Lambda, after discussion with the JSR 335 expert group, we decided to add a
FunctionalInterface annotation type to the platform.
To a first approximation, functional interfaces are those interface types which define only a single method and are therefore usable in lambda expressions.
(There are some tricky details in the definition of a functional interface relating to generics and also some details about excluding from consideration methods defined on
The new annotation type allows a library designer to clearly indicate the property of intending an interface type to be used with lambda expressions along with an implied commitment to keep that property true in the future.
However, the compiler will allow any interface type meeting the structural properties of a functional interface to be used for a lambda expression regardless of whether or not the interface has a
They types being added in the
java.util.function package are by design functional interfaces and can be annotated with
@FunctionalInterface from early days.
However, many existing Java SE types are also functional interfaces and we want to identify and annotate those appropriately too.
To find those candidate interfaces to be annotated, I ran an annotation processor over the code, using the same methodology as used to find
Closeable candidates during JDK 7.
A significant number of candidates were found throughout the JDK. After suitable discussion and review, the first batch of core libraries changes have been pushed. Analogous discussions have been started in the 2D, awt, and swing areas.
For guidance in retrofitting
@FunctionalInterface to an existing candidate type, if the type is routinely instantiated using an anonymous class, it is a good candidate for being annotated with