/* * System abbrev: * System Name: * Component No: * Component Name: * File Name:ReflectTestMain.java * author:qiuzhenping * date:2014-10-25 * Description: <description> */ /* Updation record 1: * Updation date:2014-10-25 * updator:qiuzhenping * Trace No: <trace no> * updation No: <updation no> * Updation Content: <list All contents of updation and all methods updated.> */ Package com.qiuzhping.reflect.main; Import Java.lang.reflect.Constructor; Import Java.lang.reflect.Field; Import Java.lang.reflect.Method; /** * <description functions in a word> * Reflection is the mapping of the various components in the Java class into the corresponding Java classes. * <detail description> * * @author qiuzhenping * @version [version NO, 2014-10-25] * @see [related classes/methods] * @since [Product/module version] */ public class Reflecttestmain { /** <default constructor> */ Public Reflecttestmain () { TODO auto-generated Constructor stub } /** <description functions in a word> * 2014-10-25 * <detail description> * @author qiuzhenping * @param args [Parameters description] * @return void [return type description] * @exception throws [Exception] [exception description] * @see [Related classes#related methods#related properties] */ public static void Main (string[] args) throws exception{ TODO auto-generated Method Stub constructor[] Contructor = Person.class.getConstructors (); Method[] method = Person.class.getMethods (); Person p = new person ("qiuzhping", "100001", "qiuzhping"); field [] field = P.getclass (). Getdeclaredfields (); for (Field F:field) { F.setaccessible (TRUE); System.out.println (F.getname ()); Object obj = F.get (p); System.out.println (obj); // } Changestringvalue (P); System.out.println (P.tostring ()); } /** <description functions in a word> * Replace the character of type string in the Obj object with the characters in value containing I in abc<br> * 2014-10-26 * <detail description> * @author qiuzhenping * @param obj [Parameters description] * @return void [return type description] * @exception throws [Exception] [exception description] * @see [Related classes#related methods#related properties] */ private static void Changestringvalue (Object obj) throws Exception { field[] fields = Obj.getclass (). Getdeclaredfields (); for (Field f:fields) { F.setaccessible (TRUE);//Violent reflexes if (f.gettype () = = String.class) {//byte-code comparison is used = = String oldValue = (string) f.get (obj); String newvalue = Oldvalue.replaceall ("i", "abc");//Replace All I with ABC F.set (obj, newvalue); } } } Static class Person { public person (int age, string name, string ID, string pwd) { Super (); This.age = age; THIS.name = name; This.id = ID; This.pwd = pwd; } @Override Public String toString () { Return "age =" +age + "\tname =" +name+ "\tid =" +id+ "\tpwd =" +PWD; } private int age; private String name; Private String ID; Private String pwd; public int getage () { return age; } public void Setage (int.) { This.age = age; } Public String GetName () { return name; } public void SetName (String name) { THIS.name = name; } Public String getId () { return ID; } public void SetId (String id) { This.id = ID; } Public String getpwd () { return pwd; } public void SetPwd (String pwd) { This.pwd = pwd; } /** <default constructor> */ Public person () { TODO auto-generated Constructor stub } } } |