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