Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the JAVA reflection mechanism do?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "what does the JAVA reflection mechanism do?". Friends who are interested may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what does the JAVA reflection mechanism do?"

1. What is reflection?

A very important concept in java development is the java reflection mechanism, which is also one of the important features of java.

The concept of reflection was first put forward by Smith in 1982. It mainly refers to the ability of a program to access, detect and modify its own state or behavior. Private methods and private properties can be called through reflection. Most frameworks also use the principle of reflection.

Reflection (reflection) is regarded as the key to dynamic languages, and the reflection mechanism allows programs to obtain anything with the help of Reflection API during execution.

Class, and can directly manipulate the internal properties and methods of any object.

A class has multiple components, such as member variables, methods, constructors, etc. Reflection is to load the class and dissect each component of the class.

2. What can reflection do?

The reflection mechanism of Java knows the basic structure of the class, and this ability to detect the structure of the Java class is called the "self-examination" of the Java class. For example, in eclipse, as soon as you click on it, the compiler tool will automatically list all the methods and properties that can be used by the object for the user to choose. This is the use of the principle of Java reflection, which is the exploration and self-examination of the objects we create.

Reflection can do this:

Determine the class to which any object belongs at run time

Construct an object of any class at run time

Determine the member variables and methods of any class at run time

Get generic information at run time

Call member variables and methods of any object at run time

In operation is to process annotations

Generate dynamic proxy

3. Reflection-related API

Java.lang.Class: the source of reflection

Java.lang.reflect.Method: method

Java.lang.reflect.Field: attribute

Java.lang.reflect.Constructor: constructor

...

4. Understanding of Class (1), introduction:

After the javac.exe command, the program generates one or more bytecode files (at the end of .class). Then we use the java.exe command to interpret and run a bytecode file. It is equivalent to loading a bytecode file into memory. This process is called class loading. A class that is loaded into memory is called a runtime class, and this runtime class is used as an instance of Class.

In other words, an instance of Class corresponds to a runtime class.

Runtime classes loaded into memory are cached for a certain amount of time. During this time, we can do it in different ways.

To get this runtime class.

(2) the loading process of the class: loading:

When we new the object or use Class.forName ("package name. Class"), the class loader (ClassLoader) loads the class into memory and creates a Class object

How do I get a Class object?

Class. Class

Object .getClass ()

Class.forName ("package name. Class")

Link:

The main work of the link is to verify whether the bytecode is legal, allocate memory space for the static and initialize it (not really initialization, but just give the corresponding type of variable to the default value, such as int to zero double to 0.0)

Uninstall:

Uninstall from memory (don't need us to worry about when to uninstall, let JVM do it)

(3) Class loader

The class (CLASS) cannot be run until it is loaded into the JVM. When running a specified program, JVM loads the compiled .class files into memory according to requirements and certain rules, and organizes them into a complete Java application. This loading process is done by the classloader, specifically, by ClassLoader and its subclasses. The class loader itself is also a class, and its essence is to read the class file from the hard disk to memory!

Classification of class loaders:

BootStrap: mainly responsible for loading core class libraries (java.lang.*, etc.), constructing ExtClassLoader and APPClassLoader

ExtClassLoader: mainly responsible for loading some extended jar packages in the jre/lib/ext directory

AppClassLoader: mainly responsible for loading the main function class of the application (the java file written by yourself is loaded by this class loader)

System.out.println ("app:" + System.getProperty ("java.class.path")); System.out.println ("ext:" + System.getProperty ("java.ext.dirs")); System.out.println ("- bootstrap---"); String [] str = System.getProperty ("sun.boot.class.path"). Split (";"); for (String s: str) {System.out.println (s);}

Parental entrustment (dispatch) mechanism:

When a file like Hello.class is about to be loaded. Regardless of our custom classloader, we will first check in AppClassLoader to see if it has been loaded, and if so, there is no need to load it again. If not, the parent loader is taken and the loadClass method of the parent loader is called. The same thing in the parent class will first check whether it has been loaded, if not further up. Note that this similar recursive process checks to see if it has been loaded until you reach Bootstrap classLoader, and does not choose to load it yourself. Until BootstrapClassLoader, there is no parent loader, and then we start to think about whether we can load it or not. If we can't load it, we will sink to the child loader to load it. If there is no loader to load, we will throw a ClassNotFoundException. So some people have the following questions?

Why design this mechanism?

One advantage of this design is that if someone wants to replace the system-level class: String.java. Tamper with its implementation, under which the classes of these systems have already been loaded by Bootstrap classLoader (why? Because when a class needs to load, the first to try to load is BootstrapClassLoader), so other class loaders do not have the opportunity to load again, preventing the implantation of dangerous code to some extent.

At this point, I believe you have a deeper understanding of "what the JAVA reflection mechanism does". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report