Java reflect: A good debugging tool for printing Object Attributes

Source: Internet
Author: User

Backup a useful tool class. typeutil
Its typetostring (string scope, object OBJ) method adopts the reflect mechanism of Java to print the content of any object.
This is very useful for debugging programs.
Usage:
If you have an object (such as testclassobject) and want to print its content, you can use the following method:
System. Out. println (typeutil. typetostring ("yourclassobjectname", testclassobject ));
The typeutil source program is as follows:


/** <Br/> * The typeutil class static methods for inspecting complex Java types. <br/> * The typetostring () method is used to dump the contents of a passed object <br/> * of any type (or collection) to a string. this can be very useful for debugging code that <br/> * manipulates complex structures. <br/> * @ version $ revision: 1.2.6.4 $ <br/> */</P> <p> Import Java. util. *; <br/> Import Java. lang. reflect. *; </P> <p> public class typeutil {</P> <p>/** <br/> * returns a string holding the contents <br/> * of the passed object, <br/> * @ Param scope string <br/> * @ Param parentobject object <br/> * @ Param visitedobjs list <br/> * @ return string <br/> * /<br/> Private Static string complextypetostring (string scope, object parentobject, list visitedobjs) {<br/> stringbuffer buffer = new S Tringbuffer (""); <br/> try {<br/> // OK, now we need to reflect into the object and add its child nodes... <br/> // </P> <p> class Cl = parentobject. getclass (); <br/> while (CL! = NULL) {</P> <p> processfields (Cl. getdeclaredfields (), <br/> scope, <br/> parentobject, <br/> buffer, <br/> visitedobjs); </P> <p> Cl = Cl. getsuperclass (); <br/>}< br/>} catch (illegalaccessexception IAE) {<br/> buffer. append (IAE. tostring (); <br/>}</P> <p> return (buffer. tostring ()); <br/>}</P> <p>/** <br/> * method processfields <br/> * @ Param fields field [] <br/> * @ Param scope string <br/> * @ Param parentobject object <br/> * @ Param buffer stringbuffer <br/> * @ Param visitedobjs list <br/> * @ throws illegalaccessexception <br/> */<br/> Private Static void processfields (field [] fields, <br/> string scope, <br/> Object parentobject, <br/> stringbuffer, <br/> List visitedobjs) throws illegalaccessexception {<br/> for (INT I = 0; I <fields. length; I ++) {<br/> // di Sregard certain fields for IDL structures <br/> // <br/> If (fields [I]. getname (). equals ("_ discriminator") <br/> | fields [I]. getname (). equals ("_ uninitialized") {<br/> continue; <br/>}</P> <p> // <br/> // This allows us to see non-public fields. we might need to deal with some <br/> // securitymanager issues here once it is outside of Vaj... <br/> // <br/> fields [I]. setaccessible (true); <br/> I F (modifier. isstatic (fields [I]. getmodifiers () {<br/> // ignore all static members. the classes that this dehydrator is <br/> // meant to handle are simple data objects, so static members have no <br/> // bearing .... <br/> // <br/>} else {<br/> buffer. append (<br/> typetostring (scope + ". "+ fields [I]. getname (), fields [I]. get (parentobject), visitedobjs); <br/>}< br/> /** <Br/> * method iscollectiontype <br/> * @ Param OBJ object <br/> * @ return Boolean <br/> */<br/> Public static Boolean iscollectiontype (Object OBJ) {</P> <p> return (obj. getclass (). isarray () | <br/> (OBJ instanceof collection) | <br/> (OBJ instanceof hashtable) | <br/> (OBJ instanceof hashmap) | <br/> (OBJ instanceof hashset) | <br/> (OBJ instanceof list) | <br/> (OBJ instanceof abstractmap) ); <Br/>}</P> <p>/** <br/> * method iscomplextype <br/> * @ Param OBJ object <br/> * @ return Boolean <br/> */<br/> Public static Boolean iscomplextype (Object OBJ) {</P> <p> If (OBJ instanceof Boolean | <br/> OBJ instanceof short | <br/> OBJ instanceof byte | <br/> OBJ instanceof integer | <br/> OBJ instanceof long | <br/> OBJ instanceof float | <br/> OBJ instanceof character | <br/> OBJ in Stanceof double | <br/> OBJ instanceof string) {</P> <p> return false; <br/>}< br/> else {<br/> class objectclass = obj. getclass (); </P> <p> If (objectclass = Boolean. class <br/> | objectclass = Boolean. class <br/> | objectclass = short. class <br/> | objectclass = short. class <br/> | objectclass = byte. class <br/> | objectclass = byte. class <br/> | objectclass = int. class <br/> | ob Jectclass = integer. class <br/> | objectclass = long. class <br/> | objectclass = long. class <br/> | objectclass = float. class <br/> | objectclass = float. class <br/> | objectclass = char. class <br/> | objectclass = character. class <br/> | objectclass = double. class <br/> | objectclass = double. class <br/> | objectclass = string. class) {<br/> return false; <br/>}</P> <p> else {<Br/> return true; <br/>}< br/>/** <br/> * returns a string holding the contents <br/> * of passed object, <br/> * @ Param scope string <br/> * @ Param OBJ object <br/> * @ Param visitedobjs list <br/> * @ return string <br/> * /<br/> Private Static string collectiontypetostring (string scope, object OBJ, list visitedobjs) {<br/> stringbuffer buffer = new stringbuffer (""); <br/> I F (obj. getclass (). isarray () {<br/> If (array. getlength (OBJ)> 0) {<br/> for (Int J = 0; j <array. getlength (OBJ); j ++) {<br/> Object x = array. get (OBJ, J); <br/> buffer. append (typetostring (scope + "[" + J + "]", X, visitedobjs); <br/>}< br/>} else {<br/> buffer. append (scope + "[]: Empty/N"); <br/>}< br/>} else {<br/> Boolean iscollection = (OBJ instanceof collection ); <br/> Boolean Isha Shtable = (OBJ instanceof hashtable); <br/> Boolean ishashmap = (OBJ instanceof hashmap); <br/> Boolean ishashset = (OBJ instanceof hashset ); <br/> Boolean isw.actmap = (OBJ instanceof abstractmap); <br/> Boolean ismap = isw.actmap | ishashmap | ishashtable; <br/> If (ismap) {<br/> set keyset = (MAP) OBJ ). keyset (); <br/> iterator = keyset. iterator (); <br/> int size = keyset. size (); <Br/> If (size> 0) {<br/> for (Int J = 0; iterator. hasnext (); j ++) {<br/> Object key = iterator. next (); <br/> Object x = (MAP) OBJ ). get (key); <br/> buffer. append (typetostring (scope + "[/" "+ key +"/"]", X, visitedobjs )); <br/>}< br/>}else {<br/> buffer. append (scope + "[]: Empty/N "); <br/>}< br/>} else <br/> If (/* ishashtable | */<br/> iscollection | ishashset/* | ishashmap */< B R/>) {<br/> iterator = NULL; <br/> int size = 0; <br/> If (OBJ! = NULL) {<br/> If (iscollection) {<br/> iterator = (Collection) OBJ ). iterator (); <br/> size = (Collection) OBJ ). size (); <br/>}else <br/> If (ishashtable) {<br/> iterator = (hashtable) OBJ ). values (). iterator (); <br/> size = (hashtable) OBJ ). size (); <br/>}else <br/> If (ishashset) {<br/> iterator = (hashset) OBJ ). iterator (); <br/> size = (hashset) OBJ ). size (); <br/>}else <br/> If (ISH Ashmap) {<br/> iterator = (hashmap) OBJ ). values (). iterator (); <br/> size = (hashmap) OBJ ). size (); <br/>}< br/> If (size> 0) {<br/> for (Int J = 0; iterator. hasnext (); j ++) {<br/> Object x = iterator. next (); <br/> buffer. append (typetostring (scope + "[" + J + "]", X, visitedobjs); <br/>}< br/>} else {<br/> buffer. append (scope + "[]: Empty/N"); <br/>}< br/>}else {<br/> // Theo Bject is null <br/> buffer. append (scope + "[]: NULL/N"); <br/>}< br/> return (buffer. tostring ()); <br/>}< br/>/** <br/> * method typetostring <br/> * @ Param scope string <br/> * @ Param OBJ object <br/> * @ Param visitedobjs list <br/> * @ return string <br/> */<br/> Private Static string typetostring (string scope, object OBJ, list visitedobjs) {</P> <p> If (OBJ = NULL) {<br/> RET Urn (scope + ": NULL/N"); <br/>}< br/> else if (iscollectiontype (OBJ) {<br/> return collectiontypetostring (scope, OBJ, visitedobjs); <br/>}< br/> else if (iscomplextype (OBJ) {<br/> If (! Visitedobjs. contains (OBJ) {<br/> visitedobjs. add (OBJ); <br/> return complextypetostring (scope, OBJ, visitedobjs); <br/>}< br/> else {<br/> return (scope + ": <already visited>/N "); <br/>}< br/> else {<br/> return (scope +": "+ obj. tostring () + "/N"); <br/>}< br/>/** <br/> * The typetostring () method is used to dump the contents of a passed object <br/> * of any type (or collection) to a string. this can be very useful for debugging code that <br/> * manipulates complex structures. <br/> * @ Param scope <br/> * @ Param OBJ <br/> * @ return string <br/> * <br/> */</P> <p> Public static string typetostring (string scope, object OBJ) {</P> <p> If (OBJ = NULL) {<br/> return (scope + ": NULL/N "); <br/>}< br/> else if (iscollectiontype (OBJ) {<br/> return collectiontypetostring (scope, OBJ, new arraylist ()); <br/>}< br/> else if (iscomplextype (OBJ) {<br/> return complextypetostring (scope, OBJ, new arraylist ()); <br/>}< br/> else {<br/> return (scope + ":" + obj. tostring () + "/N"); <br/>}< br/>}

Related Article

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.