package hudson.util;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.DataHolder;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import hudson.model.Hudson;
import hudson.util.CopyOnWriteList.ConverterImpl;

/**
 * {@link XStream} enhanced for retroweaver support.
 * @author Kohsuke Kawaguchi
 */
public class XStream2 extends XStream {
    public XStream2() {
        init();
    }

    public XStream2(HierarchicalStreamDriver hierarchicalStreamDriver) {
        super(hierarchicalStreamDriver);
        init();
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder) {
        // init() is too early to do this
        // defensive because some use of XStream happens before plugins are initialized.
        Hudson h = Hudson.getInstance();
        if(h!=null && h.pluginManager!=null && h.pluginManager.uberClassLoader!=null) {
            setClassLoader(h.pluginManager.uberClassLoader);
        }

        return super.unmarshal(reader,root,dataHolder);
    }

    private void init() {
        registerConverter(new RobustCollectionConverter(getClassMapper()),10);
        registerConverter(new RetroweaverEnumConverter(),10);
        registerConverter(new RetrotranslatorEnumConverter(),10);
        registerConverter(new CopyOnWriteList.ConverterImpl(getClassMapper()),10);
        registerConverter(new DescribableList.ConverterImpl(getClassMapper()),10);
    }
}

@@ -264,58 +264,8 @@
 son;
-%0Aimport hudson.util.CopyOnWriteList.ConverterImpl;
 %0A%0A/*

formatting change.