Class AspectJAdviceParameterNameDiscoverer
- java.lang.Object
- org.springframework.aop.aspectj.AspectJAdviceParameterNameDiscoverer
- All Implemented Interfaces:
ParameterNameDiscoverer
public class AspectJAdviceParameterNameDiscoverer extends Object implements ParameterNameDiscoverer
ParameterNameDiscoverer
implementation that tries to deduce parameter names for an advice method from the pointcut expression, returning, and throwing clauses. If an unambiguous interpretation is not available, it returnsnull
.This class interprets arguments in the following way:
- If the first parameter of the method is of type
JoinPoint
orProceedingJoinPoint
, it is assumed to be for passingthisJoinPoint
to the advice, and the parameter name will be assigned the value"thisJoinPoint"
. - If the first parameter of the method is of type
JoinPoint.StaticPart
, it is assumed to be for passing"thisJoinPointStaticPart"
to the advice, and the parameter name will be assigned the value"thisJoinPointStaticPart"
. - If a
throwingName
has been set, and there are no unbound arguments of typeThrowable+
, then anIllegalArgumentException
is raised. If there is more than one unbound argument of typeThrowable+
, then anAspectJAdviceParameterNameDiscoverer.AmbiguousBindingException
is raised. If there is exactly one unbound argument of typeThrowable+
, then the corresponding parameter name is assigned the value <throwingName>. - If there remain unbound arguments, then the pointcut expression is examined. Let
a
be the number of annotation-based pointcut expressions (@annotation, @this, @target, @args, @within, @withincode) that are used in binding form. Usage in binding form has itself to be deduced: if the expression inside the pointcut is a single string literal that meets Java variable name conventions it is assumed to be a variable name. Ifa
is zero we proceed to the next stage. Ifa
> 1 then anAmbiguousBindingException
is raised. Ifa
== 1, and there are no unbound arguments of typeAnnotation+
, then anIllegalArgumentException
is raised. if there is exactly one such argument, then the corresponding parameter name is assigned the value from the pointcut expression. - If a returningName has been set, and there are no unbound arguments then an
IllegalArgumentException
is raised. If there is more than one unbound argument then anAmbiguousBindingException
is raised. If there is exactly one unbound argument then the corresponding parameter name is assigned the value <returningName>. - If there remain unbound arguments, then the pointcut expression is examined once more for
this
,target
, andargs
pointcut expressions used in the binding form (binding forms are deduced as described for the annotation based pointcuts). If there remains more than one unbound argument of a primitive type (which can only be bound inargs
) then anAmbiguousBindingException
is raised. If there is exactly one argument of a primitive type, then if exactly oneargs
bound variable was found, we assign the corresponding parameter name the variable name. If there were noargs
bound variables found anIllegalStateException
is raised. If there are multipleargs
bound variables, anAmbiguousBindingException
is raised. At this point, if there remains more than one unbound argument we raise anAmbiguousBindingException
. If there are no unbound arguments remaining, we are done. If there is exactly one unbound argument remaining, and only one candidate variable name unbound fromthis
,target
, orargs
, it is assigned as the corresponding parameter name. If there are multiple possibilities, anAmbiguousBindingException
is raised.
The behavior on raising an
IllegalArgumentException
orAmbiguousBindingException
is configurable to allow this discoverer to be used as part of a chain-of-responsibility. By default the condition will be logged and thegetParameterNames(..)
method will simply returnnull
. If theraiseExceptions
property is set totrue
, the conditions will be thrown asIllegalArgumentException
andAmbiguousBindingException
, respectively.Was that perfectly clear? ;)
Short version: If an unambiguous binding can be deduced, then it is. If the advice requirements cannot possibly be satisfied, then
null
is returned. By setting theraiseExceptions
property totrue
, descriptive exceptions will be thrown instead of returningnull
in the case that the parameter names cannot be discovered.- Since:
- 2.0
- Author:
- Adrian Colyer, Juergen Hoeller
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AspectJAdviceParameterNameDiscoverer.AmbiguousBindingException
Thrown in response to an ambiguous binding being detected when trying to resolve a method's parameter names.
Constructor Summary
Constructors Constructor Description AspectJAdviceParameterNameDiscoverer(String pointcutExpression)
Create a new discoverer that attempts to discover parameter names from the given pointcut expression.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String[]
getParameterNames(Constructor<?> ctor)
An advice method can never be a constructor in Spring.String[]
getParameterNames(Method method)
Deduce the parameter names for an advice method.void
setRaiseExceptions(boolean raiseExceptions)
Indicate whetherIllegalArgumentException
andAspectJAdviceParameterNameDiscoverer.AmbiguousBindingException
must be thrown as appropriate in the case of failing to deduce advice parameter names.void
setReturningName(String returningName)
IfafterReturning
advice binds the return value, the returning variable name must be specified.void
setThrowingName(String throwingName)
IfafterThrowing
advice binds the thrown value, the throwing variable name must be specified.
Constructor Detail
AspectJAdviceParameterNameDiscoverer
public AspectJAdviceParameterNameDiscoverer(String pointcutExpression)
Create a new discoverer that attempts to discover parameter names from the given pointcut expression.
Method Detail
setRaiseExceptions
public void setRaiseExceptions(boolean raiseExceptions)
Indicate whetherIllegalArgumentException
andAspectJAdviceParameterNameDiscoverer.AmbiguousBindingException
must be thrown as appropriate in the case of failing to deduce advice parameter names.- Parameters:
raiseExceptions
-true
if exceptions are to be thrown
setReturningName
public void setReturningName(String returningName)
IfafterReturning
advice binds the return value, the returning variable name must be specified.- Parameters:
returningName
- the name of the returning variable
setThrowingName
public void setThrowingName(String throwingName)
IfafterThrowing
advice binds the thrown value, the throwing variable name must be specified.- Parameters:
throwingName
- the name of the throwing variable
getParameterNames
public String[] getParameterNames(Method method)
Deduce the parameter names for an advice method.See the
class level javadoc
for this class for details of the algorithm used.- Specified by:
getParameterNames
in interfaceParameterNameDiscoverer
- Parameters:
method
- the targetMethod
- Returns:
- the parameter names
getParameterNames
public String[] getParameterNames(Constructor<?> ctor)
An advice method can never be a constructor in Spring.- Specified by:
getParameterNames
in interfaceParameterNameDiscoverer
- Parameters:
ctor
- constructor to find parameter names for- Returns:
null
- Throws:
UnsupportedOperationException
- ifraiseExceptions
has been set totrue