Class loader ClassLoader

Source: Internet
Author: User

class loader working mechanism

The class loader is looking for the class's section code file and constructs the component that the class represents the object within the JVM. In Java, the class loader loads a class into the JVM, taking the following steps:

        [1.] Loading: Finding and Importing class files;
        [2.] Link: Perform the validation, preparation and resolution steps, where the resolution step is optional:
        [2.1] Verification: Check the correctness of loading class file data;
        [2.2] Preparation: Assign storage space to static variables of class;
        [2.3] parsing: The symbolic reference is transferred to a direct reference;
        [3.]3. Initialize: Initializes the static variables of the class and static blocks of code.

Class loading work is the responsibility of ClassLoader and its subclasses, ClassLoader is an important Java Runtime system component that is responsible for locating and loading class bytecode files at run time. The JVM generates three ClassLoader at run time: the root loader, the Extclassloader (Extension class loader), and the Appclassloader (System class loader). Where the root loader is not a subclass of ClassLoader, it is written in C + +, so we do not see it in Java, the root loader is responsible for loading the JRE's core class library, such as Rt.jar, Charsets.jar, etc. under the JRE target. Both Extclassloader and Appclassloader are subclasses of ClassLoader. Where Extclassloader is responsible for loading the jar class package in the JRE extension directory ext; Appclassloader is responsible for loading the class package under the Classpath path.

There is a parent-child hierarchy between these three class loaders, that is, the root loader is the parent loader for Extclassloader, and Extclassloader is the parent loader for Appclassloader. By default, using Appclassloader to load an application's class, we can do an experiment:

Code Listing 3-11 Classloadertest

Java code
 Public class classloadertest {      publicstaticvoid  main (string[] args) {          = Thread.CurrentThread (). Getcontextclassloader ();          System.out.println ("Current loader:" +loader);          System.out.println ("Parent loader:" +loader.getparent ());          System.out.println ("grandparent loader:" +loader.getparent (). GetParent ());      }  }

Run the above code and the following information will be played on the console:

Quote current Loader:sun.misc.laun[email protected]
Parent Loader:[email Protected]
① root loader is not accessible in Java, so null is returned
Grandparent Loader:null

Through the above output information, we know that the current ClassLoader is Appclassloader, the parent ClassLoader is Extclassloader, grandfather ClassLoader is the root class loader, because in Java can not get its handle, Therefore, only null is returned.

The JVM loads the class using the "overall responsibility mechanism", "overall responsibility" refers to when a classloader load a class, unless explicitly using another ClassLoader, the class is dependent on and referenced by the class is also loaded by this classloader; "delegation mechanism" is to delegate the parent loader to look for the target class, and to find and load the target class from its own classpath only if it is not found. This is from a security perspective, just imagine how horrible it would be if someone wrote a malicious base class (such as java.lang.String) and loaded into the JVM. However, due to the "overall accountability mechanism", java.lang.String is always loaded by the root loader, thus avoiding the occurrence of the above event.

ClassLoader Important Methods

In Java, ClassLoader is an abstract class that is located in the Java.lang package. Here are some important interface methods for this class:

      • Class loadclass (String name)
      • The name parameter specifies that the class loader needs to load the name of the class, and must use the fully qualified class name, such as Com.baobaotao. Beans. Car. The method has an overloaded method LoadClass (String name, Boolean resolve), and the Resolve parameter tells the class loader whether the class needs to be parsed.  Before initializing a class, you should consider the work of class parsing, but not all classes need to be parsed, and if the JVM only needs to know if the class exists or finds a superclass of that class, then no parsing is required.
      • Class defineclass (String name, byte[] B, int off, int len)
      • Converts a byte array of a class file into a Java.lang.Class object inside the JVM. Byte arrays can be obtained from the local file system, the remote network.  Name is the fully qualified class name that corresponds to the byte array.
      • Class Findsystemclass (String name)
      • Loading a class file from the local file system throws a ClassNotFoundException exception if the class file does not exist on the local file system.  This method is the load mechanism used by the JVM by default.
      • Class Findloadedclass (String name)
      • Call this method to see if ClassLoader has mounted a class. Returns a Java.lang.Class object if it is mounted, otherwise null is returned.  If you forcibly load a class that already exists, a link error will be thrown.
      • ClassLoader getParent ()
      • Gets the parent loader of the class loader, outside of the root loader, all class loaders have and only one parent loader, Extclassloader's parent loader is the root loader, because the root loader is not written in Java, so it cannot be obtained and will return null.

In addition to the JVM default of three ClassLoader, you can write your own third-party class loader to achieve some special requirements. After the class file is loaded and parsed, it will have a corresponding Java.lang.Class class description object within the JVM, and the instance of the class has a reference to the class description object, and the class description object has a reference to the associated ClassLoader, as shown in 3-4. &NBSP

 

