In Java, you can “capture” a method call on an object as Runnable, as shown in the example.
Later, having access to this Runnable instance, is it possible to actually access the “captured” object and method parameters of the method that is called (if possible, this probably needs to be done using reflection).
For instance:
class SomePrintingClass {
public void print(String myText) {
System.out.println(myText);
}
}
public class HowToAccess {
public static void main(String[] args) throws Exception {
final String myText = "How to access this?";
final SomePrintingClass printer = new SomePrintingClass();
Runnable r = () -> printer.print(myText);
inspect(r);
}
private static void inspect(Runnable runnable) {
}
}
Is it possible in the "verification" method to access (possibly through reflection) "printer" and "myText", which was passed as a parameter?
source
share