During debugging in Android development, we often need to output class data.
For example, we often need to check the following data types.
Public class data {private int ID; private string name; private string URL; private Bitmap bitmap; private long T; private string status; Public int GETID () {return ID ;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name ;}//...... save layout}
The Code is as follows:
Tool class:
Loghelper. Java
Import Java. lang. reflect. method; public class trace {public static void reflect_object (Object o, string classpath) {If (null = o | null = classpath) return; try {class <?> Userclass = Class. forname (classpath); // load class method [] Methods = userclass. getdeclaredmethods (); // obtain the method set of the class // traverse the method set system. out. println ("= start traversing getxx methods ="); For (INT I = 0; I <methods. length; I ++) {// obtain the return values of all getxx () // methods [I]. the getname () method returns the method name if (methods [I]. getname (). startswith ("get") {object = methods [I]. invoke (o); system. out. println ("" + methods [I]. getname () + "():" + object) ;}} system. out. println ("= END =");} catch (exception e) {e. printstacktrace ();}}}
Easy to use:
Data dat = new data (); // todo dat value assignment, operation, etc ..... ... // Call the assistant and output trace. reflect_object (dat, Data. Class. getname ());
Finished.