Simeon
Simeon

Reputation: 7792

Can a custom ClassLoader load a class ignoring it's dependencies?

I have a binary class which I want to load, but I don't have it's dependencies. I still want to load it though, to get it's qualified name.

I understand that I will not be able to use it for anything else (and that is ok), I just need the qualified name.

So is there a way to do this with a custom class loader ?

Thanks,

Upvotes: 0

Views: 132

Answers (1)

Marcin Łoś
Marcin Łoś

Reputation: 3246

As for strictly answering the question: see section on symbolic references resolution in the JVM specification. In short: little is guaranteed as to when resolution is performed. I'm not sure what is the precise behaviour of current implementations, but if you go this way, the solution will not be completely reliable, even though it will probably work.

You said you only need its qualified name, though. That's a different story, that's pretty easy, in fact. JVM specification describes in detail the format of the class file. Since you have the binaries, you can extract it directly from the data, bypassing classloading mechanism of the JVM completely. If you don't want to do it by hand, use appropriate tools - ASM comes to mind, with its wonderful, detailed documentation. Some alternatives are BCEL, javassist and CGLIB (no longer maintained).

Upvotes: 1

Related Questions