user5182503
user5182503

Reputation:

Is it possible to generate lambda without generating class using byte buddy?

In this article they say:

Anonymous compiles to a class, while lambda is an invokedynamic instruction.

As I understand when we create lambda in java then this lambda compiler doesn't create a separate class. If it is correct, is it possible to create an instance of lambda without generating a class in byte buddy? If yes, could anyone give example of how to do it for this example:

Function<Integer, Integer> f = (x) -> { return x * x; };

Upvotes: 1

Views: 191

Answers (1)

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

I have written an extensive description of invokedynamic. In essence: The JVM writes the lambda body to a private method. At runtime, it dispatches the dyanmic call to the JVM's lambda meta factory that creates a class on the fly that implements the functional interface. This might however change in the future.

Byte Buddy allows you to create invokedynamic call sites that point to the same lambda metafactory, but they will apply the same mechanism.

Upvotes: 1

Related Questions