The Java reflection mechanism in my understanding is the following points: 1. A given class name (provided as a string) can dynamically construct an object instance 2. For any class, you can know all the properties and methods of this class 3. For any object, it can call any of its methods and properties, the dynamic acquisition of information and the function of the dynamic method of invoking the object is called the Java language reflection mechanism, this reflection mechanism in Java has brought us a great deal of convenience, has been widely used in JavaBean In the reflection of Java we mainly use the following classes, the following will be described individually
Java.lang.Class;
Java.lang.reflect.Constructor;
Java.lang.reflect.Field;
Java.lang.reflect.Method;
Java.lang.reflect.Modifier;
1.java.lang.class class: Used to hold information about the class to which each object belongs, in Java, the GetClass () method in the object class returns an instance of class type (Note: Remember this is a class, And the keyword class are completely different. 2.java.lang.reflect.constructor: Provides information about a single construction method of a class and access to it. 3.java.lang.reflect.field: Provides information about a single field of a class or interface, as well as dynamic access to it 4.java.lang.reflect.method: Provides information about a single method on a class or interface (and how to access the method). The method that is reflected may be a class method or an instance method, including an abstract method. 5.java.lang.reflect.modifier: Provides a class, a member of a class, a static method, and a modifier for a constant.
public class Person {//fieldprivate String person_name;protected int person_age;public boolean person_sex;//CONSTRUCTPU Blic person () {super ();} Public person (String name, int age, Boolean sex) {super (); This.setperson_name (name); this.person_age = Age;this.person_ sex = sex;} methodpublic void Addage (int year) {this.person_age + = This.person_age + year;} Public String Getperson_name () {return person_name;} public void Setperson_name (String person_name) {this.person_name = Person_name;}}
public class Employee extends Person{private double saray;protected String gangwei;public Employee () {super ();} Public Employee (Double Saray, String Gangwei) {super (); This.saray = Saray;this.gangwei = Gangwei;} Public double Getsaray () {return saray;} public void Setsaray (double saray) {this.saray = Saray;} Public String Getgangwei () {return gangwei;} public void Setgangwei (String gangwei) {This.gangwei = Gangwei;} public void Addsaray (double money) {This.saray+=money;}}
Here's how to use it in our usual way:
1. Construct an instance based on the class name
Gets the class object, based on its name, Class C = Class.forName ("Employee"); System.out.println (C.getname ()); Print here: employee//Get Parent Class class object class CP = C.getsuperclass (); System.out.println (Cp.getname ());//Print here: person//gets the access modifier for the class string modifiers = Modifier.tostring (C.getmodifiers ()); System.out.println (modifiers); Print here: public//instantiation of Object obj = C.newinstance (); Instantiation with a parameterless constructor
2. Get the field of the class, the following four methods are mainly
Class C = class.forname ("Employee");
- C.getfield (name);
- C.getfields ()
- C.getdeclaredfields ()
- C.getdeclaredfield (name)
The GetField () method returns the public fields of this class or its superclass (excluding private and protect decorated fields) Getdeclaredfield method returns all fields of this class (including private and protect decorated fields)
<span style= "White-space:pre" ></span>/* * get all field of class employee * * C.getfield (name) C.getfields () C.getdeclaredfields () * C.getdeclaredfield (name) */field[] field = C.getdeclaredfields (); for (Field f:fields) {//Get the FI The type of eld class type = F.gettype ();//Gets the name of the field string name = F.getname ();//Gets the field's access modifier string sfieldmodifiers = Modifier . ToString (F.getmodifiers ()); System.out.println (Sfieldmodifiers + "" + type.getname () + "+ name +"; ");}
3. Gets the constructor of a class as in field, there are 4 ways to get a constructor
- * C.getdeclaredconstructors ()
- * C.getconstructors ()
- * C.getdeclaredconstructor (Parametertypes)
- * C.getdeclaredconstructor (Parametertypes)
<span style= "White-space:pre" ></span>/* * Gets the constructor of the class * c.getdeclaredconstructors () * C.getconstructors () * C.getdeclaredconstructor (parametertypes) * C.getdeclaredconstructor (parameterTypes) * * Constructor[] Constructors = c.getdeclaredconstructors (); for (Constructor cr:constructors) {//constructor name String name = Cr.ge Tname ();//constructor modifier string sconstructormodifiers = Modifier.tostring (Cr.getmodifiers ());//constructor parameter class[] Paramtypes = Cr.getparametertypes (); String SParam = ""; for (int j = 0; J < Paramtypes.length; J + +) {if (J > 0) SParam + = ","; SParam + = Paramtypes[j].getname ();} System.out.println (sconstructormodifiers + "+ name +" ("+ SParam +"); ");
4. Get the method of the class as field, there are 4 methods to get the method, and one more method to get the return type
<span style= "White-space:pre" >method[] methods = C.getdeclaredmethods (); <span style= "White-space:pre" > </span>for (method M:methods) {<span style= "White-space:pre" ></span>//method return type <span style= " White-space:pre "></span>class returntype = M.getreturntype (); <span style=" White-space:pre "></ Span>string Sreturntype = returntype.tostring () <span style= "White-space:pre" ></span>//method name <span Style= "White-space:pre" ></span>string name = M.getname (); <span style= "White-space:pre" ></span >//method modifier <span style= "White-space:pre" ></span>string smethodmodifiers = modifier.tostring ( M.getmodifiers ()); <span style= "White-space:pre" ></span>//method parameters <span style= "White-space:pre" > </span>class[] Paramtypes = M.getparametertypes (); <span style= "White-space:pre" ></span>String SParam = ""; <span style= "White-space:pre" ></span>for (int j = 0; J < Paramtypes.length; J + +){<span style= "White-space:pre" ></span>if (J > 0) <span style= "White-space:pre" ></span> SParam + = ","; <span style= "White-space:pre" ></span>sparam + = Paramtypes[j].getname (); <span style= " White-space:pre "></span>}<span style=" White-space:pre "></span>system.out.println ( Smethodmodifiers + "+ Sreturntype +" "+ Name +" ("+ SParam +"); "); <span style= "White-space:pre" ></span>}</span>
5. Use reflection Dynamics to assign values to object properties, such as we need to read an employee's information from a configuration file and assign it to an object, we can write:
<span style= "White-space:pre" ></span>/*<span style= "White-space:pre" ></span> * Dynamically assigning values to class properties <span style= "White-space:pre" ></span> */<span style= "White-space:pre" ></span>class c2 = Class.forName ("Employee"); <span style= "White-space:pre" ></span>object e = C2.newinstance (); <span Style= "White-space:pre" ></span>field f = C2.getdeclaredfield ("Saray"); <span style= "White-space:pre" ></span>//because the property Saray is private, all calls to the Setaccessible method are required to assign a value to the property, otherwise the error <span style= "White-space:pre" > </span>f.setaccessible (true); <span style= "White-space:pre" ></span>f.set (e, 10000); <span Style= "White-space:pre" ></span>field F2 = C2.getdeclaredfield ("Gangwei"); <span style= "White-space:pre" ></span>f2.set (E, "Senior Software Engineer"); <span style= "White-space:pre" ></span>system.out.println (( Employee) e). Getgangwei () + "" + ((Employee) e). Getsaray ()); <span style= "White-space:pre" ></span>//Value <span style= "White-space:pre" ></span>field F3 = C2.getdeclaredfield ("Saray"); <span style= " White-space:pre "></span>field f4 = C2.getdeclaredfield (" Gangwei "); <span style=" White-space:pre "> </span>f3.setaccessible (true); <span style= "White-space:pre" ></span>system.out.println (F3.get ( e)); <span style= "White-space:pre" ></span>system.out.println (F4.get (e));
The output results are as follows:
Senior Software
engineer 10000.010000.0 Senior software engineer
6. Methods of dynamically invoking objects using reflection, such as a Addsaray method in the employee class, we look at how to invoke:
Class C3 = Class.forName ("Employee"), Object E3 = C3.newinstance (); Field Fsaray = C3.getdeclaredfield ("Saray"); Fsaray.setaccessible (true); Fsaray.set (E3, 10000); System.out.println (Fsaray.get (E3)); Output 10000//get Methodmethod m = C3.getdeclaredmethod ("Addsaray", double.class);/* * Method m = C3.getdeclaredmethod (" Addsaray "); Method m2 = * C3.getdeclaredmethod ("Addsaray", Double.class); * If there are multiple methods with the same name, to add parameters to let the compiler know which method should be called, there is only one parameter, so the above two methods can be *///call Method M.invoke (E3, 5000); System.out.println (Fsaray.get (E3)); Output 15000, so the method call proves successful
The output results are as follows:
10000.015000.0
Java Reflection Practice