The reflection mechanism is one of the dynamics of Java, and when it comes to dynamic, we need to understand dynamic language. So what is dynamic language?
First, dynamic language
Dynamic language means that a program can change its structure at run time: New functions can be introduced, existing functions can be deleted, and other structural changes. For example, the common JavaScript is dynamic language, in addition to Ruby,python and so on is also a dynamic language, and C, C + + is not a dynamic language.
Second, is Java a dynamic language?
From the dynamic language can change the structure of the program at run time or variable type, Java and C, C + + are not dynamic language.
But Java has a very prominent dynamic-related mechanism: the reflection mechanism. Through the reflection mechanism, Java can load, detect, and use classes that are completely unknown during compilation, and can generate instances of related class objects so that it can invoke its methods or change a property value. so Java can also be considered a semi-dynamic language .
Third, reflection mechanism:
1. Reflection Mechanism Concept
The reflection mechanism in Java means that in the running state, all the properties and methods of the class can be known to any one class, and any one of them can be called any one of its methods; This dynamic acquisition of information and the ability to dynamically invoke object methods become the reflection mechanism of the Java language.
2. Reflective Applications
In Java programs, there are two types of objects running: compile-time type and run-time type.
The type at compile time is determined by the type that is useful when declaring the object, and the type of the runtime is determined by the type that is actually assigned to the object
Such as:
Person p=New Student ();
Where the compile-time type is person and the run-time type is student.
In addition , the program may also receive externally-passed objects at run time, which have a compile-time type of object, but the program has methods that need to invoke the runtime type of the object. To solve these problems, the program needs to discover real information about objects and classes at run time. However, if the compiler cannot predict at all what classes the object and class belong to, the program can only rely on run-time information to discover the real information about the object and the class, which must be used to reflect.
Iv. Java Reflection API
The reflection API is used to generate information about classes, interfaces, or objects in the JVM.
- Class: Reflection of the core class, you can get the properties of the class, methods and other information.
- Field Class : A class in a Java.lang.reflec package that represents a member variable of a class that can be used to get and set property values within a class.
-Method Class : A class in the Java.lang.reflec package that represents the class's methods, which can be used to obtain method information or execute methods in a class.
- Constructor Class : A class in a Java.lang.reflec package that represents the constructor method of a class.
V. Steps to use reflection
1. Steps
- Gets the class object of the classes you want to manipulate
- Calling Methods in class classes
- Use the reflection API to manipulate this information
2. How to get a class object
- Call the GetClass () method of an object
Person p=New person (); Class clazz=p.getclass ();
- Call the class property of a category to get the class object
Class Clazz=person. class;
- Use the forname () static method in class classes; (Safest/Best performance)
Class Clazz=class.forname ("Full path of the class"); (most commonly used)
3. Get method and property information
Once we have the class object for the classes we want to manipulate, we can get and view the methods and properties in the class by means of the methods in class.
Example code :
<<<<<<<<<<<<<<<< using Reflection <<<<<<<<<< <<<<<<<<< Packagereflection;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;/** Get the member methods and properties of the class by using the full path of the user input class * declared get all private and public * 1. Gets the class object that accesses classes * 2. Methods that call class objects return method and property information for accessing classes*/ Public classTest { Public Static voidMain (string[] args) {Try { //get the class object for the person classClass clazz=class.forname ("Reflection. Person "); //get all method information for the person classMethod[] Method=Clazz.getdeclaredmethods (); for(Method m:method) {System.out.println (m.tostring ()); } //get all member property information for the person classField[] Field=Clazz.getdeclaredfields (); for(Field F:field) {System.out.println (f.tostring ()); } //Get all Construction method information for the person classConstructor[] Constructor=clazz.getdeclaredconstructors (); for(Constructor c:constructor) {System.out.println (c.tostring ()); } } Catch(Exception e) {e.printstacktrace (); } }}
Output Result:
method Information:
Public java.lang.String Reflection. Person.tostring ()
Private java.lang.String reflection. Person.getname ()
private void Reflection. Person.setname (java.lang.String)
public void Reflection. Person.setage (int)
public int Reflection. Person.getage ()
Public java.lang.String Reflection. Person.getgender ()
public void Reflection. Person.setgender (java.lang.String)
Property Information:
Private java.lang.String reflection. Person.name
Private java.lang.String reflection. Person.gender
private int Reflection. Person.age
Construction Method Information
Private reflection. Person ()
Public reflection. Person (Java.lang.string,java.lang.string,int)
4. Create an Object
When we get the class object of the required classes, we can use it to create objects, there are two ways to create objects:
- Use the newinstance () method of the class object to create an instance of the class object counterpart, but this method requires that the class object has a default empty constructor.
- Use the class object first to get the specified Constructor object, and then call the Constructor object's newinstance () method to create an instance of the class object corresponding This method allows you to select a construction method to create an instance.
Example code:
Packagereflection;ImportJava.lang.reflect.Constructor; Public classDemo01 { Public Static voidMain (string[] args) {Try { //get the class object for the person classClass clazz=class.forname ("Reflection. Person "); /*** The first method of creating an object*/ //Creating ObjectsPerson p=(person) clazz.newinstance (); //Setting PropertiesP.setname ("Zhang San"); P.setage (16); P.setgender (Male); System.out.println (P.tostring ()); /*** The second method of creating*/ //Get Construction MethodConstructor C=clazz.getdeclaredconstructor (String.class, String.class,int.class); //creating objects and setting propertiesPerson p1= (person) c.newinstance ("John Doe", "male", 20); System.out.println (P1.tostring ()); } Catch(Exception e) {e.printstacktrace (); } }}
Output Result:
Name: Zhang San Sex: male Age: 16
Name: John Doe Sex: Male Age: 20
Article reference: Hansen X
Reflection of the Advanced Java (Reflection mechanism)--reflection Concept and foundation