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 ));
This method is useful for debugging the EJB programs that are dependent on the container, and is backed up here.
The typeutil source program is as follows:
/**
* The typeutil class static methods for inspecting complex Java types.
* The typetostring () method is used to dump the contents of a passed object
* Of any type (or collection) to a string. This can be very useful for debugging code that
* Manipulates complex structures.
*
*
* @ Version $ revision: 1.2.6.4 $
*/
Import java. util .*;
Import java. Lang. Reflect .*;
Public class typeutil {
/**
* Returns a string holding the contents
* Of the passed object,
* @ Param scope string
* @ Param parentobject object
* @ Param visitedobjs list
* @ Return string
*/
Private Static string complextypetostring (string scope, object parentobject, list visitedobjs ){
Stringbuffer buffer = new stringbuffer ("");
Try {
//
// OK, now we need to reflect into the object and add its child nodes...
//
Class Cl = parentobject. getclass ();
While (CL! = NULL ){
Processfields (Cl. getdeclaredfields (),
Scope,
Parentobject,
Buffer,
Visitedobjs );
CL = Cl. getsuperclass ();
}
} Catch (illegalaccessexception IAE ){
Buffer. append (IAE. tostring ());
}
Return (buffer. tostring ());
}
/**
* Method processfields
* @ Param fields field []
* @ Param scope string
* @ Param parentobject object
* @ Param buffer stringbuffer
* @ Param visitedobjs list
* @ Throws illegalaccessexception
*/
Private Static void processfields (field [] fields,
String scope,
Object parentobject,
Stringbuffer buffer,
List visitedobjs) throws illegalaccessexception {
For (INT I = 0; I <fields. length; I ++ ){
//
// Disregard certain fields for IDL Structures
//
If (fields [I]. getname (). Equals ("_ discriminator ")
| Fields [I]. getname (). Equals ("_ uninitialized ")){
Continue;
}
//
// This allows us to see non-public fields. We might need to deal with some
// Securitymanager issues here once it is outside of Vaj...
//
Fields [I]. setaccessible (true );
If (modifier. isstatic (fields [I]. getmodifiers ())){
//
// Ignore all static members. The classes that this dehydrator is
// Meant to handle are simple data objects, so static members have no
// Bearing ....
//
} Else {
Buffer. append (
Typetostring (scope + "." + fields [I]. getname (), fields [I]. Get (parentobject), visitedobjs ));
}
}
}
/**
* Method iscollectiontype
* @ Param OBJ object
* @ Return Boolean
*/
Public static Boolean iscollectiontype (Object OBJ ){
Return (obj. getclass (). isarray () |
(OBJ instanceof collection) |
(OBJ instanceof hashtable) |
(OBJ instanceof hashmap) |
(OBJ instanceof hashset) |
(OBJ instanceof list) |
(OBJ instanceof abstractmap ));
}
/**
* Method iscomplextype
* @ Param OBJ object
* @ Return Boolean
*/
Public static Boolean iscomplextype (Object OBJ ){
If (OBJ instanceof Boolean |
OBJ instanceof short |
OBJ instanceof byte |
OBJ instanceof integer |
OBJ instanceof long |
OBJ instanceof float |
OBJ instanceof character |
OBJ instanceof double |
OBJ instanceof string ){
Return false;
}
Else {
Class objectclass = obj. getclass ();
If (objectclass = Boolean. Class
| Objectclass = Boolean. Class
| Objectclass = short. Class
| Objectclass = short. Class
| Objectclass = byte. Class
| Objectclass = byte. Class
| Objectclass = int. Class
| Objectclass = integer. Class
| Objectclass = long. Class
| Objectclass = long. Class
| Objectclass = float. Class
| Objectclass = float. Class
| Objectclass = Char. Class
| Objectclass = character. Class
| Objectclass = double. Class
| Objectclass = double. Class
| Objectclass = string. Class ){
Return false;
}
Else {
Return true;
}
}
}
/**
* Returns a string holding the contents
* Of the passed object,
* @ Param scope string
* @ Param OBJ object
* @ Param visitedobjs list
* @ Return string
*/
Private Static string collectiontypetostring (string scope, object OBJ, list visitedobjs ){
Stringbuffer buffer = new stringbuffer ("");
If (obj. getclass (). isarray ()){
If (array. getlength (OBJ)> 0 ){
For (Int J = 0; j <array. getlength (OBJ); j ++ ){
Object x = array. Get (OBJ, J );
Buffer. append (typetostring (scope + "[" + J + "]", X, visitedobjs ));
}
} Else {
Buffer. append (scope + "[]: Empty/N ");
}
} Else {
Boolean iscollection = (OBJ instanceof collection );
Boolean ishashtable = (OBJ instanceof hashtable );
Boolean ishashmap = (OBJ instanceof hashmap );
Boolean ishashset = (OBJ instanceof hashset );
Boolean isabstractmap = (OBJ instanceof abstractmap );
Boolean ismap = isabstractmap | ishashmap | ishashtable;
If (ismap ){
Set keyset = (MAP) OBJ). keyset ();
Iterator = keyset. iterator ();
Int size = keyset. Size ();
If (size> 0 ){
For (Int J = 0; iterator. hasnext (); j ++ ){
Object key = iterator. Next ();
Object x = (MAP) OBJ). Get (key );
Buffer. append (typetostring (scope + "[/" "+ key +"/"]", X, visitedobjs ));
}
} Else {
Buffer. append (scope + "[]: Empty/N ");
}
} Else
If (/* ishashtable | */
Iscollection | ishashset/* | ishashmap */
){
Iterator = NULL;
Int size = 0;
If (OBJ! = NULL ){
If (iscollection ){
Iterator = (Collection) OBJ). iterator ();
Size = (Collection) OBJ). Size ();
} Else
If (ishashtable ){
Iterator = (hashtable) OBJ). Values (). iterator ();
Size = (hashtable) OBJ). Size ();
} Else
If (ishashset ){
Iterator = (hashset) OBJ). iterator ();
Size = (hashset) OBJ). Size ();
} Else
If (ishashmap ){
Iterator = (hashmap) OBJ). Values (). iterator ();
Size = (hashmap) OBJ). Size ();
}
If (size> 0 ){
For (Int J = 0; iterator. hasnext (); j ++ ){
Object x = iterator. Next ();
Buffer. append (typetostring (scope + "[" + J + "]", X, visitedobjs ));
}
} Else {
Buffer. append (scope + "[]: Empty/N ");
}
} Else {
//
// Theobject is null
Buffer. append (scope + "[]: NULL/N ");
}
}
}
Return (buffer. tostring ());
}
/**
* Method typetostring
* @ Param scope string
* @ Param OBJ object
* @ Param visitedobjs list
* @ Return string
*/
Private Static string typetostring (string scope, object OBJ, list visitedobjs ){
If (OBJ = NULL ){
Return (scope + ": NULL/N ");
}
Else if (iscollectiontype (OBJ )){
Return collectiontypetostring (scope, OBJ, visitedobjs );
}
Else if (iscomplextype (OBJ )){
If (! Visitedobjs. Contains (OBJ )){
Visitedobjs. Add (OBJ );
Return complextypetostring (scope, OBJ, visitedobjs );
}
Else {
Return (scope + ": <already visited>/N ");
}
}
Else {
Return (scope + ":" + obj. tostring () + "/N ");
}
}
/**
* The typetostring () method is used to dump the contents of a passed object
* Of any type (or collection) to a string. This can be very useful for debugging code that
* Manipulates complex structures.
*
* @ Param Scope
* @ Param OBJ
*
* @ Return string
*
*/
Public static string typetostring (string scope, object OBJ ){
If (OBJ = NULL ){
Return (scope + ": NULL/N ");
}
Else if (iscollectiontype (OBJ )){
Return collectiontypetostring (scope, OBJ, new arraylist ());
}
Else if (iscomplextype (OBJ )){
Return complextypetostring (scope, OBJ, new arraylist ());
}
Else {
Return (scope + ":" + obj. tostring () + "/N ");
}
}
}