|
15 | 15 | */
|
16 | 16 | package com.opensymphony.xwork2.util;
|
17 | 17 |
|
| 18 | +import org.apache.commons.lang3.reflect.ConstructorUtils; |
| 19 | +import org.apache.commons.lang3.reflect.FieldUtils; |
18 | 20 | import org.apache.commons.lang3.reflect.MethodUtils;
|
19 | 21 |
|
20 |
| -import java.lang.reflect.Proxy; |
| 22 | +import java.lang.reflect.*; |
21 | 23 |
|
22 | 24 | /**
|
23 | 25 | * <code>ProxyUtil</code>
|
@@ -60,6 +62,18 @@ public static boolean isProxy(Object object) {
|
60 | 62 | return isSpringAopProxy(object);
|
61 | 63 | }
|
62 | 64 |
|
| 65 | + /** |
| 66 | + * Check whether the given member is a proxy member of a proxy object. |
| 67 | + * @param member the member to check |
| 68 | + * @param object the object to check |
| 69 | + */ |
| 70 | + public static boolean isProxyMember(Member member, Object object) { |
| 71 | + if (!isProxy(object)) |
| 72 | + return false; |
| 73 | + |
| 74 | + return isSpringProxyMember(member); |
| 75 | + } |
| 76 | + |
63 | 77 | /**
|
64 | 78 | * Determine the ultimate target class of the given spring bean instance, traversing
|
65 | 79 | * not only a top-level spring proxy but any number of nested spring proxies as well —
|
@@ -95,6 +109,27 @@ private static boolean isSpringAopProxy(Object object) {
|
95 | 109 | || isCglibProxyClass(clazz)));
|
96 | 110 | }
|
97 | 111 |
|
| 112 | + /** |
| 113 | + * Check whether the given member is a member of a spring proxy. |
| 114 | + * @param member the member to check |
| 115 | + */ |
| 116 | + private static boolean isSpringProxyMember(Member member) { |
| 117 | + try { |
| 118 | + Class<?> clazz = ClassLoaderUtil.loadClass(SPRING_ADVISED_CLASS_NAME, ProxyUtil.class); |
| 119 | + if (hasMember(clazz, member)) |
| 120 | + return true; |
| 121 | + clazz = ClassLoaderUtil.loadClass(SPRING_TARGETCLASSAWARE_CLASS_NAME, ProxyUtil.class); |
| 122 | + if (hasMember(clazz, member)) |
| 123 | + return true; |
| 124 | + clazz = ClassLoaderUtil.loadClass(SPRING_SPRINGPROXY_CLASS_NAME, ProxyUtil.class); |
| 125 | + if (hasMember(clazz, member)) |
| 126 | + return true; |
| 127 | + } catch (ClassNotFoundException ignored) { |
| 128 | + } |
| 129 | + |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
98 | 133 | /**
|
99 | 134 | * Obtain the singleton target object behind the given spring proxy, if any.
|
100 | 135 | * @param candidate the (potential) spring proxy to check
|
@@ -136,4 +171,23 @@ private static boolean implementsInterface(Class<?> clazz, String ifaceClassName
|
136 | 171 | return false;
|
137 | 172 | }
|
138 | 173 | }
|
| 174 | + |
| 175 | + /** |
| 176 | + * Check whether the given class has a given member. |
| 177 | + * @param clazz the class to check |
| 178 | + * @param member the member to check |
| 179 | + */ |
| 180 | + private static boolean hasMember(Class<?> clazz, Member member) { |
| 181 | + if (member instanceof Method) { |
| 182 | + return null != MethodUtils.getMatchingMethod(clazz, member.getName(), ((Method) member).getParameterTypes()); |
| 183 | + } |
| 184 | + if (member instanceof Field) { |
| 185 | + return null != FieldUtils.getField(clazz, member.getName(), true); |
| 186 | + } |
| 187 | + if (member instanceof Constructor) { |
| 188 | + return null != ConstructorUtils.getMatchingAccessibleConstructor(clazz, ((Constructor) member).getParameterTypes()); |
| 189 | + } |
| 190 | + |
| 191 | + return false; |
| 192 | + } |
139 | 193 | }
|
0 commit comments