1. The Field object provides the following methods for setting, getting the value of an object property:
public void Set(Object obj,object value)
public Object get (Object obj)
Case Study:
packageCom.java.reflect;
importJava.lang.reflect.Field;
importorg.junit.Test;
Publicclass Demo4 {
@Test
publicvoid test1 ()throws classnotfoundexception,nosuchfieldexception, SecurityException, illegalargumentexception,illegalaccessexception {
Person p = new person ();
Class C = class.forname ("Com.java.reflect.Person");
Field f = C.getfield ("Sex");
System.out.println (F.get (P) + "..." +f.gettype ());
}
@Test
publicvoid test2 ()throws classnotfoundexception,nosuchfieldexception, SecurityException, illegalargumentexception,illegalaccessexception {
Person p = new person ();
Class C = class.forname ("Com.java.reflect.Person");
Field f = C.getfield ("Sex");
F.set (P, "NV");
System.out.println (F.get (p));
}
}
2. Get the information instance operation of the class
packageCom.java.reflect;
importjava.lang.reflect.Modifier;
importorg.junit.Test;
Publicclass Demo5 {
@Test
publicvoid test1 ()throws ClassNotFoundException {
Class C = class.forname ("Com.java.reflect.Person");
System.out.println (C.getpackage ()); Gets information about the package where the entity of this class object corresponds
System.out.println (C.getpackage (). GetName ());//Get the full name of the package that contains the class
int mod = c.getmodifiers ();
SYSTEM.OUT.PRINTLN (mod); Gets the class modifier represented by an integer for the entity corresponding to this class object
String m = modifier.tostring (mod);
System.out.println (m);//convert to corresponding string
System.out.println (C.getname ()); Get fully qualified name
System.out.println (C.getsuperclass ());//Get the class object of the direct parent class of the entity corresponding to this class object
System.out.println (C.getinterfaces ());//Get the interface object for the entity that this class object corresponds to
}
}
3. Differences between the invoke methods of jdk1.4 and jdk1.5:
A) jdk1.5:public Objectinvoke (Object obj,object ... args)
b) jdk1.4:public Objectinvoke (Object obj,object[] args),
question:
The parameter of the main method that starts the Java program is an array of strings, that is, public static voidmain (string[] args), how to pass arguments to the Invoke method when calling this main method by reflection. By jdk1.5 's syntax, the entire array is a parameter, and by the jdk1.4 syntax, each element in the array corresponds to a parameter, and when an array of strings is passed as an argument to the Invoke method, javac exactly what syntax to follow. jdk1.5 must be compatible with jdk1.4 syntax, and will be processed according to jdk1.4 syntax, that is, to break the array into several separate parameters . Therefore, you cannot use code Mainmethod.invoke (null,new string[]{"xxx"}) when passing arguments to the main method. Javac only interprets it as a jdk1.4 syntax and does not interpret it as a jdk1.5 syntax, so the problem of incorrect parameter types occurs.
Solution:
Mainmethod.invoke (null,newobject[]{new string[]{"xxx"}});
Mainmethod.invoke (null, (Object) newstring[]{"xxx"}); , the compiler does a special processing, the compiler does not treat the argument as an array, and the array is not broken into several parameters.