/* * 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: * 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 maps various components in the Java class into 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- * <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 (24, "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 characters containing I in the Value corresponding to the String type field in the obj object with 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); // brute force reflection If (f. getType () = String. class) {// use = to compare bytecode 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 age ){ 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 } } } |