1   package org.codehaus.classworlds;
2   
3   public class ClassView
4   {
5       /***
6        * * Formats Class information for debug output purposes.
7        * *
8        * * @param clz  the Class to print information for
9        * *
10       * * @return a String describing the Class in detail
11       */
12      public static String toString( Class clz )
13      {
14          if ( clz.isPrimitive() )
15          {
16              return clz.toString();
17          }
18          else if ( clz.isArray() )
19          {
20              return "Array of " + toString( clz.getComponentType() );
21          }
22          else if ( clz.isInterface() )
23          {
24              return toInterfaceString( clz, "" );
25          }
26          else
27          {
28              return toClassString( clz, "" );
29          }
30      }
31  
32      /***
33       * * Formats Class information for debug output purposes.
34       * *
35       * * @param clz      the Class to print information for
36       * * @param sIndent  the indentation to precede each line of output
37       * *
38       * * @return a String describing the Class in detail
39       */
40      private static String toClassString( Class clz, String sIndent )
41      {
42          StringBuffer sb = new StringBuffer();
43          sb.append( sIndent )
44              .append( "Class " )
45              .append( clz.getName() )
46              .append( "  (" )
47              .append( toString( clz.getClassLoader() ) )
48              .append( ')' );
49  
50          sIndent += "  ";
51  
52          Class[] aclz = clz.getInterfaces();
53          for ( int i = 0, c = aclz.length; i < c; ++i )
54          {
55              sb.append( '\n' )
56                  .append( toInterfaceString( aclz[i], sIndent ) );
57          }
58  
59          clz = clz.getSuperclass();
60          if ( clz != null )
61          {
62              sb.append( '\n' )
63                  .append( toClassString( clz, sIndent ) );
64          }
65  
66          return sb.toString();
67      }
68  
69      /***
70       * * Formats interface information for debug output purposes.
71       * *
72       * * @param clz      the interface Class to print information for
73       * * @param sIndent  the indentation to precede each line of output
74       * *
75       * * @return a String describing the interface Class in detail
76       */
77      private static String toInterfaceString( Class clz, String sIndent )
78      {
79          StringBuffer sb = new StringBuffer();
80          sb.append( sIndent )
81              .append( "Interface " )
82              .append( clz.getName() )
83              .append( "  (" )
84              .append( toString( clz.getClassLoader() ) )
85              .append( ')' );
86  
87          Class[] aclz = clz.getInterfaces();
88          for ( int i = 0, c = aclz.length; i < c; ++i )
89          {
90              clz = aclz[i];
91  
92              sb.append( '\n' )
93                  .append( toInterfaceString( clz, sIndent + "  " ) );
94          }
95  
96          return sb.toString();
97      }
98  
99      /***
100      * * Format a description for the specified ClassLoader object.
101      * *
102      * * @param loader  the ClassLoader instance (or null)
103      * *
104      * * @return a String description of the ClassLoader
105      */
106     private static String toString( ClassLoader loader )
107     {
108         if ( loader == null )
109         {
110             return "System ClassLoader";
111         }
112 
113         return "ClassLoader class=" + loader.getClass().getName()
114             + ", hashCode=" + loader.hashCode();
115     }
116 }