1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.surf.util;
20
21 import java.net.URL;
22
23
24
25
26
27
28 public class Debug
29 {
30
31
32
33
34
35
36
37 public static String whichClass(String className) throws ClassNotFoundException
38 {
39 String path = className;
40
41
42 if (path.startsWith("/") == false)
43 {
44 path = "/" + path;
45 }
46 path = path.replace('.', '/');
47 path = path + ".class";
48
49
50 URL url = Debug.class.getResource(path);
51 if (url == null)
52 {
53 throw new ClassNotFoundException(className);
54 }
55
56
57 String location = url.toExternalForm();
58 if (location.startsWith("jar"))
59 {
60 location = location.substring(10, location.lastIndexOf("!"));
61 }
62 else if (location.startsWith("file:"))
63 {
64 location = location.substring(6);
65 }
66
67 return location;
68 }
69
70
71
72
73
74
75
76
77 public static String whichClassLoader(String className) throws ClassNotFoundException
78 {
79 String result = "Could not determine class loader for " + className;
80
81 Class clazz = Class.forName(className);
82 ClassLoader loader = clazz.getClassLoader();
83
84 if (loader != null)
85 {
86 result = clazz.getClassLoader().toString();
87 }
88
89 return result;
90 }
91
92
93
94
95
96
97
98
99 public static String whichClassLoaderHierarchy(String className) throws ClassNotFoundException
100 {
101 StringBuffer buffer = new StringBuffer();
102 Class clazz = Class.forName(className);
103 ClassLoader loader = clazz.getClassLoader();
104 if (loader != null)
105 {
106 buffer.append(loader.toString());
107
108 ClassLoader parent = loader.getParent();
109 while (parent != null)
110 {
111 buffer.append("\n-> ").append(parent.toString());
112 parent = parent.getParent();
113 }
114 }
115 else
116 {
117 buffer.append("Could not determine class loader for " + className);
118 }
119
120 return buffer.toString();
121 }
122 }