mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-4105 Removes SpringUtils to ProxyUtil
This commit is contained in:
@@ -20,7 +20,7 @@ import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.Result;
|
||||
import com.opensymphony.xwork2.Unchainable;
|
||||
import com.opensymphony.xwork2.inject.Inject;
|
||||
import com.opensymphony.xwork2.spring.SpringUtils;
|
||||
import com.opensymphony.xwork2.util.ProxyUtil;
|
||||
import com.opensymphony.xwork2.util.CompoundRoot;
|
||||
import com.opensymphony.xwork2.util.TextParseUtil;
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
@@ -163,8 +163,8 @@ public class ChainingInterceptor extends AbstractInterceptor {
|
||||
for (Object object : list) {
|
||||
if (shouldCopy(object)) {
|
||||
Object action = invocation.getAction();
|
||||
if(SpringUtils.isAopProxy(action)) {
|
||||
action = SpringUtils.getUltimateTargetObject(action);
|
||||
if(ProxyUtil.isSpringAopProxy(action)) {
|
||||
action = ProxyUtil.getSpringUltimateTargetObject(action);
|
||||
}
|
||||
reflectionProvider.copy(object, action, ctxMap, prepareExcludes(), includes);
|
||||
}
|
||||
|
||||
+14
-13
@@ -13,21 +13,23 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.opensymphony.xwork2.spring;
|
||||
package com.opensymphony.xwork2.util;
|
||||
|
||||
import com.opensymphony.xwork2.util.ClassLoaderUtil;
|
||||
import org.apache.commons.lang3.reflect.MethodUtils;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
/**
|
||||
* <code>SpringUtils</code>
|
||||
* <code>ProxyUtil</code>
|
||||
* <p>
|
||||
* Various utility methods dealing with spring framework
|
||||
* Various utility methods dealing with proxies
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
public class SpringUtils {
|
||||
public class ProxyUtil {
|
||||
private static final String SPRING_ADVISED_CLASS_NAME = "org.springframework.aop.framework.Advised";
|
||||
private static final String SPRING_SPRINGPROXY_CLASS_NAME = "org.springframework.aop.SpringProxy";
|
||||
|
||||
/**
|
||||
* Get the ultimate <em>target</em> object of the supplied {@code candidate}
|
||||
* object, unwrapping not only a top-level proxy but also any number of
|
||||
@@ -40,13 +42,12 @@ public class SpringUtils {
|
||||
* @return the target object or the {@code candidate} (never {@code null})
|
||||
* @throws IllegalStateException if an error occurs while unwrapping a proxy
|
||||
*/
|
||||
public static <T> T getUltimateTargetObject(Object candidate) {
|
||||
public static <T> T getSpringUltimateTargetObject(Object candidate) {
|
||||
try {
|
||||
if (isAopProxy(candidate) &&
|
||||
implementsInterface(candidate.getClass(), "org.springframework.aop.framework.Advised")) {
|
||||
if (isSpringAopProxy(candidate) && implementsInterface(candidate.getClass(), SPRING_ADVISED_CLASS_NAME)) {
|
||||
Object targetSource = MethodUtils.invokeMethod(candidate, "getTargetSource");
|
||||
Object target = MethodUtils.invokeMethod(targetSource, "getTarget");
|
||||
return getUltimateTargetObject(target);
|
||||
return getSpringUltimateTargetObject(target);
|
||||
}
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
@@ -59,10 +60,10 @@ public class SpringUtils {
|
||||
* Check whether the given object is a Spring proxy.
|
||||
* @param object the object to check
|
||||
*/
|
||||
public static boolean isAopProxy(Object object) {
|
||||
public static boolean isSpringAopProxy(Object object) {
|
||||
Class<?> clazz = object.getClass();
|
||||
return (implementsInterface(clazz, "org.springframework.aop.SpringProxy") &&
|
||||
(Proxy.isProxyClass(clazz) || isCglibProxyClass(clazz)));
|
||||
return (implementsInterface(clazz, SPRING_SPRINGPROXY_CLASS_NAME) && (Proxy.isProxyClass(clazz)
|
||||
|| isCglibProxyClass(clazz)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +81,7 @@ public class SpringUtils {
|
||||
*/
|
||||
private static boolean implementsInterface(Class<?> clazz, String ifaceClassName) {
|
||||
try {
|
||||
Class ifaceClass = ClassLoaderUtil.loadClass(ifaceClassName, SpringUtils.class);
|
||||
Class ifaceClass = ClassLoaderUtil.loadClass(ifaceClassName, ProxyUtil.class);
|
||||
return ifaceClass.isAssignableFrom(clazz);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return false;
|
||||
+17
-16
@@ -17,13 +17,14 @@ package com.opensymphony.xwork2.spring;
|
||||
|
||||
import com.opensymphony.xwork2.*;
|
||||
import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
|
||||
import com.opensymphony.xwork2.util.ProxyUtil;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* Test various utility methods dealing with spring framework.
|
||||
* Test various utility methods dealing with spring proxies.
|
||||
*
|
||||
*/
|
||||
public class SpringUtilsTest extends XWorkTestCase {
|
||||
public class SpringProxyUtilTest extends XWorkTestCase {
|
||||
private ApplicationContext appContext;
|
||||
|
||||
@Override public void setUp() throws Exception {
|
||||
@@ -36,49 +37,49 @@ public class SpringUtilsTest extends XWorkTestCase {
|
||||
appContext = ((SpringObjectFactory)container.getInstance(ObjectFactory.class)).appContext;
|
||||
}
|
||||
|
||||
public void testIsAopProxy() throws Exception {
|
||||
public void testIsSpringAopProxy() throws Exception {
|
||||
Object simpleAction = appContext.getBean("simple-action");
|
||||
assertFalse(SpringUtils.isAopProxy(simpleAction));
|
||||
assertFalse(ProxyUtil.isSpringAopProxy(simpleAction));
|
||||
|
||||
Object proxiedAction = appContext.getBean("proxied-action");
|
||||
assertTrue(SpringUtils.isAopProxy(proxiedAction));
|
||||
assertTrue(ProxyUtil.isSpringAopProxy(proxiedAction));
|
||||
|
||||
Object autoProxiedAction = appContext.getBean("auto-proxied-action");
|
||||
assertTrue(SpringUtils.isAopProxy(autoProxiedAction));
|
||||
assertTrue(ProxyUtil.isSpringAopProxy(autoProxiedAction));
|
||||
|
||||
Object pointcuttedTestBean = appContext.getBean("pointcutted-test-bean");
|
||||
assertTrue(SpringUtils.isAopProxy(pointcuttedTestBean));
|
||||
assertTrue(ProxyUtil.isSpringAopProxy(pointcuttedTestBean));
|
||||
|
||||
Object pointcuttedTestSubBean = appContext.getBean("pointcutted-test-sub-bean");
|
||||
assertTrue(SpringUtils.isAopProxy(pointcuttedTestSubBean));
|
||||
assertTrue(ProxyUtil.isSpringAopProxy(pointcuttedTestSubBean));
|
||||
|
||||
Object aspectedTestSubBean = appContext.getBean("aspected-test-sub-bean");
|
||||
assertFalse(SpringUtils.isAopProxy(aspectedTestSubBean));
|
||||
assertFalse(ProxyUtil.isSpringAopProxy(aspectedTestSubBean));
|
||||
}
|
||||
|
||||
public void testGetUltimateTargetObject() throws Exception {
|
||||
public void testGetSpringUltimateTargetObject() throws Exception {
|
||||
Object simpleAction = appContext.getBean("simple-action");
|
||||
Object simpleActionUltimateTargetObject = SpringUtils.getUltimateTargetObject(simpleAction);
|
||||
Object simpleActionUltimateTargetObject = ProxyUtil.getSpringUltimateTargetObject(simpleAction);
|
||||
assertEquals(simpleAction, simpleActionUltimateTargetObject);
|
||||
|
||||
Object proxiedAction = appContext.getBean("proxied-action");
|
||||
Object proxiedActionUltimateTargetObject = SpringUtils.getUltimateTargetObject(proxiedAction);
|
||||
Object proxiedActionUltimateTargetObject = ProxyUtil.getSpringUltimateTargetObject(proxiedAction);
|
||||
assertEquals(SimpleAction.class, proxiedActionUltimateTargetObject.getClass());
|
||||
|
||||
Object autoProxiedAction = appContext.getBean("auto-proxied-action");
|
||||
Object autoProxiedActionUltimateTargetObject = SpringUtils.getUltimateTargetObject(autoProxiedAction);
|
||||
Object autoProxiedActionUltimateTargetObject = ProxyUtil.getSpringUltimateTargetObject(autoProxiedAction);
|
||||
assertEquals(SimpleAction.class, autoProxiedActionUltimateTargetObject.getClass());
|
||||
|
||||
Object pointcuttedTestBean = appContext.getBean("pointcutted-test-bean");
|
||||
Object pointcuttedTestBeanUltimateTargetObject = SpringUtils.getUltimateTargetObject(pointcuttedTestBean);
|
||||
Object pointcuttedTestBeanUltimateTargetObject = ProxyUtil.getSpringUltimateTargetObject(pointcuttedTestBean);
|
||||
assertEquals(TestBean.class, pointcuttedTestBeanUltimateTargetObject.getClass());
|
||||
|
||||
Object pointcuttedTestSubBean = appContext.getBean("pointcutted-test-sub-bean");
|
||||
Object pointcuttedTestSubBeanUltimateTargetObject = SpringUtils.getUltimateTargetObject(pointcuttedTestSubBean);
|
||||
Object pointcuttedTestSubBeanUltimateTargetObject = ProxyUtil.getSpringUltimateTargetObject(pointcuttedTestSubBean);
|
||||
assertEquals(TestSubBean.class, pointcuttedTestSubBeanUltimateTargetObject.getClass());
|
||||
|
||||
Object aspectedTestSubBean = appContext.getBean("aspected-test-sub-bean");
|
||||
Object aspectedTestSubBeanUltimateTargetObject = SpringUtils.getUltimateTargetObject(aspectedTestSubBean);
|
||||
Object aspectedTestSubBeanUltimateTargetObject = ProxyUtil.getSpringUltimateTargetObject(aspectedTestSubBean);
|
||||
assertEquals(aspectedTestSubBean, aspectedTestSubBeanUltimateTargetObject);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user