org.springframework.aop.support

Class AopUtils

  • java.lang.Object
    • org.springframework.aop.support.AopUtils


  • public abstract class AopUtils
    extends java.lang.Object
    Utility methods for AOP support code.

    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:
    AopProxyUtils
    • Constructor Summary

      Constructors 
      Constructor and Description
      AopUtils() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static boolean canApply(Advisor advisor, java.lang.Class<?> targetClass)
      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 canApply(Advisor advisor, java.lang.Class<?> targetClass, boolean hasIntroductions)
      Can the given advisor apply at all on the given class?
      static boolean canApply(Pointcut pc, java.lang.Class<?> targetClass)
      Can the given pointcut apply at all on the given class?
      static boolean canApply(Pointcut pc, java.lang.Class<?> targetClass, boolean hasIntroductions)
      Can the given pointcut apply at all on the given class?
      static java.util.List<Advisor> findAdvisorsThatCanApply(java.util.List<Advisor> candidateAdvisors, java.lang.Class<?> clazz)
      Determine the sublist of the candidateAdvisors list that is applicable to the given class.
      static java.lang.reflect.Method getMostSpecificMethod(java.lang.reflect.Method method, java.lang.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 java.lang.Class<?> getTargetClass(java.lang.Object candidate)
      Determine the target class of the given bean instance which might be an AOP proxy.
      static java.lang.Object invokeJoinpointUsingReflection(java.lang.Object target, java.lang.reflect.Method method, java.lang.Object[] args)
      Invoke the given target via reflection, as part of an AOP method invocation.
      static boolean isAopProxy(java.lang.Object object)
      Check whether the given object is a JDK dynamic proxy or a CGLIB proxy.
      static boolean isCglibProxy(java.lang.Object object)
      Check whether the given object is a CGLIB proxy.
      static boolean isEqualsMethod(java.lang.reflect.Method method)
      Determine whether the given method is an "equals" method.
      static boolean isFinalizeMethod(java.lang.reflect.Method method)
      Determine whether the given method is a "finalize" method.
      static boolean isHashCodeMethod(java.lang.reflect.Method method)
      Determine whether the given method is a "hashCode" method.
      static boolean isJdkDynamicProxy(java.lang.Object object)
      Check whether the given object is a JDK dynamic proxy.
      static boolean isToStringMethod(java.lang.reflect.Method method)
      Determine whether the given method is a "toString" method.
      static java.lang.reflect.Method selectInvocableMethod(java.lang.reflect.Method method, java.lang.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AopUtils

        public AopUtils()
    • Method Detail

      • isJdkDynamicProxy

        public static boolean isJdkDynamicProxy(@Nullable
                                                java.lang.Object object)
        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 of SpringProxy.

        Parameters:
        object - the object to check
        See Also:
        Proxy.isProxyClass(java.lang.Class<?>)
      • getTargetClass

        public static java.lang.Class<?> getTargetClass(java.lang.Object candidate)
        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:
        TargetClassAware.getTargetClass(), AopProxyUtils.ultimateTargetClass(Object)
      • selectInvocableMethod

        public static java.lang.reflect.Method selectInvocableMethod(java.lang.reflect.Method method,
                                                                     @Nullable
                                                                     java.lang.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.
        Parameters:
        method - the method to check
        targetType - the target type to search methods on (typically an AOP proxy)
        Returns:
        a corresponding invocable method on the target type
        Throws:
        java.lang.IllegalStateException - if the given method is not invocable on the given target type (typically due to a proxy mismatch)
        Since:
        4.3
        See Also:
        MethodIntrospector.selectInvocableMethod(Method, Class)
      • isEqualsMethod

        public static boolean isEqualsMethod(@Nullable
                                             java.lang.reflect.Method method)
        Determine whether the given method is an "equals" method.
        See Also:
        Object.equals(java.lang.Object)
      • isHashCodeMethod

        public static boolean isHashCodeMethod(@Nullable
                                               java.lang.reflect.Method method)
        Determine whether the given method is a "hashCode" method.
        See Also:
        Object.hashCode()
      • isToStringMethod

        public static boolean isToStringMethod(@Nullable
                                               java.lang.reflect.Method method)
        Determine whether the given method is a "toString" method.
        See Also:
        Object.toString()
      • isFinalizeMethod

        public static boolean isFinalizeMethod(@Nullable
                                               java.lang.reflect.Method method)
        Determine whether the given method is a "finalize" method.
        See Also:
        Object.finalize()
      • getMostSpecificMethod

        public static java.lang.reflect.Method getMostSpecificMethod(java.lang.reflect.Method method,
                                                                     @Nullable
                                                                     java.lang.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. E.g. the method may be IFoo.bar() and the target class may be DefaultFoo. In this case, the method may be DefaultFoo.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 Java 5 bridge methods in order to retrieve attributes from the original method definition.

        Parameters:
        method - the method to be invoked, which may come from an interface
        targetClass - the target class for the current invocation. May be null or may not even implement the method.
        Returns:
        the specific target method, or the original method if the targetClass doesn't implement it or is null
        See Also:
        ClassUtils.getMostSpecificMethod(java.lang.reflect.Method, java.lang.Class<?>)
      • canApply

        public static boolean canApply(Pointcut pc,
                                       java.lang.Class<?> targetClass)
        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 check
        targetClass - the class to test
        Returns:
        whether the pointcut can apply on any method
      • canApply

        public static boolean canApply(Pointcut pc,
                                       java.lang.Class<?> targetClass,
                                       boolean hasIntroductions)
        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 check
        targetClass - the class to test
        hasIntroductions - whether or not the advisor chain for this bean includes any introductions
        Returns:
        whether the pointcut can apply on any method
      • canApply

        public static boolean canApply(Advisor advisor,
                                       java.lang.Class<?> targetClass)
        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 check
        targetClass - class we're testing
        Returns:
        whether the pointcut can apply on any method
      • canApply

        public static boolean canApply(Advisor advisor,
                                       java.lang.Class<?> targetClass,
                                       boolean hasIntroductions)
        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 check
        targetClass - class we're testing
        hasIntroductions - whether or not the advisor chain for this bean includes any introductions
        Returns:
        whether the pointcut can apply on any method
      • findAdvisorsThatCanApply

        public static java.util.List<Advisor> findAdvisorsThatCanApply(java.util.List<Advisor> candidateAdvisors,
                                                                       java.lang.Class<?> clazz)
        Determine the sublist of the candidateAdvisors list that is applicable to the given class.
        Parameters:
        candidateAdvisors - the Advisors to evaluate
        clazz - 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 java.lang.Object invokeJoinpointUsingReflection(@Nullable
                                                                                java.lang.Object target,
                                                                                java.lang.reflect.Method method,
                                                                                java.lang.Object[] args)
                                                                         throws java.lang.Throwable
        Invoke the given target via reflection, as part of an AOP method invocation.
        Parameters:
        target - the target object
        method - the method to invoke
        args - the arguments for the method
        Returns:
        the invocation result, if any
        Throws:
        java.lang.Throwable - if thrown by the target method
        AopInvocationException - in case of a reflection error