In general, the reflection mechanism in Java maps various components in Java classes into corresponding Java classes.
For Java programs, using the reflection mechanism will lead to a decline in program performance.
We know that each Java class can be represented by a class object. The components of a class include member variables, methods, and constructor methods, the packet structure and other information are also represented by categories, just like a car is a class, the components of a car: the engine, gearbox, chassis, and so on can also be a class. Indicates that the class of the Java class obviously needs to provide a series of methods to obtain information such as variables, methods, constructor, modifier, and Package Structure in the class. This information also uses the corresponding Java; class, such as field, method, constructor, and package.
To understand the reflection mechanism, you must be familiar with reflection-related classes in Java APIs.
# First take a look at the constructor class
Constructor represents a constructor in a class:
1. Get all the constructor methods of a class:
Example: constructor [] constructors = Class. forname ("Java. Lang. String"). getconstructors ();
2. Obtain a constructor:
Example: constructor = Class. forname ("Java. Lang. String"). getconstructor (stringbuffer. Class );
3. Create an instance object:
Common method: String STR = new string (New stringbuffer ("ABC "));
Reflection Method: String STR = (string) constructor. newinstance (New stringbuffer ("ABC ");
4. Compared with 3, the class also has non-static method newinstance ()
Example: String STR = (string) class. forname ("Java. Lang. String"). newinstance ();
This method first obtains the default constructor object (without parameters), and then creates an Instance Object using this constructor method.
This method uses a caching mechanism internally to save the Instance Object (constructor object without parameters) of the default constructor method ).
# Then the Field Class
The field class represents a member variable in a class.
The field instance object obtained by the getfield (name) method is irrelevant to the specific object of the class and only indicates the domain of the class.
# Then the method class
The method class represents a member method in a class.
1. Obtain a method in the class.
Example: Method methodcharat = string. Class. getmethod ("charat", Int. Class );
2. Call Method
Common method: system. Out. println (Str. charat (1 ));
Reflection Method: system. Out. println (methodcharat. Invoke (STR, 1 ));
If the first parameter of the invoke method passed to the method object is null, it indicates that the method object corresponds to a static method.
3. Differences between the invoke method of JDK 1.4 and JDK 1.5
JDK 1.4: Public object invoke (Object OBJ, object [] ARGs)
JDK 1.5: Public object invoke (Object OBJ, object... ARGs)
JDK 1.5 is compatible with JDK 1.4 syntax, so the reflected code page in 2 can be rewritten to JDK 1.4 in the form of methodcharat. Invoke (STR, new object [] {1 });