KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > debug > ObjectDebuger


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.commons.debug;
6
7 import java.util.* ;
8 import java.lang.reflect.Field JavaDoc;
9 /**
10  * Jul 29, 2004
11  * @author: Tuan Nguyen
12  * @email: tuan08@users.sourceforge.net
13  * @version: $Id: ObjectDebuger.java,v 1.4 2004/09/21 00:04:43 tuan08 Exp $
14  */

15 public class ObjectDebuger {
16     static public void printObject(Object JavaDoc o) throws Exception JavaDoc {
17     System.out.println(asString(o)) ;
18   }
19   
20   static public String JavaDoc asString(Object JavaDoc o) {
21      StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
22      try {
23       Map map = new HashMap(100) ;
24       if(o instanceof Collection) printCollection(map, (Collection)o, b, "") ;
25       else if(o instanceof Map) printCollection(map, ((Map)o).values(), b, "") ;
26       else printObject(map, o, b, "") ;
27      } catch (Exception JavaDoc ex) {
28        ex.printStackTrace() ;
29        b.append("\n").append(ex.getMessage()) ;
30      }
31      return b.toString() ;
32   }
33   
34     static private void printObject(Map printedObjects, Object JavaDoc o, StringBuffer JavaDoc b, String JavaDoc indent) throws Exception JavaDoc{
35     if(o == null) return ;
36     if(printedObjects.containsKey(o)) return ;
37     printedObjects.put(o, o) ;
38         Class JavaDoc clazz = o.getClass() ;
39         Field JavaDoc[] fields = clazz.getDeclaredFields() ;
40         b.append(indent).append("object: " + getClassName(clazz)).append("\n") ;
41         indent = indent + " " ;
42         for(int i = 0; i < fields.length; i++) {
43       if(fields[i].getDeclaringClass().getName().startsWith("java")) continue ;
44             Class JavaDoc type = fields[i].getType() ;
45             fields[i].setAccessible(true);
46             if(type.equals(String JavaDoc.class)) {
47                 String JavaDoc s = (String JavaDoc)fields[i].get(o) ;
48         if(s == null) s = "" ;
49                 if(s.length() > 50 ) s = s.substring(0, 50) + "...\n" ;
50                 b.append(indent).append(fields[i].getName()).append(": ").append(s).append("\n") ;
51             } else if(type.equals(Boolean JavaDoc.class) || type.equals(Boolean.TYPE) ||
52                     type.equals(Integer JavaDoc.class) || type.equals(Integer.TYPE) ||
53           type.equals(Long JavaDoc.class) || type.equals(Long.TYPE) ||
54                     type.equals(Float JavaDoc.class) || type.equals(Float.TYPE) ||
55                     type.equals(Double JavaDoc.class) || type.equals(Double.TYPE)) {
56                 Object JavaDoc value = fields[i].get(o) ;
57                 b.append(indent).append(fields[i].getName()).append(": ").append(value).append("\n");
58             } else {
59                 Object JavaDoc value = fields[i].get(o) ;
60                 if(value instanceof Collection) {
61                     b.append(indent).append(fields[i].getName()).append("[Collection]\n");
62                     printCollection(printedObjects, (Collection) value, b, indent + " ") ;
63                 } else if(value instanceof Map) {
64                     b.append(indent).append(fields[i].getName()).append("[Map]\n");
65                     printCollection(printedObjects, ((Map)value).values(), b, indent + " ") ;
66                 } else {
67                     printObject(printedObjects, value, b, indent) ;
68                 }
69             }
70         }
71     }
72   
73   static private void printCollection(Map printedObjects, Collection c,
74                                       StringBuffer JavaDoc b, String JavaDoc indent) throws Exception JavaDoc {
75     Iterator i = c.iterator() ;
76     while(i.hasNext()) {
77       Object JavaDoc o = i.next() ;
78       printObject(printedObjects,o, b, indent) ;
79     }
80   }
81   
82   static private String JavaDoc getClassName(Class JavaDoc clazz) {
83     String JavaDoc name = clazz.getName() ;
84     int idx = name.lastIndexOf(".") ;
85     if(idx > 0) name = name.substring(idx + 1, name.length()) ;
86     return name ;
87   }
88 }
Popular Tags