473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Copy an Object from one ClassLoader to Another??

I have an Object that was created in a custom class loader. Since a
Class is defined as the class name plus the class loader, a Class from
one ClassLoader is not equivalent to a Class from another ClassLoader.
For example:

Class c1 = getClass().getClassLoader().loadClass( "com.acme.FooBar" );
Class c2 = CustomClassLoader.loadClass( "com.acme.FooBar" );
FooBar newC2 = (FooBar)c2; // This throws ClassCastException!

You can not cast c2 to FooBar because the FooBar class loaded by the
system ClassLoader is not equivalent to the FooBar class that created
c2.

I've worked around this problem by serializing my c2 object into a
byte array, then deserializing my byte array back into an Object.
This has the effect of recreating the c2 object in system class
loader.

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream( baos );
oos.writeObject( c2 );

ByteArrayInputStream bais = new ByteArrayInputStream(
baos.toByteArray() );
ObjectInputStream ois = new ObjectInputStream( bais );
FooBar newC2 = (FooBar) ois.readObject();

It's a bit of a hack, but it works. The only caveat is that FooBar
must be Serializable. Now, how do I get the object created by
CustomClassLoader into my system class loader??? I've found one
method, but I'm looking for other suggestions (hopefully better than
mine).

-- John
Jul 17 '05 #1
1 4618
"John Davison" <jd******@my-deja.com> wrote in message
news:67**************************@posting.google.c om...
I have an Object that was created in a custom class loader. Since a
Class is defined as the class name plus the class loader, a Class from
one ClassLoader is not equivalent to a Class from another ClassLoader.
For example:

Class c1 = getClass().getClassLoader().loadClass( "com.acme.FooBar" );
Class c2 = CustomClassLoader.loadClass( "com.acme.FooBar" );
FooBar newC2 = (FooBar)c2; // This throws ClassCastException!

You can not cast c2 to FooBar because the FooBar class loaded by the
system ClassLoader is not equivalent to the FooBar class that created
c2.


I think you left a call to newInstance() out. You cannot cast c2 to newC2
because c2 is an instance of Class and FooBar is not derived from it. I
just wanted to make sure that nobody gets confused.

--
David Hilsee
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Cruinne | last post by:
Hello, In a JBoss 3.2.4 server, I have deployed several enterprise javabeans (each as a separate .jar, not in an .ear bundle). These beans are completely self-contained, as they include all...
4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
5
by: stephan beal | last post by:
Good afternoon, C++ers, (i hope this is not viewed as an advertisement or shameless plug, and i appologize if it is interpretted that way.) i have developed a template-based classloader which...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
by: Jul | last post by:
Hi, I need to create copy (clone) the OracleConnection object. The OracleConnection class does not contain "Clone" method for creating copy. It contains Protected MemberwiseClone but if use...
4
by: OpticTygre | last post by:
If I pass an object to another form via the new form's tag property, I want to create an object exactly like it, with it's properties and all, but have it be a copy of the object passed through,...
8
by: rKrishna | last post by:
I was trying to understand the real need for copy constructors. From literature, the main reason for redfinition of copy constructor in a program is to allow deep copying; meaning ability to make...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.