diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java index c6b16f5c9de..bf2eb3e4505 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java @@ -121,21 +121,21 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac */ @SuppressWarnings("unchecked") @Nullable - protected static AspectJAnnotation findAspectJAnnotationOnMethod(Method method) { - for (Class clazz : ASPECTJ_ANNOTATION_CLASSES) { - AspectJAnnotation foundAnnotation = findAnnotation(method, (Class) clazz); - if (foundAnnotation != null) { - return foundAnnotation; + protected static AspectJAnnotation findAspectJAnnotationOnMethod(Method method) { + for (Class annotationType : ASPECTJ_ANNOTATION_CLASSES) { + AspectJAnnotation annotation = findAnnotation(method, (Class) annotationType); + if (annotation != null) { + return annotation; } } return null; } @Nullable - private static AspectJAnnotation findAnnotation(Method method, Class toLookFor) { - A result = AnnotationUtils.findAnnotation(method, toLookFor); - if (result != null) { - return new AspectJAnnotation<>(result); + private static AspectJAnnotation findAnnotation(Method method, Class annotationType) { + Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType); + if (annotation != null) { + return new AspectJAnnotation(annotation); } else { return null; @@ -156,9 +156,8 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac /** * Class modeling an AspectJ annotation, exposing its type enumeration and * pointcut String. - * @param the annotation type */ - protected static class AspectJAnnotation { + protected static class AspectJAnnotation { private static final String[] EXPRESSION_ATTRIBUTES = {"pointcut", "value"}; @@ -171,7 +170,7 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac AfterThrowing.class, AspectJAnnotationType.AtAfterThrowing // ); - private final A annotation; + private final Annotation annotation; private final AspectJAnnotationType annotationType; @@ -179,11 +178,11 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac private final String argumentNames; - public AspectJAnnotation(A annotation) { + public AspectJAnnotation(Annotation annotation) { this.annotation = annotation; this.annotationType = determineAnnotationType(annotation); try { - this.pointcutExpression = resolveExpression(annotation); + this.pointcutExpression = resolvePointcutExpression(annotation); Object argNames = AnnotationUtils.getValue(annotation, "argNames"); this.argumentNames = (argNames instanceof String names ? names : ""); } @@ -192,7 +191,7 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac } } - private AspectJAnnotationType determineAnnotationType(A annotation) { + private AspectJAnnotationType determineAnnotationType(Annotation annotation) { AspectJAnnotationType type = annotationTypeMap.get(annotation.annotationType()); if (type != null) { return type; @@ -200,21 +199,21 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac throw new IllegalStateException("Unknown annotation type: " + annotation); } - private String resolveExpression(A annotation) { + private String resolvePointcutExpression(Annotation annotation) { for (String attributeName : EXPRESSION_ATTRIBUTES) { Object val = AnnotationUtils.getValue(annotation, attributeName); if (val instanceof String str && !str.isEmpty()) { return str; } } - throw new IllegalStateException("Failed to resolve expression in: " + annotation); + throw new IllegalStateException("Failed to resolve pointcut expression in: " + annotation); } public AspectJAnnotationType getAnnotationType() { return this.annotationType; } - public A getAnnotation() { + public Annotation getAnnotation() { return this.annotation; } @@ -239,19 +238,22 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac */ private static class AspectJAnnotationParameterNameDiscoverer implements ParameterNameDiscoverer { + private static final String[] EMPTY_ARRAY = new String[0]; + @Override @Nullable public String[] getParameterNames(Method method) { if (method.getParameterCount() == 0) { - return new String[0]; + return EMPTY_ARRAY; } - AspectJAnnotation annotation = findAspectJAnnotationOnMethod(method); + AspectJAnnotation annotation = findAspectJAnnotationOnMethod(method); if (annotation == null) { return null; } StringTokenizer nameTokens = new StringTokenizer(annotation.getArgumentNames(), ","); - if (nameTokens.countTokens() > 0) { - String[] names = new String[nameTokens.countTokens()]; + int numTokens = nameTokens.countTokens(); + if (numTokens > 0) { + String[] names = new String[numTokens]; for (int i = 0; i < names.length; i++) { names[i] = nameTokens.nextToken(); } diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java index f842e50ed9f..74a2e1b3c43 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/InstantiationModelAwarePointcutAdvisorImpl.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +35,8 @@ import org.springframework.lang.Nullable; /** * Internal implementation of AspectJPointcutAdvisor. - * Note that there will be one instance of this advisor for each target method. + * + *

Note that there will be one instance of this advisor for each target method. * * @author Rod Johnson * @author Juergen Hoeller @@ -212,7 +213,7 @@ final class InstantiationModelAwarePointcutAdvisorImpl * creation of the advice. */ private void determineAdviceType() { - AspectJAnnotation aspectJAnnotation = + AspectJAnnotation aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(this.aspectJAdviceMethod); if (aspectJAnnotation == null) { this.isBeforeAdvice = false; diff --git a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java index 00e7ad567ef..4e2f5c3bd80 100644 --- a/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java +++ b/spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -87,7 +87,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto new InstanceComparator<>( Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class), (Converter) method -> { - AspectJAnnotation ann = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method); + AspectJAnnotation ann = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method); return (ann != null ? ann.getAnnotation() : null); }); Comparator methodNameComparator = new ConvertingComparator<>(Method::getName); @@ -216,7 +216,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto @Nullable private AspectJExpressionPointcut getPointcut(Method candidateAdviceMethod, Class candidateAspectClass) { - AspectJAnnotation aspectJAnnotation = + AspectJAnnotation aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod); if (aspectJAnnotation == null) { return null; @@ -240,7 +240,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto Class candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass(); validate(candidateAspectClass); - AspectJAnnotation aspectJAnnotation = + AspectJAnnotation aspectJAnnotation = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod); if (aspectJAnnotation == null) { return null;