Re: Copy an Object from one ClassLoader to Another??

From: David Hilsee (davidhilseenews_at_yahoo.com)
Date: 07/22/04


Date: Wed, 21 Jul 2004 21:52:11 -0400


"John Davison" <jdaviso1@my-deja.com> wrote in message
news:6768b607.0407211520.6e64a6a6@posting.google.com...
> 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


Relevant Pages

  • Re: Copy an Object from one ClassLoader to Another??
    ... > I have an Object that was created in a custom class loader. ... > one ClassLoader is not equivalent to a Class from another ClassLoader. ... > system ClassLoader is not equivalent to the FooBar class that created ... You cannot cast c2 to newC2 ...
    (comp.lang.java.programmer)
  • Re: why do I get this runtime error
    ... public static void main(Stringargs) ... The cast uses Main from the "normal" class loader to cast a Main from the custom class loader, ... I'm very inexperienced with ClassLoader idioms so I am far from sure of this analysis. ...
    (comp.lang.java.programmer)
  • Re: How to set a new Custom ClassLoader?
    ... A class loader is an object that is responsible for loading classes. ... The class ClassLoader is an abstract class. ... the Java virtual machine loads classes from the local file ...
    (comp.lang.java.programmer)
  • Re: Why cant CLASSPATH be set on the fly
    ... But as the original op stated, I am creating a ClassLoader, ... > You'll understand why when you read up on how a class loader works, ... > This will break the language considerably. ... and the majority saying "it wont work" for very valid reasons. ...
    (comp.lang.java.programmer)
  • Re: Make system classloader aware of a class in jboss
    ... > I need to make the system class loader know about a class which isn't on the ... > classpath, but is loaded at run time. ... Every classloader has a place it gets its classes from. ...
    (comp.lang.java.programmer)