Class AopUtils
Mainly for internal use within Spring's AOP support.
See AopProxyUtils
for a
collection of framework-specific AOP utility methods which depend
on internals of Spring's AOP framework implementation.
- Author:
- Rod Johnson, Juergen Hoeller, Rob Harrop
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Can the given advisor apply at all on the given class? This is an important test as it can be used to optimize out a advisor for a class.static boolean
Can the given advisor apply at all on the given class?static boolean
Can the given pointcut apply at all on the given class?static boolean
Can the given pointcut apply at all on the given class?findAdvisorsThatCanApply
(List<Advisor> candidateAdvisors, Class<?> clazz) Determine the sublist of thecandidateAdvisors
list that is applicable to the given class.static Method
getMostSpecificMethod
(Method method, Class<?> targetClass) Given a method, which may come from an interface, and a target class used in the current AOP invocation, find the corresponding target method if there is one.static Class<?>
getTargetClass
(Object candidate) Determine the target class of the given bean instance which might be an AOP proxy.static Object
invokeJoinpointUsingReflection
(Object target, Method method, Object[] args) Invoke the given target via reflection, as part of an AOP method invocation.static boolean
isAopProxy
(Object object) Check whether the given object is a JDK dynamic proxy or a CGLIB proxy.static boolean
isCglibProxy
(Object object) Check whether the given object is a CGLIB proxy.static boolean
isEqualsMethod
(Method method) Determine whether the given method is an "equals" method.static boolean
isFinalizeMethod
(Method method) Determine whether the given method is a "finalize" method.static boolean
isHashCodeMethod
(Method method) Determine whether the given method is a "hashCode" method.static boolean
isJdkDynamicProxy
(Object object) Check whether the given object is a JDK dynamic proxy.static boolean
isToStringMethod
(Method method) Determine whether the given method is a "toString" method.static Method
selectInvocableMethod
(Method method, Class<?> targetType) Select an invocable method on the target type: either the given method itself if actually exposed on the target type, or otherwise a corresponding method on one of the target type's interfaces or on the target type itself.
-
Constructor Details
-
AopUtils
public AopUtils()
-
-
Method Details
-
isAopProxy
Check whether the given object is a JDK dynamic proxy or a CGLIB proxy.This method additionally checks if the given object is an instance of
SpringProxy
.- Parameters:
object
- the object to check- See Also:
-
isJdkDynamicProxy
Check whether the given object is a JDK dynamic proxy.This method goes beyond the implementation of
Proxy.isProxyClass(Class)
by additionally checking if the given object is an instance ofSpringProxy
.- Parameters:
object
- the object to check- See Also:
-
isCglibProxy
Check whether the given object is a CGLIB proxy.This method goes beyond the implementation of
ClassUtils.isCglibProxy(Object)
by additionally checking if the given object is an instance ofSpringProxy
.- Parameters:
object
- the object to check- See Also:
-
getTargetClass
Determine the target class of the given bean instance which might be an AOP proxy.Returns the target class for an AOP proxy or the plain class otherwise.
- Parameters:
candidate
- the instance to check (might be an AOP proxy)- Returns:
- the target class (or the plain class of the given object as fallback;
never
null
) - See Also:
-
selectInvocableMethod
Select an invocable method on the target type: either the given method itself if actually exposed on the target type, or otherwise a corresponding method on one of the target type's interfaces or on the target type itself.- Parameters:
method
- the method to checktargetType
- the target type to search methods on (typically an AOP proxy)- Returns:
- a corresponding invocable method on the target type
- Throws:
IllegalStateException
- if the given method is not invocable on the given target type (typically due to a proxy mismatch)- Since:
- 4.3
- See Also:
-
isEqualsMethod
Determine whether the given method is an "equals" method.- See Also:
-
isHashCodeMethod
Determine whether the given method is a "hashCode" method.- See Also:
-
isToStringMethod
Determine whether the given method is a "toString" method.- See Also:
-
isFinalizeMethod
Determine whether the given method is a "finalize" method.- See Also:
-
getMostSpecificMethod
Given a method, which may come from an interface, and a target class used in the current AOP invocation, find the corresponding target method if there is one. E.g. the method may beIFoo.bar()
and the target class may beDefaultFoo
. In this case, the method may beDefaultFoo.bar()
. This enables attributes on that method to be found.NOTE: In contrast to
ClassUtils.getMostSpecificMethod(java.lang.reflect.Method, java.lang.Class<?>)
, this method resolves bridge methods in order to retrieve attributes from the original method definition.- Parameters:
method
- the method to be invoked, which may come from an interfacetargetClass
- the target class for the current invocation. May benull
or may not even implement the method.- Returns:
- the specific target method, or the original method if the
targetClass
doesn't implement it or isnull
- See Also:
-
canApply
Can the given pointcut apply at all on the given class?This is an important test as it can be used to optimize out a pointcut for a class.
- Parameters:
pc
- the static or dynamic pointcut to checktargetClass
- the class to test- Returns:
- whether the pointcut can apply on any method
-
canApply
Can the given pointcut apply at all on the given class?This is an important test as it can be used to optimize out a pointcut for a class.
- Parameters:
pc
- the static or dynamic pointcut to checktargetClass
- the class to testhasIntroductions
- whether or not the advisor chain for this bean includes any introductions- Returns:
- whether the pointcut can apply on any method
-
canApply
Can the given advisor apply at all on the given class? This is an important test as it can be used to optimize out a advisor for a class.- Parameters:
advisor
- the advisor to checktargetClass
- class we're testing- Returns:
- whether the pointcut can apply on any method
-
canApply
Can the given advisor apply at all on the given class?This is an important test as it can be used to optimize out a advisor for a class. This version also takes into account introductions (for IntroductionAwareMethodMatchers).
- Parameters:
advisor
- the advisor to checktargetClass
- class we're testinghasIntroductions
- whether or not the advisor chain for this bean includes any introductions- Returns:
- whether the pointcut can apply on any method
-
findAdvisorsThatCanApply
public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) Determine the sublist of thecandidateAdvisors
list that is applicable to the given class.- Parameters:
candidateAdvisors
- the Advisors to evaluateclazz
- the target class- Returns:
- sublist of Advisors that can apply to an object of the given class (may be the incoming List as-is)
-
invokeJoinpointUsingReflection
@Nullable public static Object invokeJoinpointUsingReflection(@Nullable Object target, Method method, Object[] args) throws Throwable Invoke the given target via reflection, as part of an AOP method invocation.- Parameters:
target
- the target objectmethod
- the method to invokeargs
- the arguments for the method- Returns:
- the invocation result, if any
- Throws:
Throwable
- if thrown by the target methodAopInvocationException
- in case of a reflection error
-