Class AspectJAdviceParameterNameDiscoverer
- All Implemented Interfaces:
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 returns null.
Algorithm Summary
If an unambiguous binding can be deduced, then it is.
If the advice requirements cannot possibly be satisfied, then null
is returned. By setting the raiseExceptions
property to true, descriptive exceptions will be thrown instead of
returning null in the case that the parameter names cannot be discovered.
Algorithm Details
This class interprets arguments in the following way:
- If the first parameter of the method is of type
JoinPointorProceedingJoinPoint, it is assumed to be for passingthisJoinPointto 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
throwingNamehas been set, and there are no unbound arguments of typeThrowable+, then anIllegalArgumentExceptionis raised. If there is more than one unbound argument of typeThrowable+, then anAspectJAdviceParameterNameDiscoverer.AmbiguousBindingExceptionis 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
abe 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. Ifais zero we proceed to the next stage. Ifa> 1 then anAmbiguousBindingExceptionis raised. Ifa== 1, and there are no unbound arguments of typeAnnotation+, then anIllegalArgumentExceptionis raised. If there is exactly one such argument, then the corresponding parameter name is assigned the value from the pointcut expression. - If a
returningNamehas been set, and there are no unbound arguments then anIllegalArgumentExceptionis raised. If there is more than one unbound argument then anAmbiguousBindingExceptionis raised. If there is exactly one unbound argument then the corresponding parameter name is assigned the value of thereturningName. - If there remain unbound arguments, then the pointcut expression is
examined once more for
this,target, andargspointcut 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 anAmbiguousBindingExceptionis raised. If there is exactly one argument of a primitive type, then if exactly oneargsbound variable was found, we assign the corresponding parameter name the variable name. If there were noargsbound variables found anIllegalStateExceptionis raised. If there are multipleargsbound variables, anAmbiguousBindingExceptionis 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, anAmbiguousBindingExceptionis raised.
The behavior on raising an IllegalArgumentException or
AmbiguousBindingException is configurable to allow this discoverer
to be used as part of a chain-of-responsibility. By default the condition will
be logged and the getParameterNames(Method) method will simply return
null. If the raiseExceptions
property is set to true, the conditions will be thrown as
IllegalArgumentException and AmbiguousBindingException,
respectively.
- Since:
- 2.0
- Author:
- Adrian Colyer, Juergen Hoeller
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrown in response to an ambiguous binding being detected when trying to resolve a method's parameter names. -
Constructor Summary
ConstructorsConstructorDescriptionAspectJAdviceParameterNameDiscoverer(@Nullable String pointcutExpression) Create a new discoverer that attempts to discover parameter names. -
Method Summary
Modifier and TypeMethodDescriptiongetParameterNames(Constructor<?> ctor) An advice method can never be a constructor in Spring.getParameterNames(Method method) Deduce the parameter names for an advice method.voidsetRaiseExceptions(boolean raiseExceptions) Indicate whetherIllegalArgumentExceptionandAspectJAdviceParameterNameDiscoverer.AmbiguousBindingExceptionmust be thrown as appropriate in the case of failing to deduce advice parameter names.voidsetReturningName(@Nullable String returningName) IfafterReturningadvice binds the return value, thereturningvariable name must be specified.voidsetThrowingName(@Nullable String throwingName) IfafterThrowingadvice binds the thrown value, thethrowingvariable name must be specified.
-
Constructor Details
-
AspectJAdviceParameterNameDiscoverer
-
-
Method Details
-
setRaiseExceptions
public void setRaiseExceptions(boolean raiseExceptions) Indicate whetherIllegalArgumentExceptionandAspectJAdviceParameterNameDiscoverer.AmbiguousBindingExceptionmust be thrown as appropriate in the case of failing to deduce advice parameter names.- Parameters:
raiseExceptions-trueif exceptions are to be thrown
-
setReturningName
-
setThrowingName
-
getParameterNames
Deduce the parameter names for an advice method.See the
class-level javadocfor this class for details on the algorithm used.- Specified by:
getParameterNamesin interfaceParameterNameDiscoverer- Parameters:
method- the targetMethod- Returns:
- the parameter names
-
getParameterNames
An advice method can never be a constructor in Spring.- Specified by:
getParameterNamesin interfaceParameterNameDiscoverer- Parameters:
ctor- the constructor to find parameter names for- Returns:
null- Throws:
UnsupportedOperationException- ifraiseExceptionshas been set totrue
-