-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathFSTThrowableSerializer.java
38 lines (33 loc) · 1.66 KB
/
FSTThrowableSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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() != null ? t.getMessage() : "null");
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<? extends Throwable> 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;
}
}