Value of the Action object property (field)
You already know how to view the data field name and type of any object. When writing a program, it is easy to look at the domain name and type, and the reflection mechanism can see the object fields that are unclear at compile time.
Get the domain (property) value using the Get method
the key method of viewing object domain is field of the
class get the
method. If f is an object of Type field, and obj is an object of a class that contains an F field, F.get (obj) returns an object with the type of object, whose value is the current value of the obj field. Example:
Packagecom.xiaoxiaoyihan.reflection;ImportJava.lang.reflect.Field;classPerson {PrivateString name; PublicPerson (String name) { This. Name =name; }} Public classReflectionDemo2 { Public Static voidMain (string[] args)throwsNosuchfieldexception, illegalaccessexception {person P=NewPerson ("The Rustle of the cold"); Class CLZ=P.getclass (); Field F= Clz.getdeclaredfield ("name"); //f.setaccessible (true);Object name =F.get (P); SYSTEM.OUT.PRINTLN (name); }}
"Run Results":
Exception in thread "main" Java.lang.IllegalAccessException:Class Com.xiaoxiaoyihan.reflection.ReflectionDemo2 can Not access a member of class Com.xiaoxiaoyihan.reflection.Person with modifiers "private"
At Sun.reflect.Reflection.ensureMemberAccess (reflection.java:109)
......
because the default behavior of the reflection mechanism is limited by the Java access Control . If a Java program is not limited by the security Manager's control, access control can be overridden. The Field,method or constructor object provides setaccessible
method is used to override access control.
get
method also needs to solve another problem, the return value of the Get method is of type object, the name above is of type string, and the string type value is assigned to the object, but what if a double type of field appears? Numeric types in Java are not objects. You can use the field classgetDouble
method, or you can call theget
The method then enforces the type conversion. The reflection mechanism automatically packages this field value into the appropriate object wrapper, and for the double type, it is packaged as a double.
Packagecom.xiaoxiaoyihan.reflection;ImportJava.lang.reflect.Field;classPerson {Private Doublesalary; PublicPerson (Doublesalary) { This. Salary =salary; }} Public classReflectionDemo2 { Public Static voidMain (string[] args)throwsNosuchfieldexception, illegalaccessexception {person P=NewPerson (100); Class CLZ=P.getclass (); Field F= Clz.getdeclaredfield ("Salary"); F.setaccessible (true);//Double salary = (double) f.get (p); DoubleSalary =f.getdouble (P); System.out.println ("Salary:" +salary); }}
"Run Results":
Salary: 100.0
Set the domain (property) value using the Set method
Of course, you can also use the Set method to set a new value on the F field of the Obj object. The signature of the Set method is:
void Set (obj, value) // obj: object of operation; value: New Value
Example:
Packagecom.xiaoxiaoyihan.reflection;ImportJava.lang.reflect.Field;classPerson {PrivateString name; Private Doublesalary; PublicPerson (String name,Doublesalary) { This. Name =name; This. Salary =salary; } Public Doublegetsalary () {returnsalary; } PublicString GetName () {returnname; }} Public classReflectionDemo2 { Public Static voidMain (string[] args)throwsNosuchfieldexception, illegalaccessexception {person P=NewPerson ("Zhang San", 100); System.out.println (P.getname ()+ "-----" +p.getsalary ()); Class CLZ= person.class; Field F= Clz.getdeclaredfield ("name"); F.setaccessible (true); F.set (P,"The Rustle of the cold"); F= Clz.getdeclaredfield ("Salary"); F.setaccessible (true); F.set (P,200); System.out.println (P.getname ()+ "-----" +p.getsalary ()); }}
Call any method Invoke method
Similar to the field class's Get method to view the fields of an object, the method class provides an Invoke method that allows you to invoke methods that are wrapped in the current method object. The signature of the Invoke method is:
Object Invoke (Object obj, Object...args)
The first parameter is an implicit argument, and the remaining objects provide the display parameters.
For example, M1 represents the GetName method for person, so the name is obtained by means of the invoke
form:
String n = m1.invoke (p);
if the base type is returned, the Invoke method returns its wrapper type. For example, M2 represents the Getsalary method, and the returned object is actually a double that must respond to the full type conversion.
double s = M2.invoke (p);
Based on the Java Reflection Summary (1): The description in the parsing class structure, you can Class
get an array of objects through the static method of the class, getDeclaredMethods
Method
and then iterate over the returned array to find the specified method. getField
similar to methods, getField
methods return a Field object by means of a string representing the domain name. However, because of overloaded methods, it is possible to obtain several methods of the same name. Therefore, you must also provide the parameter type of the method. Signature of the GetMethod method:
Method GetMethod (String name, class...paramtypes)
Example:
Packagecom.xiaoxiaoyihan.reflection;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;classPerson {PrivateString name; Private Doublesalary; PublicString GetName () {returnname; } Public Doublegetsalary () {returnsalary; } Public voidRaisesalary (Doubleraise) {Salary+=raise; } PublicPerson (String name,Doublesalary) { This. Name =name; This. Salary =salary; }} Public classReflectionDemo2 { Public Static voidMain (string[] args)throwsnosuchmethodexception, InvocationTargetException, illegalaccessexception {person P=NewPerson ("Rustling the cold", 100); //Get GetName Method ObjectMethod m1 = P.getclass (). GetMethod ("GetName"); //calling the GetName methodString name =(String) M1.invoke (p); System.out.println ("My name is:" +name); //Gets the Raisesalary method object, which needs to pass a parameter of type doubleMethod m2 = P.getclass (). GetMethod ("Raisesalary",Double.class); //call the Raisesalary method and pass in the parameter valueM2.invoke (p, 200); System.out.println ("After the Raise: salary=" +p.getsalary ()); }}
"Run Results":
My name is: The Rustle of the cold
After salary increase: salary=300.0
For a static method, invoke
the first argument can be ignored, and it can be set to null.
Packagecom.xiaoxiaoyihan.reflection;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;classPerson { Public Static voidspeak (String name) {System.out.println (Wood "+ name +", you speak! "); }} Public classReflectionDemo2 { Public Static voidMain (string[] args)throwsnosuchmethodexception, InvocationTargetException, illegalaccessexception {Method m= person.class. GetMethod ("Speak", String.class); M.invoke (NULL, "The Rustle of the cold"); }}
"Run Results":
Wood! The rustle of the cold, you speak Ah!
Note:the parameter and return value of invoke must be of type object. This means that the type conversion must be done several times. Doing so will be an opportunity for the compiler to miss the code. In addition, the code that obtains method pointers through reflection is less efficient than calling methods directly.
Java Reflection Summary (2): Application of Reflection