From 7ace9aa42940b06e5829c043a7e0eed855ffd262 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 24 Feb 2023 11:19:03 +0100 Subject: [PATCH] Polish AspectJAdviceParameterNameDiscoverer This commit also converts PointcutBody to a record. --- .../AspectJAdviceParameterNameDiscoverer.java | 61 +++++++------------ 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java index 9bfd785b38d..cc0aa1fe908 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java @@ -38,6 +38,14 @@ import org.springframework.util.StringUtils; * for an advice method from the pointcut expression, returning, and throwing clauses. * If an unambiguous interpretation is not available, it returns {@code null}. * + *

Algorithm Summary

+ *

If an unambiguous binding can be deduced, then it is. + * If the advice requirements cannot possibly be satisfied, then {@code null} + * is returned. By setting the {@link #setRaiseExceptions(boolean) raiseExceptions} + * property to {@code true}, descriptive exceptions will be thrown instead of + * returning {@code null} in the case that the parameter names cannot be discovered. + * + *

Algorithm Details

*

This class interprets arguments in the following way: *

    *
  1. If the first parameter of the method is of type {@link JoinPoint} @@ -65,15 +73,15 @@ import org.springframework.util.StringUtils; * zero we proceed to the next stage. If {@code a} > 1 then an * {@code AmbiguousBindingException} is raised. If {@code a} == 1, * and there are no unbound arguments of type {@code Annotation+}, - * then an {@code IllegalArgumentException} is raised. if there is + * then an {@code IllegalArgumentException} is raised. If there is * exactly one such argument, then the corresponding parameter name is * assigned the value from the pointcut expression.
  2. - *
  3. If a returningName has been set, and there are no unbound arguments + *
  4. If a {@code returningName} has been set, and there are no unbound arguments * then an {@code IllegalArgumentException} is raised. If there is * more than one unbound argument then an * {@code AmbiguousBindingException} is raised. If there is exactly * one unbound argument then the corresponding parameter name is assigned - * the value <returningName>.
  5. + * the value of the {@code returningName}. *
  6. If there remain unbound arguments, then the pointcut expression is * examined once more for {@code this}, {@code target}, and * {@code args} pointcut expressions used in the binding form (binding @@ -99,20 +107,12 @@ import org.springframework.util.StringUtils; *

    The behavior on raising an {@code IllegalArgumentException} or * {@code 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 {@code getParameterNames(..)} method will simply return + * be logged and the {@link #getParameterNames(Method)} method will simply return * {@code null}. If the {@link #setRaiseExceptions(boolean) raiseExceptions} * property is set to {@code true}, the conditions will be thrown as * {@code IllegalArgumentException} and {@code 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 {@code null} - * is returned. By setting the {@link #setRaiseExceptions(boolean) raiseExceptions} - * property to {@code true}, descriptive exceptions will be thrown instead of - * returning {@code null} in the case that the parameter names cannot be discovered. - * * @author Adrian Colyer * @author Juergen Hoeller * @since 2.0 @@ -197,7 +197,7 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov /** * If {@code afterReturning} advice binds the return value, the - * returning variable name must be specified. + * {@code returning} variable name must be specified. * @param returningName the name of the returning variable */ public void setReturningName(@Nullable String returningName) { @@ -206,18 +206,17 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov /** * If {@code afterThrowing} advice binds the thrown value, the - * throwing variable name must be specified. + * {@code throwing} variable name must be specified. * @param throwingName the name of the throwing variable */ public void setThrowingName(@Nullable String throwingName) { this.throwingName = throwingName; } - /** * Deduce the parameter names for an advice method. - *

    See the {@link AspectJAdviceParameterNameDiscoverer class level javadoc} - * for this class for details of the algorithm used. + *

    See the {@link AspectJAdviceParameterNameDiscoverer class-level javadoc} + * for this class for details on the algorithm used. * @param method the target {@link Method} * @return the parameter names */ @@ -309,7 +308,7 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov } /** - * If the first parameter is of type JoinPoint or ProceedingJoinPoint,bind "thisJoinPoint" as + * If the first parameter is of type JoinPoint or ProceedingJoinPoint, bind "thisJoinPoint" as * parameter name and return true, else return false. */ private boolean maybeBindThisJoinPoint() { @@ -387,7 +386,6 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov } } - /** * Parse the string pointcut expression looking for: * @this, @target, @args, @within, @withincode, @annotation. @@ -452,7 +450,7 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov } } - /* + /** * If the token starts meets Java identifier conventions, it's in. */ @Nullable @@ -520,7 +518,6 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov } } - if (varNames.size() > 1) { throw new AmbiguousBindingException("Found " + varNames.size() + " candidate this(), target() or args() variables but only one unbound argument slot"); @@ -596,7 +593,7 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov // else varNames.size must be 0 and we have nothing to bind. } - /* + /** * We've found the start of a binding pointcut at the given index into the * token array. Now we need to extract the pointcut body and return it. */ @@ -696,7 +693,7 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov return false; } - /* + /** * Return {@code true} if the given argument type is a subclass * of the given supertype. */ @@ -724,7 +721,7 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov return count; } - /* + /** * Find the argument index with the given type, and bind the given * {@code varName} in that position. */ @@ -741,22 +738,10 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov /** - * Simple struct to hold the extracted text from a pointcut body, together + * Simple record to hold the extracted text from a pointcut body, together * with the number of tokens consumed in extracting it. */ - private static class PointcutBody { - - private final int numTokensConsumed; - - @Nullable - private final String text; - - public PointcutBody(int tokens, @Nullable String text) { - this.numTokensConsumed = tokens; - this.text = text; - } - } - + private record PointcutBody(int numTokensConsumed, @Nullable String text) {} /** * Thrown in response to an ambiguous binding being detected when