-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db7ba74
commit 00747d5
Showing
2 changed files
with
39 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
38 changes: 38 additions & 0 deletions
38
src/main/java/org/nustaq/serialization/serializers/FSTThrowableSerializer.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,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<? 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; | ||
} | ||
} |