0

Is it possible to programatically read class and static method name referred by lambda? For example:

public class Testing {

interface Generic { void call(); }

static public void method1() {
    System.out.println("In method1");
}

static public void method2() {
    System.out.println("In method2");
}

public void test() {
    Generic g1 = () -> Testing.method1();
    Generic g2 = () -> Testing.method2();

    g1.call();
    g2.call();

    // System.out.println("g1 -> " + getLambdaReference(g1))
}

I would like to somehow read that g1 is pointing to Testing.method1 and g2 is pointing to Test.method2.

Lukasz Kujawa
  • 3,026
  • 1
  • 28
  • 43
  • 3
    But `g1` isn't "pointing to `Testing.method1`". What if you had `Generic g1 = () -> { Testing.method1(); Testing.method2(); };`? – Tunaki Aug 10 '16 at 11:32
  • Sure, I'm interested in this specific case and I'm wondering is there any direct or indirect way to try figure it out. – Lukasz Kujawa Aug 10 '16 at 11:35
  • Nope. That's not possible. – aioobe Aug 10 '16 at 11:36
  • 2
    Note that even with a method-reference `Generic g1 = Testing::method1;`, [you couldn't do it anyway](http://stackoverflow.com/a/19845393/1743880). – Tunaki Aug 10 '16 at 11:50
  • `g1` in your case is probably really `Generic$$lambda1` or something close to that - it's really not a reference but an anonymous inner class (_at runtime_) which forwards the call to desired method. So no, you can't get that. – Esko Aug 10 '16 at 12:00

1 Answers1

1

There is no such reference. Those lambda functions are just Lambdas executing some codes.

If those approachs are possible, then how can you deal with

Generics g = ()->1+2;

or any other just expressions?