Each class has a corresponding Java.lang.Class object in the JVM, which provides a description of the class structure information. arrays, enumerations, annotations, and basic Java types (such as int, double, and so on), even void have a corresponding class object. Class does not have a constructor method for public. Class objects are constructed automatically by the JVM by invoking the DefineClass () method in the class loader when the class is loaded. &NBSP

java reflection Mechanism   the

Class reflection object describes a class semantic structure that can be used to get a reflection object from a class object, such as a constructor, member variable, method class, and so on. and programmatically manipulate the target class object through these reflective objects. These reflective object classes are defined in the Java.reflect package, and the following are the main three reflection classes:  

      • ? Constructor: The class's constructor reflects the class, and the Class#getconstructors () method allows you to get an array of reflection objects for all constructors of the class. In JDK5.0, you can also get a constructor reflection object with a specific entry parameter through GetConstructor (Class ... parametertypes). One of the main methods of constructor is Newinstance (object[] initargs), which allows you to create an instance of an object class that is equivalent to the new keyword. In JDK5.0, the method evolves into a more flexible form: newinstance (Object ... initargs).
      • ? Method: Class method of Reflection class, through the Class#getdeclaredmethods () method can get all the methods of the class reflection class object array method[]. In JDK5.0, you can obtain a method of a specific signature by Getdeclaredmethod (String name, class ... parametertypes), name is the method name, class ... The list of parameter types for the method. The main methods of method are invoke (object obj, object[] args), obj represents the target object of the operation, args is the method entry, and code listing 3 shows how to use the Reflection class at 10③. In JDK 5.0, the method is adjusted to invoke (object obj, Object ... args). In addition, method has a number of ways to get more information about class methods:
      • 1) Class Getreturntype (): Gets the return value type of the method;
        2) class[] Getparametertypes (): Gets an array of parameter types for the method;
        3) class[] Getexceptiontypes (): Gets an array of exception types for the method;
      4) annotation[][] Getparameterannotations (): Gets the annotated information of the method, the new method in JDK 5.0;
    • ? Field: The reflection class of a member variable of a class, the Class#getdeclaredfields () method allows you to get an array of reflection objects of the class's member variables by Class#getdeclaredfield (String name) You can get a member variable reflection object of a specific name. The most important method of the field class is set (Object obj, object value), which obj represents the target object of the operation, and value is set for the member variable of the target object by value. If the member variable is the underlying type, the user can use the value of the type name provided in the field class to set the method, such as SetBoolean (Object obj, Boolean value), Setint (object obj, int value), and so on.

In addition, Java provides package reflection classes for packages, as well as annotatedelement reflection classes for annotations in JDK 5.0. In summary, the Java reflection system guarantees that all elements in the target class can be accessed programmatically, and for private or protected member variables and methods, as long as the security mechanism of the JVM permits, and can be invoked through reflection, see the following example:
Code Listing 3-12 Privatecarreflect

Java code
 Package com.baobaotao.reflect;    Public class Privatecar {         //①private member Variable: Use traditional class instance invocation method, only in this class to access     Private String color;            // ②protected Method: Use traditional class instance invocation method, only in subclass and this package access     protected void Drive () {           System.out.println ("Drive private car! The color is:" +color);     }  }  

Both the color variable and the drive () method are private and cannot be accessed externally by a class instance variable, and the private method is called, but this restriction can be bypassed through the reflection mechanism:

Code Listing 3-13 Privatecarreflect

Java code
... .. Public classPrivatecarreflect { Public Static voidMain (string[] args)throwsthrowable{ClassLoader Loader=Thread.CurrentThread (). Getcontextclassloader (); Class Clazz= Loader.loadclass ("Com.baobaotao.reflect.PrivateCar"); Privatecar Pcar=(Privatecar) clazz.newinstance (); Field colorfld= Clazz.getdeclaredfield ("Color"); //① Removing Java language access checks to access private variablesColorfld.setaccessible (true); Colorfld.set (Pcar,Red); Method DRIVEMTD= Clazz.getdeclaredmethod ("Drive", (class[])NULL); //Method DRIVEMTD = Clazz.getdeclaredmethod ("drive"); Use under JDK5.0//② Remove the Java language Access check to access the protected methodDrivemtd.setaccessible (true); Drivemtd.invoke (Pcar, (object[])NULL); }  } 

Run the class and print out the following information:

Quote Drive Private car! The color is: red


The Java language check must be canceled through the setaccessible (Boolean access) method when accessing private, protected member variables, and methods, or illegalaccessexception will be thrown. If the security manager of the JVM has set the appropriate security mechanism, calling the method will throw SecurityException.

Class loader ClassLoader

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.