Java 8 - how to access object and method encapsulated as lambda

前端 未结 3 1532
情歌与酒
情歌与酒 2021-01-02 20:42

In Java you can \"capture\" a \"method call on object\" as a Runnable, as in belows example.

Later, having access to this instance of Runnable, is it possible to act

相关标签:
3条回答
  • 2021-01-02 21:03

    Check if you want something as the below code where I have passed the runnable to a Thread object in your inspect method.

        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); // capture as Runnable
    
                inspect(r);
              }
    
    
              private static void inspect(Runnable runnable) {
                Thread t = new Thread(runnable);
                t.start();
    
              }
    
    
            }
    

    output will be:

    How to access this?

    0 讨论(0)
  • 2021-01-02 21:14

    It is possible, because the captured references are translated into fields of the runnable (as with all anonymous classes). The names will be not be consistent however.

    I found by testing that you need to make myText non-final, otherwise it will be seen as a compile time constant and in-lined (and will not be accessible as a field):

    private static void inspect(Runnable runnable) throws Exception {       
        for(Field f : runnable.getClass().getDeclaredFields()) {
            f.setAccessible(true);
            System.out.println("name: " + f.getName());
            Object o = f.get(runnable);
            System.out.println("value: " + o);
            System.out.println("class: " + o.getClass());
            System.out.println();
        }
    }
    

    Prints:

    name: arg$1
    value: test.SomePrintingClass@1fb3ebeb
    class: class test.SomePrintingClass
    
    name: arg$2
    value: How to access this?
    class: class java.lang.String
    
    0 讨论(0)
  • 2021-01-02 21:15

    With reflection, it is not possible to get local variables and method parameter values. Instead you can use AOP to intercept the method call and inspect the parameters.

    0 讨论(0)
提交回复
热议问题