类 AspectJAdviceParameterNameDiscoverer
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 returningName has been set, and there are no unbound arguments
then an
IllegalArgumentExceptionis 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 <returningName>. - 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 will simply return
null. If the raiseExceptions
property is set to true, the conditions will be thrown as
IllegalArgumentException and AmbiguousBindingException,
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 the raiseExceptions
property to true, descriptive exceptions will be thrown instead of
returning null in the case that the parameter names cannot be discovered.
- 从以下版本开始:
- 4.0
- 作者:
- Adrian Colyer, Juergen Hoeller
-
嵌套类概要
嵌套类修饰符和类型类说明static classThrown in response to an ambiguous binding being detected when trying to resolve a method's parameter names.private static classSimple struct to hold the extracted text from a pointcut body, together with the number of tokens consumed in extracting it. -
字段概要
字段修饰符和类型字段说明private Class<?>[]private intprivate String[]private final StringThe pointcut expression associated with the advice, as a simple String.private booleanprivate StringIf the advice is afterReturning, and binds the return value, this is the parameter name used.private static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final Stringprivate static final Stringprivate StringIf the advice is afterThrowing, and binds the thrown value, this is the parameter name used. -
构造器概要
构造器构造器说明AspectJAdviceParameterNameDiscoverer(String pointcutExpression) Create a new discoverer that attempts to discover parameter names. -
方法概要
修饰符和类型方法说明private booleanalreadyBound(String varName) private voidbindAnnotationsFromVarNames(List<String> varNames) Match the given list of extracted variable names to argument slots.private voidbindParameterName(int index, String name) private intprivate intprivate voidfindAndBind(Class<?> argumentType, String varName) String[]getParameterNames(Executable method) Deduce the parameter names for an advice method.getPointcutBody(String[] tokens, int startIndex) private booleanisSubtypeOf(Class<?> supertype, int argumentNumber) private booleanisUnbound(int i) private voidParse the string pointcut expression looking for: @this, @target, @args, @within, @withincode, @annotation.private voidMatch up args against unbound arguments of primitive types.private voidprivate voidIf a returning variable was specified and there is only one choice remaining, bind it.private booleanIf the first parameter is of type JoinPoint or ProceedingJoinPoint,bind "thisJoinPoint" as parameter name and return true, else return false.private voidprivate voidParse the string pointcut expression looking for this(), target() and args() expressions.private voidIf a throwing name was specified and there is exactly one choice remaining (argument that is a subtype of Throwable) then bind it.private StringmaybeExtractVariableName(String candidateToken) private voidmaybeExtractVariableNamesFromArgs(String argsSpec, List<String> varNames) Given an args pointcut body (could beargsorat_args), add any candidate variable names to the given list.voidsetRaiseExceptions(boolean raiseExceptions) Indicate whetherIllegalArgumentExceptionandAspectJAdviceParameterNameDiscoverer.AmbiguousBindingExceptionmust be thrown as appropriate in the case of failing to deduce advice parameter names.voidsetReturningName(String returningName) IfafterReturningadvice binds the return value, the returning variable name must be specified.voidsetThrowingName(String throwingName) IfafterThrowingadvice binds the thrown value, the throwing variable name must be specified.从类继承的方法 infra.core.ParameterNameDiscoverer
doGet, findParameterNames, getSharedInstance
-
字段详细资料
-
THIS_JOIN_POINT
- 另请参阅:
-
THIS_JOIN_POINT_STATIC_PART
- 另请参阅:
-
STEP_JOIN_POINT_BINDING
private static final int STEP_JOIN_POINT_BINDING- 另请参阅:
-
STEP_THROWING_BINDING
private static final int STEP_THROWING_BINDING- 另请参阅:
-
STEP_ANNOTATION_BINDING
private static final int STEP_ANNOTATION_BINDING- 另请参阅:
-
STEP_RETURNING_BINDING
private static final int STEP_RETURNING_BINDING- 另请参阅:
-
STEP_PRIMITIVE_ARGS_BINDING
private static final int STEP_PRIMITIVE_ARGS_BINDING- 另请参阅:
-
STEP_THIS_TARGET_ARGS_BINDING
private static final int STEP_THIS_TARGET_ARGS_BINDING- 另请参阅:
-
STEP_REFERENCE_PCUT_BINDING
private static final int STEP_REFERENCE_PCUT_BINDING- 另请参阅:
-
STEP_FINISHED
private static final int STEP_FINISHED- 另请参阅:
-
singleValuedAnnotationPcds
-
nonReferencePointcutTokens
-
pointcutExpression
The pointcut expression associated with the advice, as a simple String. -
raiseExceptions
private boolean raiseExceptions -
returningName
If the advice is afterReturning, and binds the return value, this is the parameter name used. -
throwingName
If the advice is afterThrowing, and binds the thrown value, this is the parameter name used. -
argumentTypes
-
parameterNameBindings
-
numberOfRemainingUnboundArguments
private int numberOfRemainingUnboundArguments
-
-
构造器详细资料
-
AspectJAdviceParameterNameDiscoverer
Create a new discoverer that attempts to discover parameter names. from the given pointcut expression.
-
-
方法详细资料
-
setRaiseExceptions
public void setRaiseExceptions(boolean raiseExceptions) Indicate whetherIllegalArgumentExceptionandAspectJAdviceParameterNameDiscoverer.AmbiguousBindingExceptionmust be thrown as appropriate in the case of failing to deduce advice parameter names.- 参数:
raiseExceptions-trueif exceptions are to be thrown
-
setReturningName
IfafterReturningadvice binds the return value, the returning variable name must be specified.- 参数:
returningName- the name of the returning variable
-
setThrowingName
IfafterThrowingadvice binds the thrown value, the throwing variable name must be specified.- 参数:
throwingName- the name of the throwing variable
-
getParameterNames
Deduce the parameter names for an advice method.See the
class level javadocfor this class for details of the algorithm used.- 覆盖:
getParameterNames在类中infra.core.ParameterNameDiscoverer- 参数:
method- the targetMethod- 返回:
- the parameter names
-
bindParameterName
-
maybeBindThisJoinPoint
private boolean maybeBindThisJoinPoint()If the first parameter is of type JoinPoint or ProceedingJoinPoint,bind "thisJoinPoint" as parameter name and return true, else return false. -
maybeBindThisJoinPointStaticPart
private void maybeBindThisJoinPointStaticPart() -
maybeBindThrowingVariable
private void maybeBindThrowingVariable()If a throwing name was specified and there is exactly one choice remaining (argument that is a subtype of Throwable) then bind it. -
maybeBindReturningVariable
private void maybeBindReturningVariable()If a returning variable was specified and there is only one choice remaining, bind it. -
maybeBindAnnotationsFromPointcutExpression
private void maybeBindAnnotationsFromPointcutExpression()Parse the string pointcut expression looking for: @this, @target, @args, @within, @withincode, @annotation. If we find one of these pointcut expressions, try and extract a candidate variable name (or variable names, in the case of args).Some more support from AspectJ in doing this exercise would be nice... :)
-
bindAnnotationsFromVarNames
Match the given list of extracted variable names to argument slots. -
maybeExtractVariableName
-
maybeExtractVariableNamesFromArgs
Given an args pointcut body (could beargsorat_args), add any candidate variable names to the given list. -
maybeBindThisOrTargetOrArgsFromPointcutExpression
private void maybeBindThisOrTargetOrArgsFromPointcutExpression()Parse the string pointcut expression looking for this(), target() and args() expressions. If we find one, try and extract a candidate variable name and bind it. -
maybeBindReferencePointcutParameter
private void maybeBindReferencePointcutParameter() -
getPointcutBody
private AspectJAdviceParameterNameDiscoverer.PointcutBody getPointcutBody(String[] tokens, int startIndex) -
maybeBindPrimitiveArgsFromPointcutExpression
private void maybeBindPrimitiveArgsFromPointcutExpression()Match up args against unbound arguments of primitive types. -
isUnbound
private boolean isUnbound(int i) -
alreadyBound
-
isSubtypeOf
-
countNumberOfUnboundAnnotationArguments
private int countNumberOfUnboundAnnotationArguments() -
countNumberOfUnboundPrimitiveArguments
private int countNumberOfUnboundPrimitiveArguments() -
findAndBind
-