In-depth understanding of ClassLoader (iv)-Class parent delegate loading mechanism

Source: Internet
Author: User

Last time we introduced several class loaders inside the JVM , let's redraw the diagram and look at the relationship between them.

Jvm classloader uses a tree-shaped structure, in addition bootstrapclassloader ? Each classloader will have a parentclassloader , user-defined classloader default Span lang= "en-US" >systemclassloader , of course, you can specify which classloader For an instance of , let's look at his api

The default parameterless construction method uses systemclassloader, and you can specify his parent class loader by passing in an instance of ClassLoader . It is emphasized here that many people think that each parent-child ClassLoader is an inheritance relationship, here to clarify that the parent-child class loader is a composite relationship, the subclass ClassLoader will contain a parentclassloader object, Classes are usually loaded in a tree-like way, that is, first trying to load from parentclassloader , when the parent fails to load, Load from the current class loader, and so on. the JVM guarantees that a class will only be loaded once in the same ClassLoader .

ClassLoader Abstract class defines a series of key methods for us, let's take a look at

1. The loadclass method , which is used to load the class of the specified name, isClassLoader from the loaded class and, if not, loaded with the parent loader and loaded if the load succeeds , or it is loaded from the current ClassLoader, and throws an exception if the class file has not been found classnotfoundexception

If the class requires a link, it is linked through resolveclass .

2.DefineClass, this method is used to convert a binary bytecode into aClass object, the custom loading of the class is very important, of course, as we have already said, when the class of binary files are loaded into memory, to do parsing, semantic analysis, such as a series of validation, if not conform to the JVM specification, The classformateerror error is thrown if the generated class name and the byte code are inconsistent, then the noclassdeffoundexceptionis thrown if the loaded Class is protected, with a different label name, or a java.* begins, throws SecurityExceptionif the Class has been loaded before, it throws linkageerrordirectly.

3.Resolveclass, this method completesClassLink, and return directly if the link is received. WhenJavaDeveloper CallsClass.forNameTo get aclassObject,JVMWill look for the first one on the method stackClassLoader, usually executingclass.forname " Font-family:calibri; " >classloader , and use this classloader to load this class. classloader unloads the loaded class directly, only JVM can only be uninstalled, in jdk , only no   When the is referenced, the second classloader loaded classes are unloaded!

Attached: part of the source code of ClassLoader inJDK

1. constructor Function

protectedClassLoader (ClassLoader parent) {SecurityManager Security=System.getsecuritymanager (); if(Security! =NULL) {security.checkcreateclassloader (); }     This. Parent =parent; Initialized=true;} protectedClassLoader () {SecurityManager security=System.getsecuritymanager (); if(Security! =NULL) {security.checkcreateclassloader (); }     This. Parent =Getsystemclassloader (); Initialized=true; }

2 , LoadClass

  PublicClass<?> loadclass (String name)throwsClassNotFoundException {returnLoadClass (Name,false); }    protected synchronizedClass<?> loadclass (String name,Booleanresolve)throwsClassNotFoundException {//First , check if the class has already been loadedClass C =Findloadedclass (name); if(c = =NULL) {        Try {       if(Parent! =NULL) {C= Parent.loadclass (Name,false); } Else{C=FINDBOOTSTRAPCLASS0 (name); }        } Catch(ClassNotFoundException e) {//If still not found, then invoke Findclass in order//To find the class.c =Findclass (name); }    }    if(Resolve) {resolveclass (c); }    returnC;}

This loading mechanism of class is called the parent-delegate loading mechanism , and the advantage of the parent-delegation mechanism is that it can improve the security of the software system. Because under the word mechanism, a user-defined ClassLoader cannot load a reliable class that should be loaded by the parent loader, preventing unreliable malicious code from replacing a reliable class loaded by the parent ClassLoader, thereby preventing unreliable and even malicious code from being loaded by the parent class loader. For example, theJava.lang.Object class is always loaded by the root ClassLoader, and no other user-defined class loader can load the Java.lang.Object class that contains malicious code.

The class loader is defined, and its parent ClassLoader is referred to as the initial class loader.

We know that classes with the same class name are likely to appear in Java , but the JVM can load normally because we put the same class name class under the package. This also becomes a namespace, and each classloader has its own namespace, and the namespace is made up of classes loaded by the loader and by all the parent loaders. In the same namespace, the full name of the class (package name + class name) does not appear in the same two classes, and in different namespaces it is possible to have two classes with the same full name of the class.

Classes that are loaded by the same class loader that belong to the same package make up a run-time package. Decide whether the two classes belong to the same run-time package, not only to see if their package names are the same, but also to see if they define the same classloader. Only classes and members that are visible (the default access level) can be accessed between classes that belong to the same run-time package. Assume that the user has customized a class java.lang.TestCase and is loaded by the class loader for the custom, because the java.lang.TestCase and Core class libraries java.lang.* are loaded by different class loaders, they belong to different run-time packages, so java.lang.TestCase cannot access the package visible members in the core library Java.lang package.

Classes within the same namespace are visible to each other.

The namespace of the subclass loader contains all of the parent ClassLoader's namespaces, so classes loaded by the subclass loader can see the classes loaded by the parent ClassLoader, and the classes loaded by the parent ClassLoader, instead, see the class that the subclass loader loads. If there is no direct or indirect parent-child relationship between the two loaders, the classes they load are not visible to each other.

In-depth understanding of ClassLoader (iv)-Class parent delegate loading mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.