Principles of reflection mechanism in Java

Source: Internet
Author: User

Reflection and reflection, happy programmers!

The reflection mechanism in Java is still widely used. The flexibility and scalability of the system are mostly loaded by reflection and other methods. This decouples the system from the plug-in and adds functions. However, many people only use it, but do not know its implementation mechanism. Today I will show you the secrets of the reflection mechanism.

In Java, class. forname (classname) is used to reflect classes.

Package COM. java. reflecttest; import COM. java. dbtest. dbtest;/*** Java reflection mechanism test * @ author longxuan **/public class reflecttest {/*** test reflection class */public static void reftest () {string classname = "com. java. dbtest. testconnection "; dbtest = NULL; try {// Through the reflection mechanism, the class loader is used to load the class Tc = Class. forname (classname); // class system obtained by output reflection. out. println (TC); // create an instance of this class and convert it to interface dbtest = (dbtest) TC. newinstance (); // call the dbtest method of this class through the interface. selectuser ();} catch (classnotfoundexception e) {e. printstacktrace ();} catch (instantiationexception e) {e. printstacktrace ();} catch (illegalaccessexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {reftest ();}}

The main function has been debugged.

 

After debugging and checking the information, I seem to have understood some of them based on my own speculation and understanding. We will share with you the discussion.

Let's talk about the execution process first:

The class. forname (classname) method actually calls the class. forname (classname, true, currentloader) method in the class. Parameter: name-the fully qualified name of the required class; initialize-whether the class must be initialized; loader-the Class Loader used to load the class. Currentloader obtains the current class loader by calling classloader. getcallerclassloader. To use the class, you must use the class loader. The reflection mechanism provides a cache instead of re-reflection every time. Each time, the Class Loader needs to search for it in its own cache. If it can be found, the class is directly returned.


What's interesting is that Java classloaders also have some tricks. It can be divided into Bootstrap class loader, extensions class loader, app classloader (or System
Class Loader), of course, custom classloader (user-defined class loader ). During the loading process, the system first checks whether the class has been loaded. The check sequence is from bottom to top. From custom classloader to bootstrap classloader, this class is regarded as loaded as long as a classloader has been loaded, make sure that this class is only loaded once by all classloader. The order of loading is from top to bottom, that is, the upper layer tries to load this type. A detailed introduction to the class loader will be analyzed in the following blog. You are welcome to look forward to it.


In the forname method, the classloader. loadclass method is called to complete class reflection. Based on the special nature of the class loader and my debugging process, I drew a simple flowchart,



This figure shows the class loading process of the class loader. Check whether the class has been loaded. If the class has been loaded, the class is directly returned. If not, the loadclass method of the parent class is called. If the parent class does not exist, then execute the findclass method to try to load this class, which is the one-sided "reflection" We generally understand. This process is mainly completed through the classloader. defineclass method. The defineclass method converts a byte array
Class instance (any class object is a class object ). Class. newinstance must be used to create the instance of this newly defined class, but new cannot be used for instantiation.

 

Why do we say "all class objects are class objects? In Java, each class has a corresponding class object. That is to say, when we compile a class (. Java file), after compilation, a class object will be generated in the generated. Class file to indicate the type information of this class.


In fact, it is simple to say that during the runtime, if we want to generate a class object, the Java Virtual Machine (JVM) will check whether the class object of this type has been loaded. If the file is not loaded, JVM will find the. Class file based on the class name and load it. Once a class object of a certain type has been loaded into the memory, it can be used to generate all objects of this type.


The above content is what I have debugged, checked Java API and online materials, and shared with you my understanding. If there are any mistakes, please let us know and make progress together.

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.