Bug 30588 - Classloader Problems while using java.beans.XMLEncoder
Summary: Classloader Problems while using java.beans.XMLEncoder
Status: RESOLVED WONTFIX
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Resources (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows 2000
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Platform-Resources-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-01-30 04:43 EST by Kristian Köhler CLA
Modified: 2004-02-15 01:00 EST (History)
1 user (show)

See Also:


Attachments
Sample Code (4.66 KB, application/x-zip-compressed)
2003-01-30 04:44 EST, Kristian Köhler CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Kristian Köhler CLA 2003-01-30 04:43:10 EST
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
Comment 1 Kristian Köhler CLA 2003-01-30 04:44:17 EST
Created attachment 3232 [details]
Sample Code

see bug description
Comment 2 Alex Cozzi CLA 2003-01-30 19:28:22 EST
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 {
Comment 3 Alex Cozzi CLA 2003-01-30 19:35:43 EST
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();
Comment 4 John Arthorne CLA 2003-06-02 17:30:51 EDT
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()
Comment 5 Sebastian Davids CLA 2004-02-15 01:00:58 EST
The workaround should be somewhere in the FAQs.

Wasted 2 hours on this :/