org.springframework.aop.aspectj
Class AbstractAspectJAdvice

java.lang.Object
  extended by org.springframework.aop.aspectj.AbstractAspectJAdvice
All Implemented Interfaces:
AspectJPrecedenceInformation, Ordered
Direct Known Subclasses:
AspectJAfterAdvice, AspectJAfterReturningAdvice, AspectJAfterThrowingAdvice, AspectJAroundAdvice, AspectJMethodBeforeAdvice

public abstract class AbstractAspectJAdvice
extends java.lang.Object
implements AspectJPrecedenceInformation

Base class for AOP Alliance org.aopalliance.aop.Advice classes wrapping an AspectJ aspect or an AspectJ-annotated advice method.

Since:
2.0
Author:
Rod Johnson, Adrian Colyer, Juergen Hoeller, Ramnivas Laddad

Nested Class Summary
private static class AbstractAspectJAdvice.AdviceExcludingMethodMatcher
          MethodMatcher that excludes the specified advice method.
 
Field Summary
private  int adviceInvocationArgumentCount
          The total number of arguments we have to populate on advice dispatch
private  java.util.Map<java.lang.String,java.lang.Integer> argumentBindings
           
private  java.lang.String[] argumentNames
          This will be non-null if the creator of this advice object knows the argument names and sets them explicitly
private  boolean argumentsIntrospected
           
private  AspectInstanceFactory aspectInstanceFactory
           
protected  java.lang.reflect.Method aspectJAdviceMethod
           
private  java.lang.String aspectName
          The name of the aspect (ref bean) in which this advice was defined (used when determining advice precedence so that we can determine whether two pieces of advice come from the same aspect).
private  int declarationOrder
          The order of declaration of this advice within the aspect.
private  java.lang.reflect.Type discoveredReturningGenericType
           
private  java.lang.Class discoveredReturningType
           
private  java.lang.Class discoveredThrowingType
           
protected static java.lang.String JOIN_POINT_KEY
          Key used in ReflectiveMethodInvocation userAtributes map for the current joinpoint.
private  int joinPointArgumentIndex
          Index for thisJoinPoint argument (currently only supported at index 0 if present at all)
private  int joinPointStaticPartArgumentIndex
          Index for thisJoinPointStaticPart argument (currently only supported at index 0 if present at all)
private  AspectJExpressionPointcut pointcut
           
private  java.lang.String returningName
          Non-null if after returning advice binds the return value
private  java.lang.String throwingName
          Non-null if after throwing advice binds the thrown value
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
AbstractAspectJAdvice(java.lang.reflect.Method aspectJAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aspectInstanceFactory)
          Create a new AbstractAspectJAdvice for the given advice method.
 
Method Summary
protected  java.lang.Object[] argBinding(JoinPoint jp, JoinPointMatch jpMatch, java.lang.Object returnValue, java.lang.Throwable ex)
          Take the arguments at the method execution join point and output a set of arguments to the advice method
private  void bindArgumentsByName(int numArgumentsExpectingToBind)
           
private  void bindExplicitArguments(int numArgumentsLeftToBind)
           
 Pointcut buildSafePointcut()
          Build a 'safe' pointcut that excludes the AspectJ advice method itself.
 void calculateArgumentBindings()
          Do as much work as we can as part of the set-up so that argument binding on subsequent advice invocations can be as fast as possible.
private  void configurePointcutParameters(int argumentIndexOffset)
          All parameters from argumentIndexOffset onwards are candidates for pointcut parameters - but returning and throwing vars are handled differently and must be removed from the list if present.
protected  ParameterNameDiscoverer createParameterNameDiscoverer()
          Create a ParameterNameDiscoverer to be used for argument binding.
static JoinPoint currentJoinPoint()
          Lazily instantiate joinpoint for the current invocation.
 java.lang.ClassLoader getAspectClassLoader()
          Return the ClassLoader for aspect instances.
 AspectInstanceFactory getAspectInstanceFactory()
          Return the factory for aspect instances.
 java.lang.reflect.Method getAspectJAdviceMethod()
          Return the AspectJ-style advice method.
 java.lang.String getAspectName()
          The name of the aspect (bean) in which the advice was declared.
 int getDeclarationOrder()
          The declaration order of the advice member within the aspect.
