Serialize your lambdas for a rainy day - save to file
Introduction A short post describing how a Java lambda can be persisted to a file for re-use in a different process. I then discuss how we use this technique in Fluxtion. Serialising Lambdas Lambdas, introduced in Java 8 make functions first class citizens(nearly) in the Java language. They remove the need for a dedicated class to hold a function. But how does this work under the covers? In reality javac hijacks the class containing the function adding a static method that contains the implementation of the lambda. The lambda call site is replaced with an invocation to the newly added static method. For a full description of the lambda implementation magic see this article . Oracle helpfully provides the SerializableLambda class that implements the serializable form of a lambda, providing enough meta information to reconstruct the call. All we have to do is cast the lambda to a Serializable and then use the standard java machinery to marshal the lambda. Below are a couple of...