From 7c25c84ee2ea4fba56c60bd103e87e2b7a86223c Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Wed, 6 Jul 2011 09:15:27 +0000 Subject: [PATCH] Deprecate/move CGLIB methods AopUtils=>ClassUtils isCglibProxy* methods in AopUtils are useful in lower-level modules, i.e. those that cannot depend on .aop. Therefore copied these methods to ClassUtils; deprecated the existing ones in AopUtils and now delegating to the new location; switched all usage of AopUtils#isCglibProxy* within the framework to use ClassUtils#isCglibProxy* instead. --- .../aop/framework/Cglib2AopProxy.java | 5 ++-- .../springframework/aop/support/AopUtils.java | 17 ++++++++---- ...tedConfigurationClassEnhancementTests.java | 4 +-- .../configuration/Spr7167Tests.java | 11 ++++---- .../org/springframework/util/ClassUtils.java | 26 +++++++++++++++++++ 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/org.springframework.aop/src/main/java/org/springframework/aop/framework/Cglib2AopProxy.java b/org.springframework.aop/src/main/java/org/springframework/aop/framework/Cglib2AopProxy.java index 255d0b76ec..46ed66b904 100644 --- a/org.springframework.aop/src/main/java/org/springframework/aop/framework/Cglib2AopProxy.java +++ b/org.springframework.aop/src/main/java/org/springframework/aop/framework/Cglib2AopProxy.java @@ -35,11 +35,11 @@ import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import net.sf.cglib.proxy.NoOp; import net.sf.cglib.transform.impl.UndeclaredThrowableStrategy; + import org.aopalliance.aop.Advice; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; - import org.springframework.aop.Advisor; import org.springframework.aop.PointcutAdvisor; import org.springframework.aop.RawTargetAccess; @@ -47,6 +47,7 @@ import org.springframework.aop.TargetSource; import org.springframework.aop.support.AopUtils; import org.springframework.core.SmartClassLoader; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; /** @@ -155,7 +156,7 @@ final class Cglib2AopProxy implements AopProxy, Serializable { Assert.state(rootClass != null, "Target class must be available for creating a CGLIB proxy"); Class proxySuperClass = rootClass; - if (AopUtils.isCglibProxyClass(rootClass)) { + if (ClassUtils.isCglibProxyClass(rootClass)) { proxySuperClass = rootClass.getSuperclass(); Class[] additionalInterfaces = rootClass.getInterfaces(); for (Class additionalInterface : additionalInterfaces) { diff --git a/org.springframework.aop/src/main/java/org/springframework/aop/support/AopUtils.java b/org.springframework.aop/src/main/java/org/springframework/aop/support/AopUtils.java index efb771f74d..f08481efba 100644 --- a/org.springframework.aop/src/main/java/org/springframework/aop/support/AopUtils.java +++ b/org.springframework.aop/src/main/java/org/springframework/aop/support/AopUtils.java @@ -63,7 +63,7 @@ public abstract class AopUtils { */ public static boolean isAopProxy(Object object) { return (object instanceof SpringProxy && - (Proxy.isProxyClass(object.getClass()) || isCglibProxyClass(object.getClass()))); + (Proxy.isProxyClass(object.getClass()) || ClassUtils.isCglibProxyClass(object.getClass()))); } /** @@ -76,27 +76,34 @@ public abstract class AopUtils { } /** - * Check whether the given object is a CGLIB proxy. + * Check whether the given object is a CGLIB proxy. Goes beyond the implementation + * in {@link ClassUtils#isCglibProxy(Object)} by checking also to see if the given + * object is an instance of {@link SpringProxy}. * @param object the object to check + * @see ClassUtils#isCglibProxy(Object) */ public static boolean isCglibProxy(Object object) { - return (object instanceof SpringProxy && isCglibProxyClass(object.getClass())); + return (object instanceof SpringProxy && ClassUtils.isCglibProxy(object)); } /** * Check whether the specified class is a CGLIB-generated class. * @param clazz the class to check + * @deprecated as of Spring 3.1 in favor of {@link ClassUtils#isCglibProxyClass(Class)} */ + @Deprecated public static boolean isCglibProxyClass(Class clazz) { - return (clazz != null && isCglibProxyClassName(clazz.getName())); + return ClassUtils.isCglibProxyClass(clazz); } /** * Check whether the specified class name is a CGLIB-generated class. * @param className the class name to check + * @deprecated as of Spring 3.1 in favor of {@link ClassUtils#isCglibProxyClassName(String)} */ + @Deprecated public static boolean isCglibProxyClassName(String className) { - return (className != null && className.contains(ClassUtils.CGLIB_CLASS_SEPARATOR)); + return ClassUtils.isCglibProxyClassName(className); } /** diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java index f3ea386dcf..208516f99b 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/ImportedConfigurationClassEnhancementTests.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import org.junit.Test; -import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; import org.springframework.context.ApplicationContext; @@ -29,6 +28,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.util.ClassUtils; import test.beans.TestBean; @@ -54,7 +54,7 @@ public class ImportedConfigurationClassEnhancementTests { ApplicationContext ctx = new AnnotationConfigApplicationContext(configClasses); Config config = ctx.getBean(Config.class); assertTrue("autowired config class has not been enhanced", - AopUtils.isCglibProxyClass(config.autowiredConfig.getClass())); + ClassUtils.isCglibProxy(config.autowiredConfig)); } diff --git a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/Spr7167Tests.java b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/Spr7167Tests.java index 99ac3619b6..9c8f240003 100644 --- a/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/Spr7167Tests.java +++ b/org.springframework.context/src/test/java/org/springframework/context/annotation/configuration/Spr7167Tests.java @@ -16,11 +16,11 @@ package org.springframework.context.annotation.configuration; -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import org.junit.Test; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; -import org.springframework.aop.support.AopUtils; +import org.junit.Test; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -29,6 +29,7 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.util.ClassUtils; public class Spr7167Tests { @Test @@ -40,7 +41,7 @@ public class Spr7167Tests { equalTo("post processed by MyPostProcessor")); MyConfig config = ctx.getBean(MyConfig.class); - assertTrue("Config class was not enhanced", AopUtils.isCglibProxyClass(config.getClass())); + assertTrue("Config class was not enhanced", ClassUtils.isCglibProxy(config)); } } diff --git a/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java b/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java index ff0440b182..32d34d92c8 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/util/ClassUtils.java @@ -1083,4 +1083,30 @@ public abstract class ClassUtils { } } + /** + * Check whether the given object is a CGLIB proxy. + * @param object the object to check + * @see org.springframework.aop.support.AopUtils#isCglibProxy(Object) + */ + public static boolean isCglibProxy(Object object) { + return ClassUtils.isCglibProxyClass(object.getClass()); + } + + /** + * Check whether the specified class is a CGLIB-generated class. + * @param clazz the class to check + */ + public static boolean isCglibProxyClass(Class clazz) { + return (clazz != null && isCglibProxyClassName(clazz.getName())); + } + + /** + * Check whether the specified class name is a CGLIB-generated class. + * @param className the class name to check + */ + public static boolean isCglibProxyClassName(String className) { + return (className != null && className.contains(CGLIB_CLASS_SEPARATOR)); + } + + }