In Android, Java reflection reflect is used to output variable values in the class for debugging.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.