protected  java.lang.reflect.Type getDiscoveredReturningGenericType()
           
protected  java.lang.Class getDiscoveredReturningType()
           
protected  java.lang.Class getDiscoveredThrowingType()
           
protected  JoinPoint getJoinPoint()
          Overridden in around advice to return proceeding join point.
protected  JoinPointMatch getJoinPointMatch()
          Get the current join point match at the join point we are being dispatched on.
protected  JoinPointMatch getJoinPointMatch(ProxyMethodInvocation pmi)
           
 int getOrder()
          Return the order value of this object, with a higher value meaning greater in terms of sorting.
 AspectJExpressionPointcut getPointcut()
          Return the AspectJ expression pointcut.
protected  java.lang.Object invokeAdviceMethod(JoinPoint jp, JoinPointMatch jpMatch, java.lang.Object returnValue, java.lang.Throwable t)
           
protected  java.lang.Object invokeAdviceMethod(JoinPointMatch jpMatch, java.lang.Object returnValue, java.lang.Throwable ex)
          Invoke the advice method.
protected  java.lang.Object invokeAdviceMethodWithGivenArgs(java.lang.Object[] args)
           
private  boolean isVariableName(java.lang.String name)
           
private  boolean maybeBindJoinPoint(java.lang.Class candidateParameterType)
           
private  boolean maybeBindJoinPointStaticPart(java.lang.Class candidateParameterType)
           
private  boolean maybeBindProceedingJoinPoint(java.lang.Class candidateParameterType)
           
 void setArgumentNames(java.lang.String argNames)
          Set by creator of this advice object if the argument names are known.
 void setArgumentNamesFromStringArray(java.lang.String[] args)
           
 void setAspectName(java.lang.String name)
           
 void setDeclarationOrder(int order)
          Sets the declaration order of this advice within the aspect
 void setReturningName(java.lang.String name)
           
protected  void setReturningNameNoCheck(java.lang.String name)
          We need to hold the returning name at this level for argument binding calculations, this method allows the afterReturning advice subclass to set the name.
 void setThrowingName(java.lang.String name)
           
protected  void setThrowingNameNoCheck(java.lang.String name)
          We need to hold the throwing name at this level for argument binding calculations, this method allows the afterThrowing advice subclass to set the name.
protected  boolean supportsProceedingJoinPoint()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.aop.aspectj.AspectJPrecedenceInformation
isAfterAdvice, isBeforeAdvice
 

Field Detail

JOIN_POINT_KEY

protected static final java.lang.String JOIN_POINT_KEY
Key used in ReflectiveMethodInvocation userAtributes map for the current joinpoint.


aspectJAdviceMethod

protected final java.lang.reflect.Method aspectJAdviceMethod

adviceInvocationArgumentCount

private final int adviceInvocationArgumentCount
The total number of arguments we have to populate on advice dispatch


pointcut

private final AspectJExpressionPointcut pointcut

aspectInstanceFactory

private final AspectInstanceFactory aspectInstanceFactory

aspectName

private java.lang.String aspectName
The name of the aspect (ref bean) in which this advice was defined (used when determining advice precedence so that we can determine whether two pieces of advice come from the same aspect).


declarationOrder

private int declarationOrder
The order of declaration of this advice within the aspect.


argumentNames

private java.lang.String[] argumentNames
This will be non-null if the creator of this advice object knows the argument names and sets them explicitly


throwingName

private java.lang.String throwingName
Non-null if after throwing advice binds the thrown value


returningName

private java.lang.String returningName
Non-null if after returning advice binds the return value


discoveredReturningType

private java.lang.Class discoveredReturningType

discoveredThrowingType

private java.lang.Class discoveredThrowingType

joinPointArgumentIndex

private int joinPointArgumentIndex
Index for thisJoinPoint argument (currently only supported at index 0 if present at all)


joinPointStaticPartArgumentIndex

