-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for serialization of Java8 closures (see #215).
- Loading branch information
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/com/esotericsoftware/kryo/serializers/ClosureSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.esotericsoftware.kryo.serializers; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
import com.esotericsoftware.kryo.KryoException; | ||
import com.esotericsoftware.kryo.Serializer; | ||
import com.esotericsoftware.kryo.io.Input; | ||
import com.esotericsoftware.kryo.io.Output; | ||
|
||
/** Serializer for Java8 closures. | ||
* @author Roman Levenstein <[email protected]> */ | ||
public class ClosureSerializer extends Serializer { | ||
|
||
private static Method readResolve; | ||
private static Class serializedLambda; | ||
static { | ||
try { | ||
serializedLambda = Class.forName("java.lang.invoke.SerializedLambda"); | ||
readResolve = serializedLambda.getDeclaredMethod("readResolve"); | ||
readResolve.setAccessible(true); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not obtain SerializedLambda or its methods via reflection", e); | ||
} | ||
} | ||
|
||
public ClosureSerializer () { | ||
} | ||
|
||
public void write (Kryo kryo, Output output, Object object) { | ||
try { | ||
Class type = object.getClass(); | ||
Method writeReplace = type.getDeclaredMethod("writeReplace"); | ||
writeReplace.setAccessible(true); | ||
Object replacement = writeReplace.invoke(object); | ||
if (serializedLambda.isInstance(replacement)) { | ||
// Serialize the representation of this lambda | ||
kryo.writeObject(output, replacement); | ||
} else | ||
throw new RuntimeException("Could not serialize lambda"); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not serialize lambda", e); | ||
} | ||
} | ||
|
||
public Object read (Kryo kryo, Input input, Class type) { | ||
try { | ||
Object object = kryo.readObject(input, serializedLambda); | ||
return readResolve.invoke(object); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not serialize lambda", e); | ||
} | ||
} | ||
|
||
public Object copy (Kryo kryo, Object original) { | ||
try { | ||
Class type = original.getClass(); | ||
Method writeReplace = type.getDeclaredMethod("writeReplace"); | ||
writeReplace.setAccessible(true); | ||
Object replacement = writeReplace.invoke(original); | ||
if (serializedLambda.isInstance(replacement)) { | ||
return readResolve.invoke(replacement); | ||
} else | ||
throw new RuntimeException("Could not serialize lambda"); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Could not serialize lambda", e); | ||
} | ||
} | ||
} |