Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: multiple casting with & [duplicate]

How can I elegantly serialize a lambda?

For example, the code below throws a NotSerializableException. How can I fix it without creating a SerializableRunnable "dummy" interface?

public static void main(String[] args) throws Exception {
    File file = Files.createTempFile("lambda", "ser").toFile();
    try (ObjectOutput oo = new ObjectOutputStream(new FileOutputStream(file))) {
        Runnable r = () -> System.out.println("Can I be serialized?");
        oo.writeObject(r);
    }

    try (ObjectInput oi = new ObjectInputStream(new FileInputStream(file))) {
        Runnable  r = (Runnable) oi.readObject();
        r.run();
    }
}
like image 933
assylias Avatar asked Apr 02 '14 10:04

assylias


5 Answers

Java 8 introduces the possibility to cast an object to an intersection of types by adding multiple bounds. In the case of serialization, it is therefore possible to write:

Runnable r = (Runnable & Serializable)() -> System.out.println("Serializable!");

And the lambda automagically becomes serializable.

like image 194
assylias Avatar answered Sep 17 '22 15:09

assylias


The same construction can be used for method references. For example this code:

import java.io.Serializable;

public class Test {
    static Object bar(String s) {
        return "make serializable";
    }

    void m () {
        SAM s1 = (SAM & Serializable) Test::bar;
        SAM s2 = (SAM & Serializable) t -> "make serializable";
    }

    interface SAM {
        Object action(String s);
    }
}

defines a lambda expression and a method reference with a serializable target type.

like image 34
Vicente Romero Avatar answered Sep 21 '22 15:09

Vicente Romero


Very ugly cast. I prefer to define a Serializable extension to the functional interface I'm using

For example:

interface SerializableFunction<T,R> extends Function<T,R>, Serializable {}
interface SerializableConsumer<T> extends Consumer<T>, Serializable {}

then the method accepting the lambda can be defined as such :

private void someFunction(SerializableFunction<String, Object> function) {
   ...
}

and calling the function you can pass your lambda without any ugly cast:

someFunction(arg -> doXYZ(arg));
like image 40
Pascal Avatar answered Sep 17 '22 15:09

Pascal


In case someone falls here while creating Beam/Dataflow code :

Beam has his own SerializableFunction Interface so no need for dummy interface or verbose casts.

like image 42
Rafaël Avatar answered Sep 18 '22 15:09

Rafaël


If you are willing to switch to another serialization framework like Kryo, you can get rid of the multiple bounds or the requirement that the implemented interface must implement Serializable. The approach is to

  1. Modify the InnerClassLambdaMetafactory to always generate the code required for serialization
  2. Directly call the LambdaMetaFactory during deserialization

For details and code see this blog post

like image 26
ruediste Avatar answered Sep 17 '22 15:09

ruediste