private int joinPointStaticPartArgumentIndex
Index for thisJoinPointStaticPart argument (currently only supported at index 0 if present at all)


argumentBindings

private java.util.Map<java.lang.String,java.lang.Integer> argumentBindings

argumentsIntrospected

private boolean argumentsIntrospected

discoveredReturningGenericType

private java.lang.reflect.Type discoveredReturningGenericType
Constructor Detail

AbstractAspectJAdvice

public AbstractAspectJAdvice(java.lang.reflect.Method aspectJAdviceMethod,
                             AspectJExpressionPointcut pointcut,
                             AspectInstanceFactory aspectInstanceFactory)
Create a new AbstractAspectJAdvice for the given advice method.

Parameters:
aspectJAdviceMethod - the AspectJ-style advice method
pointcut - the AspectJ expression pointcut
aspectInstanceFactory - the factory for aspect instances
Method Detail

currentJoinPoint

public static JoinPoint currentJoinPoint()
Lazily instantiate joinpoint for the current invocation. Requires MethodInvocation to be bound with ExposeInvocationInterceptor.

Do not use if access is available to the current ReflectiveMethodInvocation (in an around advice).

Returns:
current AspectJ joinpoint, or through an exception if we're not in a Spring AOP invocation.

getAspectJAdviceMethod

public final java.lang.reflect.Method getAspectJAdviceMethod()
Return the AspectJ-style advice method.


getPointcut

public final AspectJExpressionPointcut getPointcut()
Return the AspectJ expression pointcut.


buildSafePointcut

public final Pointcut buildSafePointcut()
Build a 'safe' pointcut that excludes the AspectJ advice method itself.

Returns:
a composable pointcut that builds on the original AspectJ expression pointcut
See Also:
getPointcut()

getAspectInstanceFactory

public final AspectInstanceFactory getAspectInstanceFactory()
Return the factory for aspect instances.


getAspectClassLoader

public final java.lang.ClassLoader getAspectClassLoader()
Return the ClassLoader for aspect instances.


getOrder

public int getOrder()
Description copied from interface: Ordered
Return the order value of this object, with a higher value meaning greater in terms of sorting.

Normally starting with 0, with Integer.MAX_VALUE indicating the greatest value. Same order values will result in arbitrary positions for the affected objects.

Higher values can be interpreted as lower priority. As a consequence, the object with the lowest value has highest priority (somewhat analogous to Servlet "load-on-startup" values).

Specified by:
getOrder in interface Ordered
Returns:
the order value

setAspectName

public void setAspectName(java.lang.String name)

getAspectName

public java.lang.String getAspectName()
Description copied from interface: AspectJPrecedenceInformation
The name of the aspect (bean) in which the advice was declared.

Specified by:
getAspectName in interface AspectJPrecedenceInformation

setDeclarationOrder

public void setDeclarationOrder(int order)
Sets the declaration order of this advice within the aspect


getDeclarationOrder

public int getDeclarationOrder()
Description copied from interface: AspectJPrecedenceInformation
The declaration order of the advice member within the aspect.

Specified by:
getDeclarationOrder in interface AspectJPrecedenceInformation

setArgumentNames

public void setArgumentNames(java.lang.String argNames)
Set by creator of this advice object if the argument names are known.

This could be for example because they have been explicitly specified in XML, or in an advice annotation.

Parameters:
argNames - comma delimited list of arg names

setArgumentNamesFromStringArray

public void setArgumentNamesFromStringArray(java.lang.String[] args)

setReturningName

public void setReturningName(java.lang.String name)

setReturningNameNoCheck

protected void setReturningNameNoCheck(java.lang.String name)
We need to hold the returning name at this level for argument binding calculations, this method allows the afterReturning advice subclass to set the name.


getDiscoveredReturningType

protected java.lang.Class getDiscoveredReturningType()

getDiscoveredReturningGenericType

protected java.lang.reflect.Type getDiscoveredReturningGenericType()

setThrowingName

public void setThrowingName(java.lang.String name)

setThrowingNameNoCheck

