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