KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > ThrowableConverter


1 package com.thoughtworks.xstream.converters.extended;
2
3 import com.thoughtworks.xstream.converters.Converter;
4 import com.thoughtworks.xstream.converters.MarshallingContext;
5 import com.thoughtworks.xstream.converters.UnmarshallingContext;
6 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
7 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
8
9 import java.lang.reflect.InvocationTargetException JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12
13 /**
14  * Converter for Throwable (and Exception) that retains stack trace, for JDK1.4 only.
15  *
16  * @author <a HREF="mailto:boxley@thoughtworks.com">B. K. Oxley (binkley)</a>
17  * @author Joe Walnes
18  */

19 public class ThrowableConverter implements Converter {
20
21     private Converter defaultConverter;
22
23     public ThrowableConverter(Converter defaultConverter) {
24         this.defaultConverter = defaultConverter;
25     }
26
27     public boolean canConvert(final Class JavaDoc type) {
28         return Throwable JavaDoc.class.isAssignableFrom(type);
29     }
30
31     public void marshal(Object JavaDoc source, HierarchicalStreamWriter writer, MarshallingContext context) {
32         Throwable JavaDoc throwable = (Throwable JavaDoc) source;
33         throwable.getStackTrace(); // Force stackTrace field to be lazy loaded by special JVM native witchcraft (outside our control).
34
defaultConverter.marshal(throwable, writer, context);
35     }
36
37     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
38         return defaultConverter.unmarshal(reader, context);
39     }
40 }
41
Popular Tags