protected void setThrowingNameNoCheck(java.lang.String name)
We need to hold the throwing name at this level for argument binding calculations, this method allows the afterThrowing advice subclass to set the name.


getDiscoveredThrowingType

protected java.lang.Class getDiscoveredThrowingType()

isVariableName

private boolean isVariableName(java.lang.String name)

calculateArgumentBindings

public final void calculateArgumentBindings()
Do as much work as we can as part of the set-up so that argument binding on subsequent advice invocations can be as fast as possible.

If the first argument is of type JoinPoint or ProceedingJoinPoint then we pass a JoinPoint in that position (ProceedingJoinPoint for around advice).

If the first argument is of type JoinPoint.StaticPart then we pass a JoinPoint.StaticPart in that position.

Remaining arguments have to be bound by pointcut evaluation at a given join point. We will get back a map from argument name to value. We need to calculate which advice parameter needs to be bound to which argument name. There are multiple strategies for determining this binding, which are arranged in a ChainOfResponsibility.


maybeBindJoinPoint

private boolean maybeBindJoinPoint(java.lang.Class candidateParameterType)

maybeBindProceedingJoinPoint

private boolean maybeBindProceedingJoinPoint(java.lang.Class candidateParameterType)

supportsProceedingJoinPoint

protected boolean supportsProceedingJoinPoint()

maybeBindJoinPointStaticPart

private boolean maybeBindJoinPointStaticPart(java.lang.Class candidateParameterType)

bindArgumentsByName

private void bindArgumentsByName(int numArgumentsExpectingToBind)

createParameterNameDiscoverer

protected ParameterNameDiscoverer createParameterNameDiscoverer()
Create a ParameterNameDiscoverer to be used for argument binding.

The default implementation creates a PrioritizedParameterNameDiscoverer containing a LocalVariableTableParameterNameDiscoverer and an AspectJAdviceParameterNameDiscoverer.


bindExplicitArguments

private void bindExplicitArguments(int numArgumentsLeftToBind)

configurePointcutParameters

private void configurePointcutParameters(int argumentIndexOffset)
All parameters from argumentIndexOffset onwards are candidates for pointcut parameters - but returning and throwing vars are handled differently and must be removed from the list if present.


argBinding

protected java.lang.Object[] argBinding(JoinPoint jp,
                                        JoinPointMatch jpMatch,
                                        java.lang.Object returnValue,
                                        java.lang.Throwable ex)
Take the arguments at the method execution join point and output a set of arguments to the advice method

Parameters:
jp - the current JoinPoint
jpMatch - the join point match that matched this execution join point
returnValue - the return value from the method execution (may be null)
ex - the exception thrown by the method execution (may be null)
Returns:
the empty array if there are no arguments

invokeAdviceMethod

protected java.lang.Object invokeAdviceMethod(JoinPointMatch jpMatch,
                                              java.lang.Object returnValue,
                                              java.lang.Throwable ex)
                                       throws java.lang.Throwable
Invoke the advice method.

Parameters:
jpMatch - the JoinPointMatch that matched this execution join point
returnValue - the return value from the method execution (may be null)
ex - the exception thrown by the method execution (may be null)
Returns:
the invocation result
Throws:
java.lang.Throwable - in case of invocation failure

invokeAdviceMethod

protected java.lang.Object invokeAdviceMethod(JoinPoint jp,
                                              JoinPointMatch jpMatch,
                                              java.lang.Object returnValue,
                                              java.lang.Throwable t)
                                       throws java.lang.Throwable
Throws:
java.lang.Throwable

invokeAdviceMethodWithGivenArgs

protected java.lang.Object invokeAdviceMethodWithGivenArgs(java.lang.Object[] args)
                                                    throws java.lang.Throwable
Throws:
java.lang.Throwable

getJoinPoint

protected JoinPoint getJoinPoint()
Overridden in around advice to return proceeding join point.


getJoinPointMatch

protected JoinPointMatch getJoinPointMatch()
Get the current join point match at the join point we are being dispatched on.


getJoinPointMatch

protected JoinPointMatch getJoinPointMatch(ProxyMethodInvocation pmi)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object