What is the reflection mechanism?
The reflection mechanism is in the running state, for any class, all the properties and methods of the class can be known, and any one of its methods and properties can be invoked for any object; This dynamically acquired information, and the function of the method that dynamically invokes the object, is called the reflection mechanism of the Java language.
* * What the reflex mechanism can do
**
The reflection mechanism provides the following functions: Determining the class that any object belongs to at run time, constructing an object of any class at run time, determining the member variables and methods of any class at run time, and invoking the method of any object at run time; generating dynamic proxies.
reflection mechanism get class there are three ways we can get the employee type
The first way:
CLASSC1 = Class.forName ("Employee");
The second way:
each type in//java has a class attribute.
CLASSC2 = Employee.class;
The Third way:
any Java object in the//java language has a getclass method
employeee = new Employee ();
CLASSC3 = E.getclass (); C3 is the Run-time class (The Run-time class of E is Employee)
get methods, and construct methods, no longer described in detail, just look at the keyword: getdeclaredmethods () Get all the Methods Getreturntype () Get the return type of the method Getparametertypes () Gets the passed-in parameter type Getdeclaredmethod ("Method name", Parameter type. class,......) to obtain a specific method getdeclaredconstructors () Get all the construction methods Getdeclaredconstructor (parameter type. class,......) Get a specific construction method
In addition: Getdeclaredfields () obtains all the declared fields of a class, that is, public, private, and proteced, but does not include the declaration field of the parent class.
GetFields () Gets all of the common (public) fields of a class, including the parent class.
The following are examples of use:
Package Com.app;
public class T1 {public
static void Main (string[] args) {
try {
//Create class
class<?> Class1 = Class.forna Me ("Com.app.Person");;
To create an instantiation: the equivalent of new an object
= Class1.newinstance ();
Downward transition person person
= (person) object;
Person.setid ("the");
Person.setname ("Jack");
SYSTEM.OUT.PRINTLN ("ID:" + person.getid () + "name:" + person.getname () );
} catch (ClassNotFoundException e) {
e.printstacktrace ();
} catch (Instantiationexception e) {
E.printstacktrace ();
} catch (Illegalaccessexception e) {
e.printstacktrace ();}}}
Run Result:
id:100 Name:jack
Example two:
Package Com.app;
Import Java.lang.reflect.Field; public class T1 {public static void main (string[] args) {try {//Create class Class<?> ;
Class1 = Class.forName ("Com.app.Person");
Create instance Object person = class1.newinstance ();
Gets the id attribute Field IDfield = Class1.getdeclaredfield ("id"); Breaking encapsulation In fact, Setaccessible is a switch that enables and disables access to security checks, and is not accessible as false if it is not true (or because the JDK security check is time-consuming). So pass setaccessible (true) in the way
Closed security checks can achieve the purpose of idfield.setaccessible (true) to enhance the reflection speed;
Assign a value to the id attribute idfield.set (person, "100");
Print the property value of person System.out.println (idfield.get);
catch (Instantiationexception e) {e.printstacktrace ();
catch (Illegalaccessexception e) {e.printstacktrace ();
catch (Nosuchfieldexception e) {e.printstacktrace ();
catch (SecurityException e) { E.printstacktrace ();
catch (ClassNotFoundException e) {e.printstacktrace (); }
}
}
Run results
100
In addition, the class class provides four public methods for getting the construction method of a class.
Constructor GetConstructor (class[] params) returns a specific constructor with the public property, based on the parameters of the constructor
constructor GetConstructors () returns an array
of all constructors with public properties Constructor Getdeclaredconstructor (class[] params) returns a specific constructor (without public and Non-public properties) based on the parameters of the constructor
constructor Getdeclaredconstructors () returns all the constructor arrays in the class (without public and non-public properties)
Four ways to get member methods
Methods GetMethod (String name, class[] params) returns a specific method with public properties based on the method name and Parameters
method[] GetMethods () Returns all methods array with public properties method
Getdeclaredmethod (String name, class[] params) based on the method name and parameter, Returns a specific method (without public and Non-public properties)
method[] Getdeclaredmethods () returns all the method arrays in the class (without public and non-public properties)
Four ways to get member properties
field GetField (String name) returns a specific member variable with the public property, field[] GetFields (), based on the variable name, returns a The array Field Getdeclaredfield (String name) of the member variable returns a member variable (without public and non-public properties) field[] Getdelcaredfield (), based on the variable name, to return all the member variables. Array (without public and non-public properties)