old habits, Code ..
Package COM. jadyer. reflection; </P> <p> Import Java. lang. reflect. field; <br/> Import Java. lang. reflect. invocationtargetexception; <br/> Import Java. lang. reflect. method; </P> <p> Import COM. jadyer. model. person; </P> <p>/** <br/> * Action target class member <br/> */<br/> public class processtargetclassmember {<br/> Public static void main (string []) throws exception {<br/> processtargetclassmember test = new processtargetclas Smember (); <br/> person = new person ("Zhang qiling", 26); </P> <p> system. out. println ("-------------------------> directly operate on the object's attribute public string name, integer age"); <br/> test. processobjectfield (person, "name", "Chen Wenjin"); <br/> test. processobjectfield (person, "Age", 27); <br/> system. out. println (person); </P> <p> system. out. println ("-------------------------> directly operate the attribute public static integer Total = 100"); <br/> test. proce Ssclassfield ("com. jadyer. model. person "," Total ", 88); </P> <p> system. out. println ("-------------------------> call the object's member method setall (string name, integer age)"); <br/> test. invokeobjectmethod (person, "setall", new class [] {string. class, integer. class}, new object [] {"Wu San Province", 99}); <br/> system. out. println (person); </P> <p> system. out. println ("-------------------------> call the class member method showtotal ()"); <br/> test. invokeclassmet Hod ("com. jadyer. model. person "," showtotal ", null, null ); <br/>}</P> <p>/** <br/> * directly operate on the attributes of an object <br/> * @ see. All these attributes must be public, that is, the access permission can be used only when the access permission is permitted. <br/> * @ see; otherwise, even if it is a reflection mechanism, you cannot directly access them either. <br/> */<br/> Public void processobjectfield (Object OBJ, string fieldname, object newvalue) throws nosuchfieldexception, illegalaccessexception {<br/> class <?> Clazz = obj. getclass (); // get the Class Object of the OBJ object, which describes the information of the specified class. <br/> Field field = clazz. getfield (fieldname); // return a specific attribute in the current clazz class <br/> Object oldvalue = field. get (OBJ); // get the attribute value of the specified object. In fact, the attribute value of filename is assigned to oldvalue. <br/> system. out. println ("before modification:" + fieldname + "=" + oldvalue); <br/> field. set (OBJ, newvalue); // set the attribute value of the object to the specified value, that is, set the attribute value of fieldname Of The OBJ object to newvalue <br/> system. out. println ("modified:" + fieldname + "=" + fiel D. get (OBJ )); <br/>}</P> <p>/** <br/> * directly operate the attributes of the class <br/> * @ see because the class name is specified in the form parameter, therefore, you can directly operate the class name <br/> * @ see and processfield () the method specifies an object reference <br/> */<br/> Public void processclassfield (string classname, string fieldname, object newvalue) throws classnotfoundexception, nosuchfieldexception, illegalaccessexception {<br/> class <?> Clazz = Class. forname (classname); <br/> Field field = clazz. getfield (fieldname); <br/> Object oldvalue = field. get (clazz); <br/> system. out. println ("before modification:" + fieldname + "=" + oldvalue); <br/> field. set (clazz, newvalue); <br/> system. out. println ("modified:" + fieldname + "=" + field. get (clazz )); <br/>}</P> <p>/** <br/> * call the object member method <br/> * @ Param OBJ --------- specify an object <br/> * @ Param methodname -- specifies the method name of the object to be called <br /> * @ Param argtypes ---- specify the parameters of the method <br/> * @ Param ARGs -------- real parameters of the given method <br/> */<br/> Public void invokeobjectmethod (Object OBJ, string methodname, class <?> [] Argtypes, object [] ARGs) throws nosuchmethodexception, illegalaccessexception, invocationtargetexception {<br/> class <?> Clazz = obj. getclass (); // obtain the Class Object <br/> method = clazz. getmethod (methodname, argtypes); // return a specific method. You need to specify the method name and parameter list <br/> Object result = method. Invoke (OBJ, argS); // call the method corresponding to the method object. If this method does not return a value, null is returned. <br/> system. out. println ("Result:" + result ); <br/>}</P> <p>/** <br/> * call the class member method <br/> */<br/> Public void invokeclassmethod (string classname, string methodname, class <?> [] Argtypes, object [] ARGs) throws classnotfoundexception, nosuchmethodexception, illegalargumentexception, illegalaccessexception, invocationtargetexception {<br/> class <?> Clazz = Class. forname (classname); <br/> method = clazz. getmethod (methodname, argtypes); <br/> Object result = method. invoke (null, argS); <br/> system. out. println ("Result:" + result); <br/>}< br/>}
The following is the person class used
package COM. jadyer. model; </P> <p> public class person {<br/> Public string name; <br/> Public integer age; <br/> Public static integer Total = 100; </P> <p> Public Person () {<br/> total ++; <br/>}</P> <p> Public Person (string name, integer age) {<br/> This. name = Name; <br/> This. age = age; <br/> total ++; <br/>}</P> <p> Public void setall (string name, integer age) {<br/> This. name = Name; <br/> This. age = age; <br/>}</P> <p> Public static integer showtotal () {<br/> system. out. println ("Total =" + total); <br/> return total; <br/>}</P> <p> @ override <br/> Public String tostring () {<br/> return "name:" + name + "/Tage:" + age; <br/>}