View Javadoc
1   package org.codehaus.plexus.classworlds;
2   
3   /*
4    * Copyright 2001-2006 Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  public class ClassView
20  {
21      /**
22       * * Formats Class information for debug output purposes.
23       * *
24       * * @param clz  the Class to print information for
25       * *
26       * * @return a String describing the Class in detail
27       */
28      public static String toString( Class<?> clz )
29      {
30          if ( clz.isPrimitive() )
31          {
32              return clz.toString();
33          }
34          else if ( clz.isArray() )
35          {
36              return "Array of " + toString( clz.getComponentType() );
37          }
38          else if ( clz.isInterface() )
39          {
40              return toInterfaceString( clz, "" );
41          }
42          else
43          {
44              return toClassString( clz, "" );
45          }
46      }
47  
48      /**
49       * * Formats Class information for debug output purposes.
50       * *
51       * * @param clz      the Class to print information for
52       * * @param sIndent  the indentation to precede each line of output
53       * *
54       * * @return a String describing the Class in detail
55       */
56      private static String toClassString( Class<?> clz, String sIndent )
57      {
58          StringBuffer sb = new StringBuffer();
59          sb.append( sIndent )
60              .append( "Class " )
61              .append( clz.getName() )
62              .append( "  (" )
63              .append( toString( clz.getClassLoader() ) )
64              .append( ')' );
65  
66          sIndent += "  ";
67  
68          Class<?>[] aclz = clz.getInterfaces();
69          for ( int i = 0, c = aclz.length; i < c; ++i )
70          {
71              sb.append( '\n' )
72                  .append( toInterfaceString( aclz[i], sIndent ) );
73          }
74  
75          clz = clz.getSuperclass();
76          if ( clz != null )
77          {
78              sb.append( '\n' )
79                  .append( toClassString( clz, sIndent ) );
80          }
81  
82          return sb.toString();
83      }
84  
85      /**
86       * * Formats interface information for debug output purposes.
87       * *
88       * * @param clz      the interface Class to print information for
89       * * @param sIndent  the indentation to precede each line of output
90       * *
91       * * @return a String describing the interface Class in detail
92       */
93      private static String toInterfaceString( Class<?> clz, String sIndent )
94      {
95          StringBuffer sb = new StringBuffer();
96          sb.append( sIndent )
97              .append( "Interface " )
98              .append( clz.getName() )
99              .append( "  (" )
100             .append( toString( clz.getClassLoader() ) )
101             .append( ')' );
102 
103         Class<?>[] aclz = clz.getInterfaces();
104         for ( int i = 0, c = aclz.length; i < c; ++i )
105         {
106             clz = aclz[i];
107 
108             sb.append( '\n' )
109                 .append( toInterfaceString( clz, sIndent + "  " ) );
110         }
111 
112         return sb.toString();
113     }
114 
115     /**
116      * * Format a description for the specified ClassLoader object.
117      * *
118      * * @param loader  the ClassLoader instance (or null)
119      * *
120      * * @return a String description of the ClassLoader
121      */
122     private static String toString( ClassLoader loader )
123     {
124         if ( loader == null )
125         {
126             return "System ClassLoader";
127         }
128 
129         return "ClassLoader class=" + loader.getClass().getName() + ", hashCode=" + loader.hashCode();
130     }
131 }