Java program class loading and reflection mechanism

Source: Internet
Author: User

[It168 technology]When you call a Java command to run a Java program, the command starts a Java VM process. All the threads and variables of the same JVM are in the same process, they are all the memory zones that use the JVM process.

  • The program runs until it ends normally.
  • Run the program to end the program using the system. Exit () or Runtime (). Exit () code.
  • The program ends when an uncaptured exception or error occurs during execution.
  • The JVM process is forcibly terminated on the platform where the program is located.

Class Loading

Class loading refers to reading the class file of the class into the memory and creating a java. lang. class objects, and all the classes in the system are actually objects, called class objects. They are all Java. lang. class instance, the virtual machine manages a unique Class Object for each type, that is, each class (type) has a class object.

Class connection

The connection phase will merge the binary data of the class into the JRE.

  • Verify
  • Preparation: the class preparation stage allocates the static attributes of the class to memory and sets the default initial values.
  • Analysis

Class initialization

In the class initialization phase, the virtual machine is responsible for class initialization. It mainly initializes static attributes. In the Java class, there are two ways to specify the initial values for static attributes: (1) specify an initial value when declaring a static attribute. (2) use a static initialization block to specify an initial value for a static attribute.

When a Java program uses a class or interface in the following six ways for the first time, the system will initialize the class or interface.

A) create a class instance.

B) Call the static method of a class.

C) access the static attribute of a class or interface, or assign a value to the static attribute.

D) Use the Reflection Method to forcibly create a java. Lang. Class Object corresponding to a class or interface.

E) initialize a subclass of A Class. When initializing a subclass of a class, all the parent classes of this subclass will be initialized.

F) When you directly use the java.exe command to run a primary class, the program will first initialize the primary class.

Class Loader

The Class Loader loads the. Class file into the memory and generates the corresponding java. Lang. Class Object for it.

In Java, a class uses its fully qualified class name (including the package name and Class Name) as its identifier.

In JVM, a class uses its fully qualified class name and its class loader as its unique identifier.

When JVM is started, three class loaders are formed:

  • Bootstrap classloader: the root class loader is not a subclass of Java. Lang. classloader, but Rt. Jar implemented by JVM itself.
  • Extension classloader: the extension class loader is responsible for loading the jar class package in the extension directory ext of JRE.
  • System classloader: The system class loader is responsible for loading the-classpath option or Java from the command Java at JVM startup. class. path System attribute, or the jar package and class path specified by the classpath environment variable. The current path is used as the system loading path by default.

Appclassloader: Custom class loader.

JVM class loading mechanism

  Overall responsibility

Parent class delegate: the so-called parent class delegate is to first let the parent (parent) Class Loader try to load this class, the parent class loader tries to load the class from its own class path only when it cannot load the class.

Cache Mechanism

View class information through reflection

There are three methods to obtain class objects in a Java program:

A) use the forname () Static Method of the class. This method requires the input of a string parameter. The value of this string parameter is the fully qualified class name of a class (the complete package name must be added ).

B) Call the class attribute of a class to obtain the class object corresponding to the class.

C) Call the getclass () method of an object. The method is Java. lang. A Method in the object class. Therefore, all Java objects can call this method. This method returns the class object corresponding to the class to which the object belongs.

Method B: the code is safer. during compilation, the program can check whether the class object to be accessed exists.

The program performance is improved because this method does not need to be called, so the performance is better.

Once you obtain the class object corresponding to a class, you can call the Class Object method to obtain the real information of the object and the class.

Getdeclared has nothing to do with the access level. It is explicitly declared.

Get gets all but only public, including inherited.

Generate and operate Objects Using Reflection

The class object can obtain the methods included in the class (represented by the methode object), the constructor (represented by the constructor object), and field (represented by the Field object ), these three classes are defined in Java. lang. under the reflect package, and implements Java. lang. reflect. member interface, the program can use the method object to execute the corresponding method, use the constructor object to call the corresponding constructor to create an object, and can directly access and modify the object's attribute value through the Field object.

There are two ways to generate objects through reflection:

A) use the newinstance () method of the Class Object to create an instance of the class corresponding to the Class Object. This method requires that the corresponding class of the class object has a default constructor, and execute newinstance () the default constructor is used to create instances of this class.

B) first obtain the specified constructor object using the class object, and then call the newinstance () method of the constructor object to create an instance of the class corresponding to the class object, in this way, you can choose to create an instance by using the formulation constructor of a class.

In fact, reflection is considered only when the program needs to dynamically create this object. reflection may be widely used in the development of a widely used framework and basic platform.

After obtaining the class object corresponding to a class, you can use the getmethods () method or getmethod () method of the Class Object () method To obtain all or specify the method-the returned values of these two methods are the method object array or method object.

Each method object contains a method. After obtaining the method object, the program can use this method to call the corresponding method and include an invoke method in the method.

Obejct invoke (Object OBJ, object... (ARGs); OBJ in this method is the main interface for executing this method, and ARGs is the real parameter passed in when this method is executed.

When the corresponding method is called through the method invoke method, Java requires the program to have the permission to call this method. If the program needs to call the invoke method of an object, you can call the following method of the method object first:

Setaccessible (Boolean flag): sets the accessible flag of the Flag object to the indicated Boolean value.

True indicates that the Java access permission check should be canceled when the method is used.

Access property value

The getfields () or getfield () method of the object can be used to obtain all fields (attributes) or specified fields included in the class. The field provides the following two methods to access the attributes:

Getxxx (Object OBJ): Get the attribute value of this field of the OBJ object

Setxxx (Object OBJ, xxx Val): sets this field of the OBJ object to the Val value.

You can use these two methods to access all the attributes of a specified object, including the attributes of private access control.

The pre-access check must be canceled through setaccessible (true)

Operation Array

An array class is also provided under the java. Lang. Reflect package. The array object can represent all arrays.

Use reflection to generate JDK dynamic proxy

A proxy class and an invocationhandler interface are provided under the java. Lang. Reflect package of Java. By using this class and interface, JDK dynamic proxy class or dynamic proxy object can be generated.

Proxy provides

Static object newproxyinstance (classloader loader, class [] interfaces, invocationhandler h): directly creates a dynamic proxy object. The implementation class of this proxy object implements the series interfaces specified by interfaces, each method that executes the proxy object is replaced with the invoke method that executes the invocationhandler object.

Generally, when we use a proxy to generate a dynamic proxy, a dynamic proxy is usually generated for the specified target object.

This kind of dynamic proxy is called the AOP proxy in AOP (Aspect Orient program, that is, Aspect-Oriented Programming). The AOP proxy can replace the target object. The AOP proxy contains all the methods of the target object, however, the methods in the AOP proxy are different from those in the target object: The methods in the AOP proxy can insert some general processing before executing the target method.

Related Article

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.