Java Reflection Concepts and class reflection
First, the concept of reflection:
The concept of reflection was first proposed by Smith in 1982, mainly referring to the ability of a program to access, detect, and modify its own state or behavior. The idea of this concept soon triggered a study of the application of reflection in the field of computer science. It was first adopted in the field of programming language design, and achieved results in Lisp and object-oriented aspects. Among them, lead/lead++, openc++, Metaxa and Openjava are language based on reflection mechanism. Recently, the reflection mechanism has also been applied to Windows, operating systems, and file systems.
Reflection is not a new concept in itself, it may remind us of the concept of reflection in optics, although computer science gives a new meaning to the concept of reflection, but there are certain similarities in the phenomena that help us understand. In the field of computer science, reflection refers to a class of applications that can be self-describing and self controlled. In other words, such applications use a mechanism to implement descriptions of their actions (self-representation) and monitoring (examination), and to adjust or modify the state and associated semantics of the behavior described by the application according to the State and the outcome of its behavior. It can be seen that, compared with the general concept of reflection, reflection in the field of computer science does not merely mean the reflection itself, but also the measures taken to reflect the results. All systems with reflective mechanisms (i.e., reflection systems) want to make the system more open. It can be said that the system which realizes the reflection mechanism has the openness, but the open system does not necessarily adopt the reflection mechanism, the openness is the necessary condition of the reflective system. In general, the reflective system must satisfy the causal connection (causally-connected) In addition to satisfying the open conditions. The so-called causal connection refers to the reflection system self-describing changes can immediately reflect the actual state and behavior of the system at the bottom of the situation, and vice versa. Open and causal connections are two basic elements of a reflective system.
In Java, Reflection is a powerful tool. It enables you to create flexible code that can be assembled at run time without the need for source-representative links between components. Reflection allows us to enable our program code to access the internal information of classes loaded into the JVM, rather than the code of the classes that are selected in the source code, when writing and executing. This makes reflection a major tool for building flexible applications. However, it is important to note that if used improperly, the cost of reflection is high.
Second, class reflection and attribute reflection in Java:
Reflection:
* Reflection: The process of the constructor
* Reflection: The process of parsing member methods
* Reflection: The process of resolving member properties
The main methods in 1.1 Java class reflection
For any of the following three types of components-constructors, fields, and methods-Java.lang.Class provides four separate reflection invocations to obtain information in different ways. Calls follow a standard format. The following is a set of reflection calls to find constructors:
Constructor GetConstructor (class[] params)--Gets a public constructor that uses a particular parameter type.
Constructor[] GetConstructors ()--get all public constructors for a class
Constructor Getdeclaredconstructor (class[] params)--get a constructor that uses a specific parameter type (regardless of the access level)
Constructor[] Getdeclaredconstructors ()--gets all constructors of the class (regardless of access level)
Class reflection calls to get field information are different from those used to access constructors, and field names are used in an array of parameter types:
Field GetField (String name)--Get a named public field
Field[] GetFields ()--Get all public fields of a class
Field Getdeclaredfield (String name)--Gets the named field declared by the class
Field[] Getdeclaredfields ()--Get all the fields declared by the class
To get the method information function:
Method GetMethod (String name, class[] params)--use a specific parameter type to get the named public methods
Method[] GetMethods ()--Get all the public methods of a class
Method Getdeclaredmethod (String name, class[] params)--Methods for naming a class declaration using the parameter type of a feature
Method[] Getdeclaredmethods ()--all methods of obtaining a class declaration
1.2 Start using Reflection:
Classes used for reflection, such as method, can be found in the Java.lang.relfect package. There are three steps you must follow to use these classes: The first step is to get the Java.lang.Class object of the class you want to manipulate. In a running Java program, use the Java.lang.Class class to describe classes and interfaces.
1, Load Class
Here's one way to get a Class object:
Class C = class.forname ("cn.csdn.web.Student");
This statement gets the class object of a String class.
There is another way, such as the following statement:
Class C = int.class;
Or
Class C = integer.type;
2, create the instance object
Student stu= (Student) cls.newinstance ();
3, Analytic class
Methods Md=cls.getmethod ("Getsum", null);//(method name, parameter name)
4. To implement
Object Obj=md.invoke (stu,null);//Assignment
Source:
public void Test () {
1 Load Class
Class cls = Class.forName ("cn.csdn.web.test.Student");
2, create the instance object
Student stu= (Student) cls.newinstance ();
3, Analytic class
Methods Md=cls.getmethod ("Getsum", null);//(method name, parameter name)
4, implementation
Object Obj=md.invoke (stu,null);//Assignment
System.out.println (obj);
}
Attention:
The process of parsing classes can be divided into the following
1, resolving a constructor without parameters
public void Test () {
1 Load Class
Class cls = Class.forName ("cn.csdn.web.test.Student");
2, create the instance object
Student stu= (Student) cls.newinstance ();
3, parse class without parameter constructor
Methods Md=cls.getmethod ("Getsum", null);//(method name, parameter name)
4, implementation
Object Obj=md.invoke (stu,null);//Assignment
System.out.println (obj);
}
2, to resolve the constructor with parameters
Load class
Class cls = Class.forName ("cn.csdn.web.test.Student");
Parsing with a constructor with parameters
Constructor constructor = Cls.getconstructor (String.class);
To create an instance of a class
Student entity = (Student) constructor.newinstance ("Wangli");
Object Invocation Method
Entity.stady ();
System.out.println (Entity.getname ());
System.out.println (Entity.getage ());
3, parsing the constructor with array parameters
Load class
Class cls = Class.forName ("cn.csdn.web.test.Student");
Resolving by constructors with array parameters
Constructor constructor =cls.getconstructor (String[].class);
String[] stu=new string[]{"11", "22"};
Student entity = (Student) constructor.newinstance ((Object) Stu);
Entity.stady ();
4, parsing two-parameter constructors
Class cls = Class.forName ("cn.csdn.web.test.Student");