1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.core.util;
18
19 import java.lang.reflect.InvocationHandler;
20 import java.lang.reflect.Proxy;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.apache.commons.lang.ClassUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.kuali.rice.core.api.exception.RiceRuntimeException;
27 import org.kuali.rice.core.util.reflect.TargetedInvocationHandler;
28
29
30
31
32
33
34 public final class ClassLoaderUtils {
35
36 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ClassLoaderUtils.class);
37
38 private ClassLoaderUtils() {
39 throw new UnsupportedOperationException("do not call");
40 }
41
42
43
44
45
46
47
48 public static ClassLoader getDefaultClassLoader() {
49 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
50 if (classLoader == null) {
51 classLoader = ClassLoaderUtils.class.getClassLoader();
52 }
53 return classLoader;
54 }
55
56
57
58
59
60 public static boolean isInstanceOf(Object object, Class<?> instanceClass) {
61 if (object == null) {
62 return false;
63 }
64 if (instanceClass.isInstance(object)) {
65 return true;
66 }
67 return isInstanceOf(unwrapFromProxyOnce(object), instanceClass);
68 }
69
70 public static Object unwrapFromProxy(Object proxy) {
71 Object unwrapped = unwrapFromProxyOnce(proxy);
72 if (unwrapped == null) {
73 return proxy;
74 }
75 return unwrapFromProxy(unwrapped);
76 }
77
78
79
80
81
82 private static Object unwrapFromProxyOnce(Object proxy) {
83 if (proxy != null && Proxy.isProxyClass(proxy.getClass())) {
84 InvocationHandler invocationHandler = Proxy.getInvocationHandler(proxy);
85 if (invocationHandler instanceof TargetedInvocationHandler) {
86 return ((TargetedInvocationHandler)invocationHandler).getTarget();
87 }
88 }
89 return null;
90 }
91
92
93
94
95 public static boolean isClassVisible(ClassLoader classLoader, Class<?> classToCheck) {
96 try {
97 Class<?> classFound = classLoader.loadClass(classToCheck.getName());
98 return classFound.equals(classToCheck);
99 } catch (ClassNotFoundException e) {
100 return false;
101 }
102 }
103
104
105
106
107 public static Class[] getInterfacesToProxy(Object object, ClassLoader proxyClassLoader, String[] packageNamesToFilter) {
108 List interfaces = ClassUtils.getAllInterfaces(object.getClass());
109 outer:for (Iterator iterator = interfaces.iterator(); iterator.hasNext();) {
110 Class objectInterface = (Class) iterator.next();
111
112 if (packageNamesToFilter != null) {
113 for (String packageNames : packageNamesToFilter) {
114 if (objectInterface.getName().startsWith(packageNames)) {
115 iterator.remove();
116 continue outer;
117 }
118 }
119 }
120
121 if (proxyClassLoader != null) {
122 if (!ClassLoaderUtils.isClassVisible(proxyClassLoader, objectInterface)) {
123 if (LOG.isDebugEnabled()) {
124 LOG.debug("The interface " + objectInterface + " was not visible from the proxy ClassLoader when attempting to proxy: " + object);
125 }
126 iterator.remove();
127 continue outer;
128 }
129 }
130 }
131 Class[] interfaceArray = new Class[interfaces.size()];
132 return (Class[]) interfaces.toArray(interfaceArray);
133 }
134
135
136
137 public static Class<?> getClass(String className) {
138 if (StringUtils.isEmpty(className)) {
139 return null;
140 }
141 try {
142 return ClassUtils.getClass(getDefaultClassLoader(), className);
143 } catch (ClassNotFoundException e) {
144 throw new RiceRuntimeException(e);
145 }
146 }
147
148 public static <T> Class<? extends T> getClass(String className, Class<T> type) throws ClassNotFoundException {
149 Class<?> theClass = ClassUtils.getClass(getDefaultClassLoader(), className);
150 return theClass.asSubclass(type);
151 }
152
153 }