Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

Generally speaking, the Java documents strongly discourage us from serializing a lambda expression. That's because the lambda expression will generate synthetic constructs. And, these synthetic constructs suffer several potential problems: no corresponding construct in the source code, variation among different Java compiler implementations, and compatibility issues with a different JRE implementation. However, sometimes, serializing a lambda is necessary.

In this tutorial, we're going to explain how to serialize a lambda expression and its underlying mechanism.

2. Lambda And Serialization

When we use Java Serialization to serialize or deserialize an object, its class and non-static fields must be all serializable. Otherwise, it will lead to NotSerializableException. Likewise, when serializing a lambda expression, we must make sure its target type and capturing arguments are serializable.

2.1. A Failed Lambda Serialization

In the source file, let's use the Runnable interface to construct a lambda expression:

public class NotSerializableLambdaExpression {
    public static Object getLambdaExpressionObject() {
        Runnable r = () -> System.out.println("please serialize this message");
        return r;
    }
}

When trying to serialize the Runnable object, we'll get a NotSerializableException. Before going on, let's explain it a little bit.

When the JVM encounters a lambda expression, it will use the built-in ASM to build an inner class. So, what does this inner class look like? We can dump this generated inner class by specifying the jdk.internal.lambda.dumpProxyClasses property on the command line:

-Djdk.internal.lambda.dumpProxyClasses=<dump directory>

Be careful here: When we replace the <dump directory> with our target directory, this target directory had better be empty because the JVM may dump quite a few unexpected generated inner classes if our project depends on third-party libraries.

After dumping, we can inspect this generated inner class with an appropriate Java decompiler:

In the above picture, the generated inner class only implements the Runnable interface, which is the lambda expression's target type. Also, in the run method, the code will invoke the NotSerializableLambdaExpression.lambda$getLambdaExpressionObject$0 method, which is generated by the Java compiler and represents our lambda expression implementation.

Because this generated inner class is our lambda expression's actual class and it doesn't implement the Serializable interface, the lambda expression isn't suitable for serialization.

2.2. How to Serialize Lambda

At this point, the problem falls to the point: how to add the Serializable interface to the generated inner class? The answer is casting a lambda expression with an intersection type that combines the functional interface and the Serializable interface.

For example, let's combine the Runnable and Serializable into an intersection type:

Runnable r = (Runnable & Serializable) () -> System.out.println("please serialize this message");

Now, if we try to serialize the above Runnable object, it will succeed.

However, if we do this often, it can introduce a lot of boilerplate. To make the code clean, we can define a new interface that implements both Runnable and Serializable:

interface SerializableRunnable extends Runnable, Serializable {
}

Then we can use it:

SerializableRunnable obj = () -> System.out.println("please serialize this message");

But we should also be careful not to capture any non-serializable arguments. For example, let's define another interface:

interface SerializableConsumer<T> extends Consumer<T>, Serializable {
}

Then we may select the System.out::println as its implementation:

SerializableConsumer<String> obj = System.out::println;

As a result, it will lead to a NotSerializableException. That's because this implementation will capture as its argument the System.out variable, whose class is PrintStream, which is not serializable.

3. The Underlying Mechanism

At this point, we may be wondering: What happens underneath after we introduce an intersection type?

To have a basis for discussion, let's prepare another piece of code:

public class SerializableLambdaExpression {
    public static Object getLambdaExpressionObject() {
        Runnable r = (Runnable & Serializable) () -> System.out.println("please serialize this message");
        return r;
    }
}

3.1. The Compiled Class File

After compiling, we can use the javap to inspect the compiled class:

javap -v -p SerializableLambdaExpression.class

The -v option will print verbose messages, and the -p option will display private methods.

And, we may find that the Java compiler provides a $deserializeLambda$ method, which accepts a SerializedLambda parameter:

For readability, let's decompile the above bytecode into Java code:

The main responsibility of the above $deserializeLambda$ method is to construct an object. First, it checks the SerializedLambda‘s getXXX methods with different parts of the lambda expression details. Then, if all conditions are met, it will invoke the SerializableLambdaExpression::lambda$getLambdaExpressionObject$36ab28bd$1 method reference to create an instance. Otherwise, it will throw an IllegalArgumentException.

3.2. The Generated Inner Class

Besides inspecting the compiled class file, we also need to inspect the newly generated inner class. So, let's use the jdk.internal.lambda.dumpProxyClasses property to dump the generated inner class:

In the above code, the newly generated inner class implements both the Runnable and Serializable interfaces, which means it's suitable for serialization. And, it also provides an extra writeReplace method. To look inside, this method returns a SerializedLambda instance describing the lambda expression implementation details.

To form a closed loop, there is one more thing missing: the serialized lambda file.

3.3. The Serialized Lambda File

As the serialized lambda file is stored in binary format, we can use a hex tool to check its contents:

In the serialized stream, the hex “AC ED” (“rO0” in Base64) is the stream magic number, and the hex “00 05” is the stream version. But, the remaining data isn't human-readable.

According to the Object Serialization Stream Protocol, the remaining data can be interpreted:

From the above picture, we may notice the serialized lambda file actually contains the SerializedLambda class data. To be specific, it contains 10 fields and corresponding values. And, these fields and values of the SerializedLambda class are bridges between the $deserializeLambda$ method in the compiled class file and the writeReplace method in the generated inner class.

3.4. Putting It All Together

Now, it's time to combine different parts together:

When we use the ObjectOutputStream to serialize a lambda expression, the ObjectOutputStream will find the generated inner class contains a writeReplace method that returns a SerializedLambda instance. Then, the ObjectOutputStream will serialize this SerializedLambda instance instead of the original object.

Next, when we use the ObjectInputStream to deserialize the serialized lambda file, a SerializedLambda instance is created. Then, the ObjectInputStream will use this instance to invoke the readResolve defined in the SerializedLambda class. And, the readResolve method will invoke the $deserializeLambda$ method defined in the capturing class. Finally, we get the deserialized lambda expression.

To summarize, the SerializedLambda class is the key to the lambda serialization process.

4. Conclusion

In this article, we first looked at a failed lambda serialization example and explained why it failed. Then, we introduced how to make a lambda expression serializable. Finally, we explored the underlying mechanism of lambda serialization.

As usual, the source code for this tutorial can be found over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments