Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

java - Get a list of classes lambdas

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 Supplier<Integer> getOne(){
        return () -> 42;
    }

    public Supplier<Integer> getTwo(){
        return () -> 128;
    }

    public Supplier<Integer> getThree(){
         return () -> 3;
    }
}

and then I print it out like so:

    System.out.println(factory.getOne());
    System.out.println(factory.getOne());
    System.out.println(factory.getTwo());
    System.out.println(factory.getThree());

the output will be something like

examples.LambdaFactory$$Lambda$1@4e515669
examples.LambdaFactory$$Lambda$1@4e515669
examples.LambdaFactory$$Lambda$2@1b9e1916
examples.LambdaFactory$$Lambda$3@ba8a1dc

So we can see two thing here. The same lambda called twice gives us the same lambda object (this is not the same as with anon inner classes where we could get a new one every time). We also see that they look like they are being kept in some kind of "Lambda" structure that is part of the class

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...