This question is now over 3 years old and specifically addressed Java 8, with the accepted answer also citing the Java SE 8 Final Specification.
I would be interested if something regarding this question will change in Java 9: Is there any way to annotate a lambda expression similar to annotating a corresponding anonymous class?
Example:
Annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
public @interface MyTypeAnnotation {
public String value();
}
Working annotation of anonymous class:
Consumer<String> consumer = new @MyTypeAnnotation("Hello ") Consumer<String>() {
@Override
public void accept(String str) {
System.out.println(str);
}
};
Annotating a lamba expression, currently not working in Java 8:
Consumer<String> myAnnotatedConsumer = @MyTypeAnnotation("Hello") (p -> System.out.println(p));