vendredi 17 mai 2019

How can I find out which method a lambda applies to?

If a method in a library is called with a Java Lambda expression, these are often just wrapped method calls. Is it possible to find out what method that originally was, just for logging purposes?

class Foo {
    private void doSomething() { ... }

    public void doSomethingInTransaction() {
        doInTransaction(this::doSomething);
    }

    private doInTransaction(Runnable run) { ... } 
}

When calling doSomethingInTransaction() the method doInTransaction is actually called with an Object of type Runnable. It would sometimes be nice to log the name and class of the method that was passed here (that is, Foo.doSomething). Is it somehow possible to find out what that was via reflection or whatnot? If that requires specific Java versions, that'd be an interesting answer, too.





Aucun commentaire:

Enregistrer un commentaire