@Override public String toString() { StringBuilder sb = new StringBuilder(); Advice advice = this.advisor.getAdvice(); sb.append(ClassUtils.getShortName(advice.getClass())); sb.append(": "); if (this.advisor instanceof Ordered) { sb.append("order ").append(((Ordered) this.advisor).getOrder()).append(", "); } if (advice instanceof AbstractAspectJAdvice) { AbstractAspectJAdvice ajAdvice = (AbstractAspectJAdvice) advice; sb.append(ajAdvice.getAspectName()); sb.append(", declaration order "); sb.append(ajAdvice.getDeclarationOrder()); } return sb.toString(); } }
/** * Overridden in around advice to return proceeding join point. */ protected JoinPoint getJoinPoint() { return currentJoinPoint(); }
private Advisor createAspectJAdvice(int advisorOrder, int adviceDeclarationOrder, String aspectName, AbstractAspectJAdvice advice) { advice.setDeclarationOrder(adviceDeclarationOrder); advice.setAspectName(aspectName); AspectJPointcutAdvisor advisor = new AspectJPointcutAdvisor(advice); advisor.setOrder(advisorOrder); return advisor; }
/** * Invoke the advice method. * @param jpMatch the JoinPointMatch that matched this execution join point * @param returnValue the return value from the method execution (may be null) * @param ex the exception thrown by the method execution (may be null) * @return the invocation result * @throws Throwable in case of invocation failure */ protected Object invokeAdviceMethod( @Nullable JoinPointMatch jpMatch, @Nullable Object returnValue, @Nullable Throwable ex) throws Throwable { return invokeAdviceMethodWithGivenArgs(argBinding(getJoinPoint(), jpMatch, returnValue, ex)); }
protected Object invokeAdviceMethod(JoinPoint jp, @Nullable JoinPointMatch jpMatch, @Nullable Object returnValue, @Nullable Throwable t) throws Throwable { return invokeAdviceMethodWithGivenArgs(argBinding(jp, jpMatch, returnValue, t)); }
/** * 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. * <p>If the first argument is of type JoinPoint or ProceedingJoinPoint then we * pass a JoinPoint in that position (ProceedingJoinPoint for around advice). * <p>If the first argument is of type {@code JoinPoint.StaticPart} * then we pass a {@code JoinPoint.StaticPart} in that position. * <p>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. */ public final synchronized void calculateArgumentBindings() { // The simple case... nothing to bind. if (this.argumentsIntrospected || this.parameterTypes.length == 0) { return; } int numUnboundArgs = this.parameterTypes.length; Class<?>[] parameterTypes = this.aspectJAdviceMethod.getParameterTypes(); if (maybeBindJoinPoint(parameterTypes[0]) || maybeBindProceedingJoinPoint(parameterTypes[0]) || maybeBindJoinPointStaticPart(parameterTypes[0])) { numUnboundArgs--; } if (numUnboundArgs > 0) { // need to bind arguments by name as returned from the pointcut match bindArgumentsByName(numUnboundArgs); } this.argumentsIntrospected = true; }
AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation(); if (StringUtils.hasText(afterReturningAnnotation.returning())) { springAdvice.setReturningName(afterReturningAnnotation.returning()); AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation(); if (StringUtils.hasText(afterThrowingAnnotation.throwing())) { springAdvice.setThrowingName(afterThrowingAnnotation.throwing()); springAdvice.setAspectName(aspectName); springAdvice.setDeclarationOrder(declarationOrder); String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod); if (argNames != null) { springAdvice.setArgumentNamesFromStringArray(argNames); springAdvice.calculateArgumentBindings();
/** * Return the name of the aspect (bean) in which the advice was declared. * @since 4.3.15 * @see AbstractAspectJAdvice#getAspectName() */ public String getAspectName() { return this.advice.getAspectName(); }
private void bindArgumentsByName(int numArgumentsExpectingToBind) { if (this.argumentNames == null) { this.argumentNames = createParameterNameDiscoverer().getParameterNames(this.aspectJAdviceMethod); } if (this.argumentNames != null) { // We have been able to determine the arg names. bindExplicitArguments(numArgumentsExpectingToBind); } else { throw new IllegalStateException("Advice method [" + this.aspectJAdviceMethod.getName() + "] " + "requires " + numArgumentsExpectingToBind + " arguments to be bound by name, but " + "the argument names were not specified and could not be discovered."); } }
/** * Return the AspectJ expression pointcut. */ public final AspectJExpressionPointcut getPointcut() { calculateArgumentBindings(); return this.pointcut; }
/** * Create a new AspectJPointcutAdvisor for the given advice. * @param advice the AbstractAspectJAdvice to wrap */ public AspectJPointcutAdvisor(AbstractAspectJAdvice advice) { Assert.notNull(advice, "Advice must not be null"); this.advice = advice; this.pointcut = advice.buildSafePointcut(); }
configurePointcutParameters(this.argumentNames, argumentIndexOffset);
AfterReturning afterReturningAnnotation = (AfterReturning) aspectJAnnotation.getAnnotation(); if (StringUtils.hasText(afterReturningAnnotation.returning())) { springAdvice.setReturningName(afterReturningAnnotation.returning()); AfterThrowing afterThrowingAnnotation = (AfterThrowing) aspectJAnnotation.getAnnotation(); if (StringUtils.hasText(afterThrowingAnnotation.throwing())) { springAdvice.setThrowingName(afterThrowingAnnotation.throwing()); springAdvice.setAspectName(aspectName); springAdvice.setDeclarationOrder(declarationOrder); String[] argNames = this.parameterNameDiscoverer.getParameterNames(candidateAdviceMethod); if (argNames != null) { springAdvice.setArgumentNamesFromStringArray(argNames); springAdvice.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. * <p>If the first argument is of type JoinPoint or ProceedingJoinPoint then we * pass a JoinPoint in that position (ProceedingJoinPoint for around advice). * <p>If the first argument is of type {@code JoinPoint.StaticPart} * then we pass a {@code JoinPoint.StaticPart} in that position. * <p>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. */ public final synchronized void calculateArgumentBindings() { // The simple case... nothing to bind. if (this.argumentsIntrospected || this.parameterTypes.length == 0) { return; } int numUnboundArgs = this.parameterTypes.length; Class<?>[] parameterTypes = this.aspectJAdviceMethod.getParameterTypes(); if (maybeBindJoinPoint(parameterTypes[0]) || maybeBindProceedingJoinPoint(parameterTypes[0]) || maybeBindJoinPointStaticPart(parameterTypes[0])) { numUnboundArgs--; } if (numUnboundArgs > 0) { // need to bind arguments by name as returned from the pointcut match bindArgumentsByName(numUnboundArgs); } this.argumentsIntrospected = true; }
/** * Invoke the advice method. * @param jpMatch the JoinPointMatch that matched this execution join point * @param returnValue the return value from the method execution (may be null) * @param ex the exception thrown by the method execution (may be null) * @return the invocation result * @throws Throwable in case of invocation failure */ protected Object invokeAdviceMethod( @Nullable JoinPointMatch jpMatch, @Nullable Object returnValue, @Nullable Throwable ex) throws Throwable { return invokeAdviceMethodWithGivenArgs(argBinding(getJoinPoint(), jpMatch, returnValue, ex)); }
/** * Return the name of the aspect (bean) in which the advice was declared. * @since 4.3.15 * @see AbstractAspectJAdvice#getAspectName() */ public String getAspectName() { return this.advice.getAspectName(); }
private void bindArgumentsByName(int numArgumentsExpectingToBind) { if (this.argumentNames == null) { this.argumentNames = createParameterNameDiscoverer().getParameterNames(this.aspectJAdviceMethod); } if (this.argumentNames != null) { // We have been able to determine the arg names. bindExplicitArguments(numArgumentsExpectingToBind); } else { throw new IllegalStateException("Advice method [" + this.aspectJAdviceMethod.getName() + "] " + "requires " + numArgumentsExpectingToBind + " arguments to be bound by name, but " + "the argument names were not specified and could not be discovered."); } }
protected Object invokeAdviceMethod(JoinPoint jp, @Nullable JoinPointMatch jpMatch, @Nullable Object returnValue, @Nullable Throwable t) throws Throwable { return invokeAdviceMethodWithGivenArgs(argBinding(jp, jpMatch, returnValue, t)); }
@Nullable Object returnValue, @Nullable Throwable ex) { calculateArgumentBindings();
/** * Create a new AspectJPointcutAdvisor for the given advice. * @param advice the AbstractAspectJAdvice to wrap */ public AspectJPointcutAdvisor(AbstractAspectJAdvice advice) { Assert.notNull(advice, "Advice must not be null"); this.advice = advice; this.pointcut = advice.buildSafePointcut(); }