java - How can I find the target of a Java8 method reference? -
i want capture calls 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 have function<object, string> is there reflecto-magic let me find service function created method reference? public withclause allowing(function<t,r> f) { object myservicebackagain = findtargetof(function); .... } i know function come these method references, i'm happy down-cast as necessary. this not same question related is possible convert method reference methodhandle? because, start isn't same question, in related area. , if can methodhandle, can't target it. using trick this post can find target. important method below findtarget . turns out, lambdas indeed capture targets, , can access them serializedlambda . however, pretty nasty reflection hack , it's break in future versions. no...