diff --git a/src/main/java/org/nustaq/serialization/FSTConfiguration.java b/src/main/java/org/nustaq/serialization/FSTConfiguration.java index 499f5864..5129943f 100644 --- a/src/main/java/org/nustaq/serialization/FSTConfiguration.java +++ b/src/main/java/org/nustaq/serialization/FSTConfiguration.java @@ -501,6 +501,7 @@ protected static FSTConfiguration initDefaultFstConfigurationInternal(FSTConfigu reg.putSerializer(Hashtable.class, new FSTMapSerializer(), true); reg.putSerializer(ConcurrentHashMap.class, new FSTMapSerializer(), true); reg.putSerializer(FSTStruct.class, new FSTStructSerializer(), true); + reg.putSerializer(Throwable.class, new FSTThrowableSerializer(),true); reg.putSerializer(BitSet.class, new FSTBitSetSerializer(),true); reg.putSerializer(Timestamp.class, new FSTTimestampSerializer(),true); diff --git a/src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java b/src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java new file mode 100644 index 00000000..96d95de2 --- /dev/null +++ b/src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.java @@ -0,0 +1,38 @@ +package org.nustaq.serialization.serializers; + +import org.nustaq.serialization.FSTBasicObjectSerializer; +import org.nustaq.serialization.FSTClazzInfo; +import org.nustaq.serialization.FSTObjectInput; +import org.nustaq.serialization.FSTObjectOutput; + +import java.io.IOException; +import java.lang.reflect.Constructor; + +public class FSTThrowableSerializer extends FSTBasicObjectSerializer { + @Override + public void writeObject(FSTObjectOutput out, Object toWrite, FSTClazzInfo clzInfo, + FSTClazzInfo.FSTFieldInfo referencedBy, int streamPosition) throws IOException { + Throwable t = (Throwable)toWrite; + out.writeStringUTF(t.getMessage()); + StackTraceElement[] ste = t.getStackTrace(); + out.writeObject(ste); + out.writeObject(t.getCause()); + out.writeObject(t.getSuppressed()); + } + + + @Override + public Object instantiate(Class objectClass, FSTObjectInput in, FSTClazzInfo serializationInfo, + FSTClazzInfo.FSTFieldInfo referencee, int streamPosition) throws Exception { + Constructor constructor = objectClass.getConstructor(String.class); + Throwable t = constructor.newInstance(in.readStringUTF()); // This causes stack trace to be filled in twice but not an easy way to solve + StackTraceElement[] ste = (StackTraceElement[]) in.readObject(); + if (ste!=null) + t.setStackTrace(ste); + t.initCause((Throwable) in.readObject()); + Throwable[] suppressed = (Throwable[]) in.readObject(); + for (Throwable s : suppressed) + t.addSuppressed(s); + return t; + } +}