View Javadoc

1   
2   /*
3    * Copyright (C) 2005 by Arno Schumacher
4    *
5    * This file is part of net.sourceforge.servletspy
6    *
7    * net.sourceforge.servletspy is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public 
9    * License as published by the Free Software Foundation; either 
10   * version 2, or (at your option) any later version.
11   *
12   * net.sourceforge.servletspy is distributed in the hope that it will
13   * be useful, but WITHOUT ANY WARRANTY; without even the implied 
14   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
15   * See the GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA 
20   */
21  
22  package net.sourceforge.servletspy.util;
23  
24  import java.lang.reflect.Proxy;
25  import java.util.Arrays;
26  
27  /***
28   * @author arno schumacher
29   */
30  public final class ClassUtil {
31  
32      private ClassUtil() {
33      }
34  
35      public static String getSimpleClassName(final Class clazz) {
36          return getClassName(clazz, true);
37      }
38  
39      public static String getClassName(final Class clazz) {
40          return getClassName(clazz, false);
41      }
42  
43      private static String getClassName(final Class clazz,
44              final boolean stripPackage) {
45          if (clazz.isArray()) {
46              return getClassName(clazz.getComponentType(), stripPackage) + "[]";
47          } else if (Proxy.isProxyClass(clazz)) {
48              return "Proxy<"
49                      + Arrays.asList(asStringArray(clazz.getInterfaces(),
50                              stripPackage)) + ">";
51          } else if (stripPackage && clazz.getPackage() != null
52                  && clazz.getPackage().getName().length() > 0) {
53              return clazz.getName().substring(
54                      clazz.getPackage().getName().length() + 1);
55          } else {
56              return clazz.getName();
57          }
58      }
59  
60      private static String[] asStringArray(final Class[] clazzes,
61              boolean stripPackage) {
62          if (clazzes == null) {
63              return new String[0];
64          } else {
65              final String[] strings = new String[clazzes.length];
66              for (int i = 0; i < strings.length; i++) {
67                  strings[i] = getClassName(clazzes[i], stripPackage);
68              }
69              return strings;
70          }
71      }
72  }