`

转帖一个看起来比较有用的类-打印任意对象的内容

阅读更多

TypeUtil
它的typeToString(String scope, Object obj)方法,采用java的reflect机制,可以打印出任何对象的内容.
这对调试程序非常有用.
使用方法:
如果你有一个对象(比如testClassObject),想打印它的内容,可用如下方法:
System.out.println(TypeUtil.typeToString("yourClassObjectName",testClassObject));
这个方法,对调试那些对容器依赖的ejb程序很有用,特此备份.
以下为TypeUtil源程序:

java 代码
  1.   
  2. /**  
  3.  * The TypeUtil class static methods for inspecting complex java types.  
  4.  * The typeToString() method is used to dump the contents of a passed object    
  5.  * of any type (or collection) to a String.  This can be very useful for debugging code that   
  6.  * manipulates complex structures.   
  7.  *   
  8.  *  
  9.  * @version $Revision : 1.2.6.4 $  
  10.  */  
  11.   
  12. import java.util.*;   
  13. import java.lang.reflect.*;   
  14.   
  15. public class TypeUtil {   
  16. /**  
  17.  * Returns a string holding the contents   
  18.  * of the passed object,  
  19.  * @param scope String  
  20.  * @param parentObject Object  
  21.  * @param visitedObjs List  
  22.  * @return String  
  23.  */  
  24.   
  25.  private static String complexTypeToString(String scope, Object parentObject,List visitedObjs) {   
  26.   
  27.   StringBuffer buffer = new StringBuffer("");   
  28.   
  29.   try {   
  30.    //   
  31.    // Ok, now we need to reflect into the object and add its child nodes...   
  32.    //   
  33.   
  34.   
  35.    Class cl = parentObject.getClass();   
  36.    while ( cl != null )  {   
  37.          
  38.     processFields(cl.getDeclaredFields(),   
  39.         scope,   
  40.         parentObject,     
  41.         buffer,    
  42.         visitedObjs );   
  43.            
  44.     cl = cl.getSuperclass();   
  45.    }   
  46.   } catch (IllegalAccessException iae) {   
  47.    buffer.append(iae.toString());   
  48.   }   
  49.      
  50.   return (buffer.toString());   
  51.  }   
  52.     
  53.  /**  
  54.   * Method processFields  
  55.   * @param fields Field[]  
  56.   * @param scope String  
  57.   * @param parentObject Object  
  58.   * @param buffer StringBuffer  
  59.   * @param visitedObjs List  
  60.   * @throws IllegalAccessException  
  61.   */  
  62.  private static void processFields( Field[] fields,    
  63.           String scope,    
  64.           Object parentObject,   
  65.           StringBuffer buffer,   
  66.           List visitedObjs ) throws IllegalAccessException {   
  67.   
  68.   for (int i = 0; i < fields.length; i++) {   
  69.   
  70.    //   
  71.    // Disregard certain fields for IDL structures   
  72.    //   
  73.    if (fields[i].getName().equals("__discriminator")   
  74.     || fields[i].getName().equals("__uninitialized")) {   
  75.     continue;   
  76.    }   
  77.       
  78.    //   
  79.    // This allows us to see non-public fields.  We might need to deal with some   
  80.    // SecurityManager issues here once it is outside of VAJ...   
  81.    //   
  82.    fields[i].setAccessible(true);   
  83.   
  84.    if (Modifier.isStatic(fields[i].getModifiers())) {   
  85.     //   
  86.     // Ignore all static members.  The classes that this dehydrator is   
  87.     // meant to handle are simple data objects, so static members have no   
  88.     // bearing....   
  89.     //   
  90.    } else {   
  91.     buffer.append(   
  92.      typeToString(scope + "." + fields[i].getName(), fields[i].get(parentObject), visitedObjs));   
  93.    }   
  94.   }   
  95.   
  96.  }   
  97.   
  98.  /**  
  99.   * Method isCollectionType  
  100.   * @param obj Object  
  101.   * @return boolean  
  102.   */  
  103.  public static boolean isCollectionType(Object obj) {   
  104.      
  105.   return( obj.getClass().isArray()||   
  106.     (obj instanceof Collection)||   
  107.     (obj instanceof Hashtable)||   
  108.     (obj instanceof HashMap)||   
  109.     (obj instanceof HashSet)||   
  110.     (obj instanceof List)||   
  111.     (obj instanceof AbstractMap )  );   
  112.  }   
  113.     
  114.  /**  
  115.   * Method isComplexType  
  116.   * @param obj Object  
  117.   * @return boolean  
  118.   */  
  119.  public static boolean isComplexType(Object obj) {   
  120.      
  121.   if ( obj instanceof Boolean ||   
  122.    obj instanceof Short ||   
  123.    obj instanceof Byte ||   
  124.    obj instanceof Integer ||   
  125.    obj instanceof Long ||   
  126.    obj instanceof Float ||   
  127.    obj instanceof Character ||   
  128.    obj instanceof Double ||   
  129.    obj instanceof String )  {   
  130.          
  131.    return false;   
  132.   }   
  133.   else {   
  134.   
  135.    Class objectClass = obj.getClass();   
  136.       
  137.    if (objectClass == boolean.class  
  138.     || objectClass == Boolean.class  
  139.     || objectClass == short.class  
  140.     || objectClass == Short.class  
  141.     || objectClass == byte.class  
  142.     || objectClass == Byte.class  
  143.     || objectClass == int.class  
  144.     || objectClass == Integer.class  
  145.     || objectClass == long.class  
  146.     || objectClass == Long.class  
  147.     || objectClass == float.class  
  148.     || objectClass == Float.class  
  149.     || objectClass == char.class  
  150.     || objectClass == Character.class  
  151.     || objectClass == double.class  
  152.     || objectClass == Double.class  
  153.     || objectClass == String.class ) {   
  154.   
  155.     return false;   
  156.   
  157.    }   
  158.       
  159.    else {    
  160.     return true;   
  161.    }   
  162.   }   
  163.  }   
  164. /**  
  165.  * Returns a string holding the contents   
  166.  * of the passed object,  
  167.  * @param scope String  
  168.  * @param obj Object  
  169.  * @param visitedObjs List  
  170.  * @return String  
  171.  */  
  172.   
  173. private static String collectionTypeToString(String scope, Object obj, List visitedObjs) {   
  174.   
  175.     StringBuffer buffer = new StringBuffer("");   
  176.   
  177.     if (obj.getClass().isArray()) {   
  178.         if (Array.getLength(obj) > 0) {   
  179.   
  180.             for (int j = 0; j < Array.getLength(obj); j++) {   
  181.   
  182.                 Object x = Array.get(obj, j);   
  183.   
  184.                 buffer.append(typeToString(scope + "[" + j + "]", x, visitedObjs));   
  185.             }   
  186.   
  187.         } else {   
  188.             buffer.append(scope + "[]: empty\n");   
  189.         }   
  190.     } else {   
  191.         boolean isCollection = (obj instanceof Collection);   
  192.         boolean isHashTable = (obj instanceof Hashtable);   
  193.         boolean isHashMap = (obj instanceof HashMap);   
  194.         boolean isHashSet = (obj instanceof HashSet);   
  195.         boolean isAbstractMap = (obj instanceof AbstractMap);   
  196.         boolean isMap = isAbstractMap || isHashMap || isHashTable;   
  197.   
  198.         if (isMap) {   
  199.             Set keySet = ((Map) obj).keySet();   
  200.             Iterator iterator = keySet.iterator();   
  201.             int size = keySet.size();   
  202.   
  203.             if (size > 0) {   
  204.   
  205.                 for (int j = 0; iterator.hasNext(); j++) {   
  206.   
  207.                     Object key = iterator.next();   
  208.                     Object x = ((Map) obj).get(key);   
  209.   
  210.                     buffer.append(typeToString(scope + "[\"" + key + "\"]", x, visitedObjs));   
  211.                 }   
  212.             } else {   
  213.                 buffer.append(scope + "[]: empty\n");   
  214.             }   
  215.         } else  
  216.             if (/*isHashTable || */  
  217.                 isCollection || isHashSet /* || isHashMap */  
  218.                 ) {   
  219.   
  220.                 Iterator iterator = null;   
  221.                 int size = 0;   
  222.   
  223.                 if (obj != null) {   
  224.   
  225.                     if (isCollection) {   
  226.                         iterator = ((Collection) obj).iterator();   
  227.                         size = ((Collection) obj).size();   
  228.                     } else  
  229.                         if (isHashTable) {   
  230.                             iterator = ((Hashtable) obj).values().iterator();   
  231.                             size = ((Hashtable) obj).size();   
  232.                         } else  
  233.                             if (isHashSet) {   
  234.                                 iterator = ((HashSet) obj).iterator();   
  235.                                 size = ((HashSet) obj).size();   
  236.                             } else  
  237.                                 if (isHashMap) {   
  238.                                     iterator = ((HashMap) obj).values().iterator();   
  239.                                     size = ((HashMap) obj).size();   
  240.                                 }   
  241.   
  242.                     if (size > 0) {   
  243.   
  244.                         for (int j = 0; iterator.hasNext(); j++) {   
  245.   
  246.                             Object x = iterator.next();   
  247.                             buffer.append(typeToString(scope + "[" + j + "]", x, visitedObjs));   
  248.                         }   
  249.                     } else {   
  250.                         buffer.append(scope + "[]: empty\n");   
  251.                     }   
  252.                 } else {   
  253.                     //   
  254.                     // theObject is null   
  255.                     buffer.append(scope + "[]: null\n");   
  256.                 }   
  257.             }   
  258.     }   
  259.   
  260.     return (buffer.toString());   
  261.   
  262. }   
  263.  /**  
  264.   * Method typeToString  
  265.   * @param scope String  
  266.   * @param obj Object  
  267.   * @param visitedObjs List  
  268.   * @return String  
  269.   */  
  270.  private static String typeToString(String scope, Object obj, List visitedObjs) {   
  271.      
  272.   if (obj == null) {   
  273.    return (scope + ": null\n");   
  274.   }   
  275.   else if (isCollectionType( obj ) ) {   
  276.    return collectionTypeToString( scope, obj, visitedObjs );   
  277.   }   
  278.   else if (isComplexType( obj ) ) {   
  279.    if( ! visitedObjs.contains(obj)) {   
  280.     visitedObjs.add(obj);     
  281.     return complexTypeToString( scope, obj, visitedObjs ) ;   
  282.    }   
  283.    else {   
  284.     return(scope + ": <already visited>\n" );   
  285.    }   
  286.   }   
  287.   else {   
  288.    return ( scope + ": " + obj.toString() + "\n");   
  289.   }   
  290.  }   
  291. /**  
  292.  * The typeToString() method is used to dump the contents of a passed object    
  293.  * of any type (or collection) to a String.  This can be very useful for debugging code that   
  294.  * manipulates complex structures.   
  295.  *   
  296.  * @param scope  
  297.  * @param obj  
  298.  *  
  299.  * @return String  
  300.  *   
  301.  */  
  302.     
  303.  public static String typeToString(String scope, Object obj) {   
  304.      
  305.   if (obj == null) {   
  306.    return (scope + ": null\n");   
  307.   }   
  308.   else if (isCollectionType( obj ) ) {   
  309.    return collectionTypeToString( scope, obj, new ArrayList());   
  310.   }   
  311.   else if (isComplexType( obj ) ) {   
  312.    return complexTypeToString( scope, obj, new ArrayList() ) ;   
  313.   }   
  314.   else {   
  315.    return ( scope + ": " + obj.toString() + "\n");   
  316.   }   
  317.  }   
  318. }   
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics