Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Annotated" lambda expression instead of interface with single apply method [duplicate]

instead of using anonymous classes like so

register(new EventListener() {
  @Override
  public void apply(Event e) {
    // do your work
  }
});

with java 8 I can use lambda expressions:

register(e -> (// do your work));

But what if the method in my interface is annotated?

interface EventListener {
  @Annotation
  void apply;
}

Is it possible to annotate a lambda expression? (Specifically, I want to use Guava's EventBus.register() method with lambda expressions)

like image 628
S1lentSt0rm Avatar asked May 31 '14 10:05

S1lentSt0rm


1 Answers

See this:

Annotating the functional interface of a Lambda Expression

Basically, you can't directly annotate in a lambda expression, but you can use the extending interface or class and access the annotation by calling Class#getAnnotatedInterfaces(). I believe that should answer your question.

Hope this helps,

Santiago

like image 106
Santiago Benoit Avatar answered Oct 29 '22 14:10

Santiago Benoit