Search code examples
xmlclassloaderxstreamnetbeans-platform

Overload class loading in XStream


I'm implementing a save/load in NetBeans platform application. And I'm using XStream to serialize to XML.

But I have a problem with deserialization. When I was using ObjectInputStream, I've reloaded it this way:

public class NBSystemClassLoaderObjectInputStream extends ObjectInputStream
{
    public NBSystemClassLoaderObjectInputStream(InputStream in) throws IOException
    {
        super(in);
    }

    @Override
    public Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException
    {
        try
        {
            ClassLoader currentTccl = Thread.currentThread().getContextClassLoader();
            return currentTccl.loadClass(desc.getName());
        } catch (Exception e)
        {
        }
        return super.resolveClass(desc);
    }
}

So it'll use NB Platform features to find a class. How can I overwrite XStream class loading the same way?


Solution

  • XStream has two useful instance methods: getClassLoader and setClassLoader. The last one can be use to supply XStream instance with desired ClassLoader