Community
Participate
Working Groups
Hi we have a strange problem while using the java.beans.XMLEncoder (JDK 1.4+). When trying to encode one of our classes inside some Eclipse Plug-in code of ours we get the following exception (Message comes from the JDK): --- 8< snip --- java.lang.ClassNotFoundException: de.oio.bugkilla.DummyObject Continuing ... java.lang.Exception: discarding statement XMLEncoder0.writeObject(DummyObject0); Continuing ... --- 8< snip --- We attached some code which mimics the problem. Any ideas? Steffen and Kristian -------- java version "1.4.1_01" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01) Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode) -------- Eclipse Platform Version: 2.1 Build id: 200212181304
Created attachment 3232 [details] Sample Code see bug description
Found a workaround: the problem is that the Thread.currentThrad.getContextClassLoader() returns the wrong classloader. The workaround involves setting the plugin classloader as the context class loader. You need a static method to get the class loader from your plugin class: public class ToDoPlugin extends Plugin {
the problem is that the Thread.currentThrad.getContextClassLoader() returns the wrong classloader. The workaround involves setting the plugin classloader as the context class loader. You need a static method to get the class loader from your plugin class: public class ToDoPlugin extends Plugin { static ToDoPlugin instance; public ToDoPlugin(IPluginDescriptor descriptor) { super(descriptor); instance = this; } public ClassLoader getClassLoader() { return getDescriptor().getPluginClassLoader(); } public static ToDoPlugin getDefault() { return instance; } } and then you can set the correct classloader where you are using xmlencoder: Thread.currentThread().setContextClassLoader(ToDoPlugin.getDefault().getClassLoader()); // Serialize object into XML XMLEncoder encoder = new XMLEncoder(os); encoder.writeObject(getTopTaskItem()); encoder.close();
This is a known issue. We can't fix this in the Eclipse platform. Several plugins may be operating in a given thread, and we can't switch the context class loader depending on what plugin is currently executing in the thread. Also, if we hammer the context loader we may interfere with other tools that are setting this for a good reason. Your convenience method on your plugin to expose the class loader is also not needed. getDescriptor() is public so you can do this: ToDoPlugin.getDefault().getDescriptor().getPluginClassLoader()
The workaround should be somewhere in the FAQs. Wasted 2 hours on this :/