On Thu, Jan 21, 2010 at 2:50 PM, Lior <lior.gon...@gmail.com> wrote:
> Hi guys,
>
> I'm trying to replace the class loader with my custom class loader. I
> don't want to replace the default functionality, only add new
> functionality.
>
> The goal is that every new class instance that calls:
> getClass().getResourceAsStream(resName)
>
> will load the requested resource in the way I define in my
> CustomClassLoader.
>
> My code contains the following:
> ClassLoader classLoader = new CustomClassLoader
> (ClassLoaderTestActivity.class.getClassLoader()); // Create my own
> class loader with the default class loader as parent.
>
> Thread.currentThread().setContextClassLoader(classLoader); // This is
> just for testing
>
> TestClass test = (TestClass)Class.forName("com.test.TestClass", true,
> classLoader).newInstance() ;
>
> Since the 'test' TestClass instance is created with reference to the
> new class-loader, I'd expect that after this line executes,
> test.getClass().getClassLoader() will return an instance of
> CustomClassLoader, but I always get the default PathClassLoader
> returned.

Why would you expect that? Java uses parent delegation first so it
will find the class in the parent and return that one -- hence, its
classloader is the parent.

> My only assumption so far is that since my CustomClassLoader only
> overrides specific functionality, and the ClassLoader that actually
> calls 'defineClass' is the default one, the TestClass instance is
> never associated with my ClassLoader.

yup. as it should be.

> Any help would be highly appreciated.

Well, what is it you want to do? You now have a working classloader
that delegates classloads to its parent. Assuming you only want to
override getResource* then you still need to do that. Otherwise, I'm
missing your problem...

regards,

Karl
> Thanks,
> Lior
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Karl Pauls
karlpa...@gmail.com
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to