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));
Java 11 allows to use var in a lambda expression and it can be used to apply modifiers to local variables.
You can run Java code in AWS Lambda. Lambda provides runtimes for Java that run your code to process events. Your code runs in an Amazon Linux environment that includes AWS credentials from an AWS Identity and Access Management (IAM) role that you manage.
Introduction. Lambda expressions are a new and important feature included in Java SE 8. They provide a clear and concise way to represent one method interface using an expression. Lambda expressions also improve the Collection libraries making it easier to iterate through, filter, and extract data from a Collection .
The existence of a Stackoverflow question is not sufficient to indicate that such a feature is planned, not even that someone is ever considering it.
If you look at the list of JEPs, you will see that there is no such JEP, not even in a draft state, suggesting such a feature.
Also, if you look at the current state of Java 9’s LambdaMetafactory
, you will see that no change has been made to support passing the meta information that would be necessary to generate a runtime class with recorded annotation data.
There seems to be some desire to add plenty of meta-information to what actually should be a small piece of throw-away code, but I doubt that the language designer will follow it. Lambda expressions are meant to define a function which encapsulates behavior, not an alternative way to describe an anonymous class. The long term evolution will rather lead to lambda expression implementations which are even less of an ordinary class.
it's interesting that they do annotate the inner class that "represents" the lambda expression in InnerClassLambdaMetafactory.spinInnerClass
via :
mv.visitAnnotation("Ljava/lang/invoke/LambdaForm$Hidden;", true)
But that is annotating a class obviously, not a lambda per-se.
Annotating a lambda would require changes to the invokedynamic
and implicitly the LambdaMetafactory
as far as I can see - and what would happen when invokedynamic would not create a class for the lambda, but something else, what would happen to those 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