Have you ever looked at a serialization exception and not been able to figure out where the heck an errant object is hiding out? I used to run into this problem all the time as we use serialization for
HttpSession replication. Stack traces aren't much help. For example, this one only tells us we have a non-serializable instance of
Tee in a
List somewhere in our object graph:
Exception in thread "main" java.io.NotSerializableException: serialization.Main$Tee
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1075)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291)
at java.util.ArrayList.writeObject(ArrayList.java:569)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:890)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1333)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291)
at serialization.Main.main(Main.java:47)
After exploring numerous options, I've settled on a simple
ObjectOutputStream hack which keeps track of the path to the errant object with very little overhead. Now you can wrap serialization exceptions and add more information:
DebuggingObjectOutputStream out =
new DebuggingObjectOutputStream(...);
try {
out.writeObject(...);
} catch (Exception e) {
throw new RuntimeException(
"Serialization error. Path to bad object: "
+ out.getStack(), e);
}
The new exception message is much more helpful:
Exception in thread "main" java.lang.RuntimeException: Serialization error.
Path to bad object: [serialization.Main$Foo@94948a, serialization.Main$Bar@a401c2, [serialization.Main$Tee@ff5ea7], serialization.Main$Tee@ff5ea7]
at serialization.Main.main(Main.java:55)
Caused by: java.io.NotSerializableException: serialization.Main$Tee
We can now see that
Foo references
Bar which references a
List which contains
Tee. In our application, we actually log the type of each object, too, in case the
toString() output isn't helpful.
As a few key ObjectOutputStream members are final, I ended up having to access the private depth field and use it in conjunction with replaceObject():
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.List;
import java.util.ArrayList;
public class DebuggingObjectOutputStream
extends ObjectOutputStream {
private static final Field DEPTH_FIELD;
static {
try {
DEPTH_FIELD = ObjectOutputStream.class
.getDeclaredField("depth");
DEPTH_FIELD.setAccessible(true);
} catch (NoSuchFieldException e) {
throw new AssertionError(e);
}
}
final List<Object> stack
= new ArrayList<Object>();
/**
* Indicates whether or not OOS has tried to
* write an IOException (presumably as the
* result of a serialization error) to the
* stream.
*/
boolean broken = false;
public DebuggingObjectOutputStream(
OutputStream out) throws IOException {
super(out);
enableReplaceObject(true);
}
/**
* Abuse {@code replaceObject()} as a hook to
* maintain our stack.
*/
protected Object replaceObject(Object o) {
// ObjectOutputStream writes serialization
// exceptions to the stream. Ignore
// everything after that so we don't lose
// the path to a non-serializable object. So
// long as the user doesn't write an
// IOException as the root object, we're OK.
int currentDepth = currentDepth();
if (o instanceof IOException
&& currentDepth == 0) {
broken = true;
}
if (!broken) {
truncate(currentDepth);
stack.add(o);
}
return o;
}
private void truncate(int depth) {
while (stack.size() > depth) {
pop();
}
}
private Object pop() {
return stack.remove(stack.size() - 1);
}
/**
* Returns a 0-based depth within the object
* graph of the current object being
* serialized.
*/
private int currentDepth() {
try {
Integer oneBased
= ((Integer) DEPTH_FIELD.get(this));
return oneBased - 1;
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
}
/**
* Returns the path to the last object
* serialized. If an exception occurred, this
* should be the path to the non-serializable
* object.
*/
public List<Object> getStack() {
return stack;
}
}
I'll see what I can do about building a more secure version of this into
ObjectOutputStream in Java 7.