Simplify AbstractAspectJAdvisorFactory internals
This commit is contained in:
parent
d2a4ac519c
commit
9d28fe90f5
|
|
@ -121,21 +121,21 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Nullable
|
@Nullable
|
||||||
protected static AspectJAnnotation<?> findAspectJAnnotationOnMethod(Method method) {
|
protected static AspectJAnnotation findAspectJAnnotationOnMethod(Method method) {
|
||||||
for (Class<?> clazz : ASPECTJ_ANNOTATION_CLASSES) {
|
for (Class<?> annotationType : ASPECTJ_ANNOTATION_CLASSES) {
|
||||||
AspectJAnnotation<?> foundAnnotation = findAnnotation(method, (Class<Annotation>) clazz);
|
AspectJAnnotation annotation = findAnnotation(method, (Class<Annotation>) annotationType);
|
||||||
if (foundAnnotation != null) {
|
if (annotation != null) {
|
||||||
return foundAnnotation;
|
return annotation;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static <A extends Annotation> AspectJAnnotation<A> findAnnotation(Method method, Class<A> toLookFor) {
|
private static AspectJAnnotation findAnnotation(Method method, Class<? extends Annotation> annotationType) {
|
||||||
A result = AnnotationUtils.findAnnotation(method, toLookFor);
|
Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
|
||||||
if (result != null) {
|
if (annotation != null) {
|
||||||
return new AspectJAnnotation<>(result);
|
return new AspectJAnnotation(annotation);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -156,9 +156,8 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
|
||||||
/**
|
/**
|
||||||
* Class modeling an AspectJ annotation, exposing its type enumeration and
|
* Class modeling an AspectJ annotation, exposing its type enumeration and
|
||||||
* pointcut String.
|
* pointcut String.
|
||||||
* @param <A> the annotation type
|
|
||||||
*/
|
*/
|
||||||
protected static class AspectJAnnotation<A extends Annotation> {
|
protected static class AspectJAnnotation {
|
||||||
|
|
||||||
private static final String[] EXPRESSION_ATTRIBUTES = {"pointcut", "value"};
|
private static final String[] EXPRESSION_ATTRIBUTES = {"pointcut", "value"};
|
||||||
|
|
||||||
|
|
@ -171,7 +170,7 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
|
||||||
AfterThrowing.class, AspectJAnnotationType.AtAfterThrowing //
|
AfterThrowing.class, AspectJAnnotationType.AtAfterThrowing //
|
||||||
);
|
);
|
||||||
|
|
||||||
private final A annotation;
|
private final Annotation annotation;
|
||||||
|
|
||||||
private final AspectJAnnotationType annotationType;
|
private final AspectJAnnotationType annotationType;
|
||||||
|
|
||||||
|
|
@ -179,11 +178,11 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
|
||||||
|
|
||||||
private final String argumentNames;
|
private final String argumentNames;
|
||||||
|
|
||||||
public AspectJAnnotation(A annotation) {
|
public AspectJAnnotation(Annotation annotation) {
|
||||||
this.annotation = annotation;
|
this.annotation = annotation;
|
||||||
this.annotationType = determineAnnotationType(annotation);
|
this.annotationType = determineAnnotationType(annotation);
|
||||||
try {
|
try {
|
||||||
this.pointcutExpression = resolveExpression(annotation);
|
this.pointcutExpression = resolvePointcutExpression(annotation);
|
||||||
Object argNames = AnnotationUtils.getValue(annotation, "argNames");
|
Object argNames = AnnotationUtils.getValue(annotation, "argNames");
|
||||||
this.argumentNames = (argNames instanceof String names ? names : "");
|
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());
|
AspectJAnnotationType type = annotationTypeMap.get(annotation.annotationType());
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
return type;
|
return type;
|
||||||
|
|
@ -200,21 +199,21 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
|
||||||
throw new IllegalStateException("Unknown annotation type: " + annotation);
|
throw new IllegalStateException("Unknown annotation type: " + annotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String resolveExpression(A annotation) {
|
private String resolvePointcutExpression(Annotation annotation) {
|
||||||
for (String attributeName : EXPRESSION_ATTRIBUTES) {
|
for (String attributeName : EXPRESSION_ATTRIBUTES) {
|
||||||
Object val = AnnotationUtils.getValue(annotation, attributeName);
|
Object val = AnnotationUtils.getValue(annotation, attributeName);
|
||||||
if (val instanceof String str && !str.isEmpty()) {
|
if (val instanceof String str && !str.isEmpty()) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("Failed to resolve expression in: " + annotation);
|
throw new IllegalStateException("Failed to resolve pointcut expression in: " + annotation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public AspectJAnnotationType getAnnotationType() {
|
public AspectJAnnotationType getAnnotationType() {
|
||||||
return this.annotationType;
|
return this.annotationType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public A getAnnotation() {
|
public Annotation getAnnotation() {
|
||||||
return this.annotation;
|
return this.annotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -239,19 +238,22 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
|
||||||
*/
|
*/
|
||||||
private static class AspectJAnnotationParameterNameDiscoverer implements ParameterNameDiscoverer {
|
private static class AspectJAnnotationParameterNameDiscoverer implements ParameterNameDiscoverer {
|
||||||
|
|
||||||
|
private static final String[] EMPTY_ARRAY = new String[0];
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
public String[] getParameterNames(Method method) {
|
public String[] getParameterNames(Method method) {
|
||||||
if (method.getParameterCount() == 0) {
|
if (method.getParameterCount() == 0) {
|
||||||
return new String[0];
|
return EMPTY_ARRAY;
|
||||||
}
|
}
|
||||||
AspectJAnnotation<?> annotation = findAspectJAnnotationOnMethod(method);
|
AspectJAnnotation annotation = findAspectJAnnotationOnMethod(method);
|
||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
StringTokenizer nameTokens = new StringTokenizer(annotation.getArgumentNames(), ",");
|
StringTokenizer nameTokens = new StringTokenizer(annotation.getArgumentNames(), ",");
|
||||||
if (nameTokens.countTokens() > 0) {
|
int numTokens = nameTokens.countTokens();
|
||||||
String[] names = new String[nameTokens.countTokens()];
|
if (numTokens > 0) {
|
||||||
|
String[] names = new String[numTokens];
|
||||||
for (int i = 0; i < names.length; i++) {
|
for (int i = 0; i < names.length; i++) {
|
||||||
names[i] = nameTokens.nextToken();
|
names[i] = nameTokens.nextToken();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.
|
* Internal implementation of AspectJPointcutAdvisor.
|
||||||
* Note that there will be one instance of this advisor for each target method.
|
*
|
||||||
|
* <p>Note that there will be one instance of this advisor for each target method.
|
||||||
*
|
*
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
|
@ -212,7 +213,7 @@ final class InstantiationModelAwarePointcutAdvisorImpl
|
||||||
* creation of the advice.
|
* creation of the advice.
|
||||||
*/
|
*/
|
||||||
private void determineAdviceType() {
|
private void determineAdviceType() {
|
||||||
AspectJAnnotation<?> aspectJAnnotation =
|
AspectJAnnotation aspectJAnnotation =
|
||||||
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(this.aspectJAdviceMethod);
|
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(this.aspectJAdviceMethod);
|
||||||
if (aspectJAnnotation == null) {
|
if (aspectJAnnotation == null) {
|
||||||
this.isBeforeAdvice = false;
|
this.isBeforeAdvice = false;
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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<>(
|
new InstanceComparator<>(
|
||||||
Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class),
|
Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class),
|
||||||
(Converter<Method, Annotation>) method -> {
|
(Converter<Method, Annotation>) method -> {
|
||||||
AspectJAnnotation<?> ann = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method);
|
AspectJAnnotation ann = AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(method);
|
||||||
return (ann != null ? ann.getAnnotation() : null);
|
return (ann != null ? ann.getAnnotation() : null);
|
||||||
});
|
});
|
||||||
Comparator<Method> methodNameComparator = new ConvertingComparator<>(Method::getName);
|
Comparator<Method> methodNameComparator = new ConvertingComparator<>(Method::getName);
|
||||||
|
|
@ -216,7 +216,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private AspectJExpressionPointcut getPointcut(Method candidateAdviceMethod, Class<?> candidateAspectClass) {
|
private AspectJExpressionPointcut getPointcut(Method candidateAdviceMethod, Class<?> candidateAspectClass) {
|
||||||
AspectJAnnotation<?> aspectJAnnotation =
|
AspectJAnnotation aspectJAnnotation =
|
||||||
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
|
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
|
||||||
if (aspectJAnnotation == null) {
|
if (aspectJAnnotation == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -240,7 +240,7 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto
|
||||||
Class<?> candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
|
Class<?> candidateAspectClass = aspectInstanceFactory.getAspectMetadata().getAspectClass();
|
||||||
validate(candidateAspectClass);
|
validate(candidateAspectClass);
|
||||||
|
|
||||||
AspectJAnnotation<?> aspectJAnnotation =
|
AspectJAnnotation aspectJAnnotation =
|
||||||
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
|
AbstractAspectJAdvisorFactory.findAspectJAnnotationOnMethod(candidateAdviceMethod);
|
||||||
if (aspectJAnnotation == null) {
|
if (aspectJAnnotation == null) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue