lovespring
lovespring

Reputation: 19589

About ClassLoader, what happen if we didn't delegate to the parent class loader?

A ClassLoader should delegate the loading process to it's parent first. This is some docs says.

So, what happen if we don't follow this pattern. that is:

We override loadClass, and in it's implementation, we directly call:

defineClass(...)  // get Class objet for a given byte array
resoveClass(...)  // link class to vm

Upvotes: 1

Views: 626

Answers (1)

vanOekel
vanOekel

Reputation: 6548

Tomcat's Web Application Classloader is an example of a Classloader that does not (always) delegate the loading process to its parent first (as per Servlet Specification). The classloader-howto article explains this and also gives insight in what (not) to do. If you want to know how it works in practice, you can view the source code of WebappClassLoader.

On a side node, creating a URLClassLoader without passing on Thread.currentThread().getContextClassLoader() as parent classloader in the constructor can give surprising results if the current Thread was started with a custom Classloader. The URLClassLoader will only know the classes from the System Classloader and the classes in the URLs provided, but not the classes from the custom Classloader.

Upvotes: 2

Related Questions