Async annotations on interface methods with CGLIB proxies

Issue: SPR-14949
This commit is contained in:
Juergen Hoeller
2017-04-26 18:16:40 +02:00
parent 42d6d7ec4e
commit 6c43d14a77
5 changed files with 61 additions and 22 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2017 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.
@@ -48,9 +48,10 @@ public class AnnotationClassFilter implements ClassFilter {
/**
* Create a new AnnotationClassFilter for the given annotation type.
* @param annotationType the annotation type to look for
* @param checkInherited whether to explicitly check the superclasses and
* interfaces for the annotation type as well (even if the annotation type
* is not marked as inherited itself)
* @param checkInherited whether to also check the superclasses and
* interfaces as well as meta-annotations for the annotation type
* (i.e. whether to use {@link AnnotationUtils#findAnnotation(Class, Class)}
* semantics instead of standard Java {@link Class#isAnnotationPresent})
*/
public AnnotationClassFilter(Class<? extends Annotation> annotationType, boolean checkInherited) {
Assert.notNull(annotationType, "Annotation type must not be null");

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2017 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.
@@ -46,16 +46,15 @@ public class AnnotationMatchingPointcut implements Pointcut {
* @param classAnnotationType the annotation type to look for at the class level
*/
public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationType) {
this.classFilter = new AnnotationClassFilter(classAnnotationType);
this.methodMatcher = MethodMatcher.TRUE;
this(classAnnotationType, false);
}
/**
* Create a new AnnotationMatchingPointcut for the given annotation type.
* @param classAnnotationType the annotation type to look for at the class level
* @param checkInherited whether to explicitly check the superclasses and
* interfaces for the annotation type as well (even if the annotation type
* is not marked as inherited itself)
* @param checkInherited whether to also check the superclasses and interfaces
* as well as meta-annotations for the annotation type
* @see AnnotationClassFilter#AnnotationClassFilter(Class, boolean)
*/
public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationType, boolean checkInherited) {
this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited);
@@ -72,18 +71,36 @@ public class AnnotationMatchingPointcut implements Pointcut {
public AnnotationMatchingPointcut(
Class<? extends Annotation> classAnnotationType, Class<? extends Annotation> methodAnnotationType) {
this(classAnnotationType, methodAnnotationType, false);
}
/**
* Create a new AnnotationMatchingPointcut for the given annotation type.
* @param classAnnotationType the annotation type to look for at the class level
* (can be {@code null})
* @param methodAnnotationType the annotation type to look for at the method level
* (can be {@code null})
* @param checkInherited whether to also check the superclasses and interfaces
* as well as meta-annotations for the annotation type
* @since 5.0
* @see AnnotationClassFilter#AnnotationClassFilter(Class, boolean)
* @see AnnotationMethodMatcher#AnnotationMethodMatcher(Class, boolean)
*/
public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationType,
Class<? extends Annotation> methodAnnotationType, boolean checkInherited) {
Assert.isTrue((classAnnotationType != null || methodAnnotationType != null),
"Either Class annotation type or Method annotation type needs to be specified (or both)");
if (classAnnotationType != null) {
this.classFilter = new AnnotationClassFilter(classAnnotationType);
this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited);
}
else {
this.classFilter = ClassFilter.TRUE;
}
if (methodAnnotationType != null) {
this.methodMatcher = new AnnotationMethodMatcher(methodAnnotationType);
this.methodMatcher = new AnnotationMethodMatcher(methodAnnotationType, checkInherited);
}
else {
this.methodMatcher = MethodMatcher.TRUE;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2017 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.
@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import org.springframework.aop.support.AopUtils;
import org.springframework.aop.support.StaticMethodMatcher;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
/**
@@ -36,25 +37,48 @@ public class AnnotationMethodMatcher extends StaticMethodMatcher {
private final Class<? extends Annotation> annotationType;
private final boolean checkInherited;
/**
* Create a new AnnotationClassFilter for the given annotation type.
* @param annotationType the annotation type to look for
*/
public AnnotationMethodMatcher(Class<? extends Annotation> annotationType) {
this(annotationType, false);
}
/**
* Create a new AnnotationClassFilter for the given annotation type.
* @param annotationType the annotation type to look for
* @param checkInherited whether to also check the superclasses and
* interfaces as well as meta-annotations for the annotation type
* (i.e. whether to use {@link AnnotationUtils#findAnnotation(Method, Class)}
* semantics instead of standard Java {@link Method#isAnnotationPresent})
* @since 5.0
*/
public AnnotationMethodMatcher(Class<? extends Annotation> annotationType, boolean checkInherited) {
Assert.notNull(annotationType, "Annotation type must not be null");
this.annotationType = annotationType;
this.checkInherited = checkInherited;
}
@Override
public boolean matches(Method method, Class<?> targetClass) {
if (method.isAnnotationPresent(this.annotationType)) {
if (matchesMethod(method)) {
return true;
}
// The method may be on an interface, so let's check on the target class as well.
Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
return (specificMethod != method && specificMethod.isAnnotationPresent(this.annotationType));
return (specificMethod != method && matchesMethod(specificMethod));
}
private boolean matchesMethod(Method method) {
return (this.checkInherited ?
(AnnotationUtils.findAnnotation(method, this.annotationType) != null) :
method.isAnnotationPresent(this.annotationType));
}
@Override