Get a list of classes lambdas

后端 未结 2 1200
刺人心
刺人心 2020-12-17 06:52

In Java 8 it looks like the lambdas of a class are kept in an array. For example, lets say we have this class:

public class LambdaFactory {
    public Suppli         


        
相关标签:
2条回答
  • 2020-12-17 07:09

    The Java Language Specification states

    At run time, evaluation of a lambda expression is similar to evaluation of a class instance creation expression, insofar as normal completion produces a reference to an object. [...]

    Either a new instance of a class with the properties below is allocated and initialized, or an existing instance of a class with the properties below is referenced.

    [...]

    These rules are meant to offer flexibility to implementations of the Java programming language, in that:

    • A new object need not be allocated on every evaluation.
    • [...]

    As such, it is up to a compiler or run time environment to decide what should be returned when a lambda expression is evaluated.

    My question is, can I get ahold of the lambdas in a class? I don't have any reason to do so, I just like dissecting things

    You can think of a lambda expression as any other class constant, a String, an integer literal, etc. These are constants that appear in the constant pool of a .class file. These are references to objects that are created and exist at run time. There is no way to refer to the actual objects from a class' constant pool.

    In the case of a lambda, it wouldn't be helpful anyway because it might not actually be the same object.

    0 讨论(0)
  • 2020-12-17 07:24

    The lambdas are created by the JRE and the way they are created is controlled by the JRE and might vary between different JRE vendors and might change in future versions.

    If you want to have fun you can create a lambda at runtime which has no corresponding information within the class file:

    import java.lang.invoke.*;
    
    public class ManualLambda {
      public static void main(String[] args) throws Throwable {
        MethodHandles.Lookup me=MethodHandles.lookup();
        MethodType t=MethodType.methodType(void.class);
        MethodType rt=MethodType.methodType(Runnable.class);
        CallSite site = LambdaMetafactory.metafactory(
          me, "run", rt, t, me.findStatic(ManualLambda.class, "sayHello", t), t);
        MethodHandle factory=site.getTarget();
        Runnable r=(Runnable)factory.invoke();
    
        System.out.println("created lambda: "+r);
        r.run();
      }
      private static void sayHello() {
          System.out.println("hello world");
      }
    }
    

    The code above retraces what happens when a lambda is created. But for compile-time (“real”) lambda expressions the entire thing is triggered by a single invokedynamic byte code instruction. The LambdaMetafactory.metafactory(…) method is the bootstrap method which is called when the invokedynamic instruction is executed the first time. The returned CallSite object is permanently associated with the invokedynamic instruction. If the CallSite is a ConstantCallSite and its MethodHandle returns the same lambda object on every execution, the invokedynamic instruction will “produce” the same lambda instance forever.

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