jeudi 2 juillet 2015

How can I find the target of a Java8 method reference?

I want to capture calls to a mock object

public interface Service {
    public String stringify(Object o);
}
service = mockery.mock(Service.class);
mockery.allowing(service::stringify).with(42).will(() -> "42");

So inside allowing I have a Function<Object, String>

Is there any reflecto-magic that will let me find the service from the function created from the method reference?

public WithClause allowing(Function<T,R> f) {
    Object myServiceBackAgain = findTargetOf(function);
    ....
}

I know that the Function will always come from this method references, so I'm happy to down-cast as much as is necessary.





Aucun commentaire:

Enregistrer un commentaire