Java ClassLoader delegation model?

前端 未结 6 1499
悲哀的现实
悲哀的现实 2021-02-01 08:35

When calling loadClass() on a ClassLoader, does the ClassLoader first check if the class has been loaded, or does it immediately delegate this check to

6条回答
  •  南方客
    南方客 (楼主)
    2021-02-01 09:22

    To concur with Sri's answer, it will always get delegated to the parent and the api is correct. If you are playing with classloading, this can make things a little tricky to get right, or achieve the effects you are after. I would suggest starting the jvm with a minimal classpath and then loading all classes using your custom classloader, the easiest way to do this is to use a URLClassloader or a composite object wrapping a URLClassloader so you would be able to trace which classes are loaded and when.

    Also worth bearing in mind that a class A loaded by classloader C != class A loaded by classloader C if C and D are not part of the same classloader-parent-child hierarchy.

提交回复
热议问题