KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > marshall > data > Debug


1 package org.jboss.cache.marshall.data;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.security.CodeSource JavaDoc;
6 import java.security.ProtectionDomain JavaDoc;
7
8 /**
9  * Various debugging utility methods available for use in unit tests
10  *
11  * @author Scott.Stark@jboss.org
12  */

13 public class Debug
14 {
15    /**
16     * Format a string buffer containing the Class, Interfaces, CodeSource,
17     * and ClassLoader information for the given object clazz.
18     *
19     * @param clazz the Class
20     * @param results the buffer to write the info to
21     */

22    public static void displayClassInfo(Class JavaDoc clazz, StringBuffer JavaDoc results)
23    {
24       displayClassInfo(clazz, results, true);
25    }
26
27    public static void displayClassInfo(Class JavaDoc clazz, StringBuffer JavaDoc results,
28                                        boolean showParentClassLoaders)
29    {
30
31       ClassLoader JavaDoc cl = clazz.getClassLoader();
32       results.append("\n" + clazz.getName() + "(" + Integer.toHexString(clazz.hashCode()) + ").ClassLoader=" + cl);
33       ClassLoader JavaDoc parent = cl;
34       while (parent != null)
35       {
36          results.append("\n.." + parent);
37          URL JavaDoc[] urls = getClassLoaderURLs(parent);
38          int length = urls != null ? urls.length : 0;
39          for (int u = 0; u < length; u ++)
40          {
41             results.append("\n...." + urls[u]);
42          }
43          if (showParentClassLoaders == false)
44          {
45             break;
46          }
47          if (parent != null)
48          {
49             parent = parent.getParent();
50          }
51       }
52       CodeSource JavaDoc clazzCS = clazz.getProtectionDomain().getCodeSource();
53       if (clazzCS != null)
54       {
55          results.append("\n++++CodeSource: " + clazzCS);
56       }
57       else
58       {
59          results.append("\n++++Null CodeSource");
60       }
61
62       results.append("\nImplemented Interfaces:");
63       Class JavaDoc[] ifaces = clazz.getInterfaces();
64       for (int i = 0; i < ifaces.length; i ++)
65       {
66          Class JavaDoc iface = ifaces[i];
67          results.append("\n++" + iface + "(" + Integer.toHexString(iface.hashCode()) + ")");
68          ClassLoader JavaDoc loader = ifaces[i].getClassLoader();
69          results.append("\n++++ClassLoader: " + loader);
70          ProtectionDomain JavaDoc pd = ifaces[i].getProtectionDomain();
71          CodeSource JavaDoc cs = pd.getCodeSource();
72          if (cs != null)
73          {
74             results.append("\n++++CodeSource: " + cs);
75          }
76          else
77          {
78             results.append("\n++++Null CodeSource");
79          }
80       }
81    }
82
83    /**
84     * Use reflection to access a URL[] getURLs or ULR[] getAllURLs method so
85     * that non-URLClassLoader class loaders, or class loaders that override
86     * getURLs to return null or empty, can provide the true classpath info.
87     */

88    public static URL JavaDoc[] getClassLoaderURLs(ClassLoader JavaDoc cl)
89    {
90       URL JavaDoc[] urls = {};
91       try
92       {
93          Class JavaDoc returnType = urls.getClass();
94          Class JavaDoc[] parameterTypes = {};
95          Method JavaDoc getURLs = cl.getClass().getMethod("getURLs", parameterTypes);
96          if (returnType.isAssignableFrom(getURLs.getReturnType()))
97          {
98             Object JavaDoc[] args = {};
99             urls = (URL JavaDoc[]) getURLs.invoke(cl, args);
100          }
101       }
102       catch (Exception JavaDoc ignore)
103       {
104       }
105       return urls;
106    }
107
108 }
109
Popular Tags