views:

390

answers:

2

I've tried this, but get a ClassNotFoundException when calling:

Class.forName("com.AClass", false, mySpecialLoader)
+4  A: 

The ClassLoader will have to call defineClass to get the Class. According to the JavaDoc for defineClass:

If name is not null, it must be equal to the binary name of the class specified by the byte array.

If the name is null, it will get it from the bytecode. So you can return any class you want as long as it's called com.AClass. In other words, you could have multiple versions of com.AClass. You could even use something like JavaAssist to create a class on the fly.

But that doesn't explain the ClassNotFoundException - it sounds like your class loader isn't returning anything.

Brian Deterling
+1  A: 

It is impossible to return a class named differently than the one requested. However it is possible to use bytecode manipulation tools like ASM to automatically rename the class you want to return to the one requested.

Jevgeni Kabanov