After executing a JavaScript function that injects a style attribute on a HTML tag using HtmlUnit-2.13, we cannot serialize the resultant HtmlPage and we receive the following error:
java.io.NotSerializableException:com.gargoylesoftware.htmlunit.javascript.host.css.CSSStyleDeclaration$StyleElement
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeWriteObject(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
If the style attribute is on the Page and not injected, the page can be serialized without any problem.
Updated on 12/04/2013:
I found the problem to be located at this field:
private Map<String, StyleElement> styleMap_;
which is member of the CSSStyleDeclaration class. The StyleElement class does not implement the Serializable interface.
Initially, the field equals to null and no problem occurs. However, once a JavaScript event is executed, the Map does not longer point to null and the exception is being thrown.
Am I doing something wrong ? This field is supposed to be always null or nobody has tried to serialize that class before ?
I had the same problem. I created a bug, and it's fixed now: https://sourceforge.net/p/htmlunit/bugs/1569/ So you can use latest build from their SVN and it works fine!