What Java reflection mechanism?

Java reflection mechanism is an important feature of the Java language. Before learning Java reflection, we should first understand the two concepts, compile and run.

It refers to compile the source code to the compiler to process a computer file that can be performed . That is the process of Java code compiled into class files in Java. Compile just did some translation, and did not put the code in memory up and running, but only the code as a text operations, such as checking for errors.

It is to run the compiled files to the computer to perform until the end of the program run . The so-called run-up to put the implementation of the code on the disk into memory.

Java reflection mechanism is in the operating state, for any class, are made known to all properties and methods of this class; for any object, are able to call any of its methods and properties; This dynamic access to information and dynamic method call object the feature is called reflection mechanism of Java language . In simple terms, it refers to the reflection program can obtain their information at run time. In Java, just to name a given class, you can get all the information to the class by reflection.

Java reflection mechanism has been widely used in server and middleware programs. On the server side, the customer often need to request a dynamic invocation of a particular method object. In addition, the realization of ORM middleware, the use of Java reflection can read all the attributes of a JavaBean any, or to the property assignment.
Here Insert Picture Description

Java reflection mechanism mainly provides the following functions, which are located in java.lang.reflect package.

	在运行时判断任意一个对象所属的类。
	
	在运行时构造任意一个类的对象。
	
	在运行时判断任意一个类所具有的成员变量和方法。
	
	在运行时调用任意一个对象的方法。
	
	生成动态代理。

To know the properties and methods of a class, you must first get to the bytecode file object class. When access to information and the like, is used method in class Class. So first acquired corresponding to each object type Class file byte code (.class).

As we all know, all Java classes inherit the Object class that defines a getClass () method in the Object class, which returns the same object of type Class of. For example, the following sample code:

Class labelCls = label1.getClass();    // label1为 JLabel 类的对象

Using the object class can access labelCls Class labelCls object description information, and information of the base class Object JLabel class.

General information accessible reflection
Here Insert Picture Description

As shown above, calling getFields () and getMethods () will be sequentially acquired for the fields and variables public authority when the method then comprising inherits from a superclass to the member variables and methods. Through getDeclareFields () and getDeclareMethod () Gets only member variables and methods defined in this class.

Advantages and disadvantages of Java reflection mechanism

Advantages:
dynamic access can be runtime instances of a class, greatly improved flexibility and scalability of the system.

Combined with Java dynamic compiler can achieve very powerful.

Java language compiler for this first run, and allows us to easily create flexible code that can be assembled at run-time, without the need for links between components in source code, object-oriented easier.

Disadvantages:
reflection will consume some system resources, so if you do not need to dynamically create an object, you do not need reflection;

Reflection can be ignored when calling the method permission checks, access to private methods and properties of this class, so could undermine the class encapsulation and lead to security problems.

Published 457 original articles · won praise 94 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_45743799/article/details/104727853