package com.thoughtworks.xstream.converters.extended;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Converter for Throwable (and Exception) that retains stack trace, for JDK1.4 only.
 *
 * @author <a href="mailto:boxley@thoughtworks.com">B. K. Oxley (binkley)</a>
 * @author Joe Walnes
 */
public class ThrowableConverter implements Converter {

    private final static Method initCause;
    private final static Method getCause;
    static {
        Method init = null;
        Method get = null;
        try {
            init = Throwable.class.getDeclaredMethod("initCause", new Class[]{Throwable.class});
            get = Throwable.class.getDeclaredMethod("getCause", null);
        } catch (NoSuchMethodException e) {
            // JDK 1.3
        }
        initCause = init;
        getCause = get;
    }
    
    private Converter defaultConverter;

    public ThrowableConverter(Converter defaultConverter) {
        this.defaultConverter = defaultConverter;
    }

    public boolean canConvert(final Class type) {
        return Throwable.class.isAssignableFrom(type);
    }

    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        Throwable throwable = (Throwable) source;
        if (getCause != null) {
            try {
                Throwable cause = (Throwable)getCause.invoke(throwable, null);
                if (cause == null) {
                    initCause.invoke(throwable, new Object[]{ null });
                }
            } catch (IllegalArgumentException e) {
                // ignore, initCause failed, cause was already set
            } catch (IllegalAccessException e) {
                // ignore
            } catch (InvocationTargetException e) {
                // ignore
            }
        }
        throwable.getStackTrace(); // Force stackTrace field to be lazy loaded by special JVM native witchcraft (outside our control).
        defaultConverter.marshal(throwable, writer, context);
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        return defaultConverter.unmarshal(reader, context);
    }
}

@@ -357,94 +357,8 @@
 r;%0A%0A
-import java.lang.reflect.InvocationTargetException;%0Aimport java.lang.reflect.Method;%0A%0A
 /**%0A
@@ -608,477 +608,8 @@
 er %7B
-%0A%0A    private final static Method initCause;%0A    private final static Method getCause;%0A    static %7B%0A        Method init = null;%0A        Method get = null;%0A        try %7B%0A            init = Throwable.class.getDeclaredMethod(%22initCause%22, new Class%5B%5D%7BThrowable.class%7D);%0A            get = Throwable.class.getDeclaredMethod(%22getCause%22, null);%0A        %7D catch (NoSuchMethodException e) %7B%0A            // JDK 1.3%0A        %7D%0A        initCause = init;%0A        getCause = get;%0A    %7D
 %0A   
@@ -1044,24 +1044,34 @@
     if (
+throwable.
 getCause
  != null
@@ -1066,10 +1066,12 @@
 ause
- !
+() =
 = nu
@@ -1114,467 +1114,152 @@
     
-Throwable cause = (Throwable)getCause.invoke(throwable, null);%0A                if (cause == null) %7B%0A                    initCause.invoke(throwable, new Object%5B%5D%7B null %7D);%0A                %7D%0A            %7D catch (IllegalArgumentException e) %7B%0A                // ignore, initCause failed, cause was already set%0A            %7D catch (IllegalAccessException e) %7B%0A                // ignore%0A            %7D catch (InvocationTargetException e) %7B%0A                // ignore
+throwable.initCause(null);%0A            %7D catch (IllegalArgumentException e) %7B%0A                // ignore, initCause failed, cause was already set
 %0A   

Remove reflection stuff, ThrowableConverter if 1.4 only anyway.