Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-dev] invokedynamic and AspectJ

Something that came up on stack overflow, I’m interested in opinions on this…

If you move from an inline anonymous class usage to a lambda, you lose the ability to advise execution of the method in that anonymous class:

aspect X {
  before(): execution(* Runnable.run(..)) {
    System.out.println(">>");
  }
}

public class Code {
  public static void main(String[] argv) {
    foo();
    bar();
  }

  public static void foo() {
    Runnable r = new Runnable() {
        public void run() {
          System.out.println("a");
        }
    };
    r.run();
  }

  public static void bar() {
    Runnable r = () -> { System.out.println("b"); };
    r.run();
  }
}

The pointcut only matches the long winded new Runnable() {…} and not the lambda.

This is because of the invokedynamic that is used to implement the lambda usage, it causes the ‘anonymous class’ to be generated at runtime, so the AspectJ compile time weaver will never see it in order to inject advice.  How should invokedynamic manifest as a joinpoint? I have some ideas but, as I say, I’m interested in opinions.

cheers,
Andy

Back to the top