Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom ClassLoader for p13n and ClassCastException problem  RSS feed

Thierry Belanger
Posts: 1
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, I'll just explain the goal I'm trying to achieve.

I want to implement a p13n (personalization) system in an application. Personalized classes would be sub-classes of those in the core packages.
I want to create a custom ClassLoader that will first look in a p13n package for a personalized class, load it if present, or look in the core package and load the default class.

Here is my problem.
I created a simple CustomClassLoader that prefix the classname with "p13n." when loading a class, and upon a classNotFound, it loads the plain classname. This ClassLoader uses the childFirst pattern, obviously.
Let's say that there are no personalized classes yet, only core classes. When I call Class.forName("Blah", true, customClassLoader), my classLoader does the job allright, when inspecting Blah.class.getClassLoader(), it is CustomClassLoader as expected. In the context where Class.forName was called, I put the returned object in a variable of type Class and invoke newInstance(). The moment I try to cast the new Object instance into Blah, there is a ClassCastException. The reason is simple: Blah from CustomClassLoader cannot be cast into Blah from (the current ClassLoader).
Using super-classes or interfaces causes the same thing. If Blah extends SuperBlah or implements iBlah, CustomClassLoader loads its own bytes of Blah AND SuperBlah, and when the current context casts it into SuperBlah, the current ClassLoader kicks in and loads its own bytes of SuperBlah, and BANG! ClassCastException.

Is there a work around for this? I have been searching a lot and found nothing applicable.
I know that I can get away with reflection, but the context where I'm doing all of this has many references to Blah (fake name of course). I would be very complex.
If I use inheritance, is there a way to have CustomClassLoader load Blah by itself, but delegate the loading of SuperBlah to its parent (that is, the current ClassLoader) ? That way, the current context will cast successfully since they're the same class from the same ClassLoader. Can this be done without giving away my childFirst pattern ?

I've tried Thread.currentThread().setContextClassLoader() but to no avail. After calling that, inspecting SuperBlah.class.getClassLoader() does not show CustomClassLoader.

Is it possible in my current context to reference the SuperBlah symbol (I assume as soon as a class name is referenced, that's where the ClassLoader kicks in) and force its loading by a custom ClassLoader ?

If any genius mind can help, it would be a great breakthrough for me!
Thanks to all
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!