Skip to content

Commit 6c43d14

Browse files
committed
Async annotations on interface methods with CGLIB proxies
Issue: SPR-14949
1 parent 42d6d7e commit 6c43d14

File tree

5 files changed

+61
-22
lines changed

5 files changed

+61
-22
lines changed

spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationClassFilter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,9 +48,10 @@ public AnnotationClassFilter(Class<? extends Annotation> annotationType) {
4848
/**
4949
* Create a new AnnotationClassFilter for the given annotation type.
5050
* @param annotationType the annotation type to look for
51-
* @param checkInherited whether to explicitly check the superclasses and
52-
* interfaces for the annotation type as well (even if the annotation type
53-
* is not marked as inherited itself)
51+
* @param checkInherited whether to also check the superclasses and
52+
* interfaces as well as meta-annotations for the annotation type
53+
* (i.e. whether to use {@link AnnotationUtils#findAnnotation(Class, Class)}
54+
* semantics instead of standard Java {@link Class#isAnnotationPresent})
5455
*/
5556
public AnnotationClassFilter(Class<? extends Annotation> annotationType, boolean checkInherited) {
5657
Assert.notNull(annotationType, "Annotation type must not be null");

spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMatchingPointcut.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,16 +46,15 @@ public class AnnotationMatchingPointcut implements Pointcut {
4646
* @param classAnnotationType the annotation type to look for at the class level
4747
*/
4848
public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationType) {
49-
this.classFilter = new AnnotationClassFilter(classAnnotationType);
50-
this.methodMatcher = MethodMatcher.TRUE;
49+
this(classAnnotationType, false);
5150
}
5251

5352
/**
5453
* Create a new AnnotationMatchingPointcut for the given annotation type.
5554
* @param classAnnotationType the annotation type to look for at the class level
56-
* @param checkInherited whether to explicitly check the superclasses and
57-
* interfaces for the annotation type as well (even if the annotation type
58-
* is not marked as inherited itself)
55+
* @param checkInherited whether to also check the superclasses and interfaces
56+
* as well as meta-annotations for the annotation type
57+
* @see AnnotationClassFilter#AnnotationClassFilter(Class, boolean)
5958
*/
6059
public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationType, boolean checkInherited) {
6160
this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited);
@@ -72,18 +71,36 @@ public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationTyp
7271
public AnnotationMatchingPointcut(
7372
Class<? extends Annotation> classAnnotationType, Class<? extends Annotation> methodAnnotationType) {
7473

74+
this(classAnnotationType, methodAnnotationType, false);
75+
}
76+
77+
/**
78+
* Create a new AnnotationMatchingPointcut for the given annotation type.
79+
* @param classAnnotationType the annotation type to look for at the class level
80+
* (can be {@code null})
81+
* @param methodAnnotationType the annotation type to look for at the method level
82+
* (can be {@code null})
83+
* @param checkInherited whether to also check the superclasses and interfaces
84+
* as well as meta-annotations for the annotation type
85+
* @since 5.0
86+
* @see AnnotationClassFilter#AnnotationClassFilter(Class, boolean)
87+
* @see AnnotationMethodMatcher#AnnotationMethodMatcher(Class, boolean)
88+
*/
89+
public AnnotationMatchingPointcut(Class<? extends Annotation> classAnnotationType,
90+
Class<? extends Annotation> methodAnnotationType, boolean checkInherited) {
91+
7592
Assert.isTrue((classAnnotationType != null || methodAnnotationType != null),
7693
"Either Class annotation type or Method annotation type needs to be specified (or both)");
7794

7895
if (classAnnotationType != null) {
79-
this.classFilter = new AnnotationClassFilter(classAnnotationType);
96+
this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited);
8097
}
8198
else {
8299
this.classFilter = ClassFilter.TRUE;
83100
}
84101

85102
if (methodAnnotationType != null) {
86-
this.methodMatcher = new AnnotationMethodMatcher(methodAnnotationType);
103+
this.methodMatcher = new AnnotationMethodMatcher(methodAnnotationType, checkInherited);
87104
}
88105
else {
89106
this.methodMatcher = MethodMatcher.TRUE;

spring-aop/src/main/java/org/springframework/aop/support/annotation/AnnotationMethodMatcher.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.aop.support.AopUtils;
2323
import org.springframework.aop.support.StaticMethodMatcher;
24+
import org.springframework.core.annotation.AnnotationUtils;
2425
import org.springframework.util.Assert;
2526

2627
/**
@@ -36,25 +37,48 @@ public class AnnotationMethodMatcher extends StaticMethodMatcher {
3637

3738
private final Class<? extends Annotation> annotationType;
3839

40+
private final boolean checkInherited;
41+
3942

4043
/**
4144
* Create a new AnnotationClassFilter for the given annotation type.
4245
* @param annotationType the annotation type to look for
4346
*/
4447
public AnnotationMethodMatcher(Class<? extends Annotation> annotationType) {
48+
this(annotationType, false);
49+
}
50+
51+
/**
52+
* Create a new AnnotationClassFilter for the given annotation type.
53+
* @param annotationType the annotation type to look for
54+
* @param checkInherited whether to also check the superclasses and
55+
* interfaces as well as meta-annotations for the annotation type
56+
* (i.e. whether to use {@link AnnotationUtils#findAnnotation(Method, Class)}
57+
* semantics instead of standard Java {@link Method#isAnnotationPresent})
58+
* @since 5.0
59+
*/
60+
public AnnotationMethodMatcher(Class<? extends Annotation> annotationType, boolean checkInherited) {
4561
Assert.notNull(annotationType, "Annotation type must not be null");
4662
this.annotationType = annotationType;
63+
this.checkInherited = checkInherited;
4764
}
4865

4966

67+
5068
@Override
5169
public boolean matches(Method method, Class<?> targetClass) {
52-
if (method.isAnnotationPresent(this.annotationType)) {
70+
if (matchesMethod(method)) {
5371
return true;
5472
}
5573
// The method may be on an interface, so let's check on the target class as well.
5674
Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
57-
return (specificMethod != method && specificMethod.isAnnotationPresent(this.annotationType));
75+
return (specificMethod != method && matchesMethod(specificMethod));
76+
}
77+
78+
private boolean matchesMethod(Method method) {
79+
return (this.checkInherited ?
80+
(AnnotationUtils.findAnnotation(method, this.annotationType) != null) :
81+
method.isAnnotationPresent(this.annotationType));
5882
}
5983

6084
@Override

spring-context/src/main/java/org/springframework/scheduling/annotation/AsyncAnnotationAdvisor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@
4747
*
4848
* @author Juergen Hoeller
4949
* @since 3.0
50-
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationAdvisor
51-
* @see org.springframework.stereotype.Repository
52-
* @see org.springframework.dao.DataAccessException
53-
* @see org.springframework.dao.support.PersistenceExceptionTranslator
50+
* @see Async
51+
* @see AnnotationAsyncExecutionInterceptor
5452
*/
5553
@SuppressWarnings("serial")
5654
public class AsyncAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware {
@@ -157,7 +155,7 @@ protected Pointcut buildPointcut(Set<Class<? extends Annotation>> asyncAnnotatio
157155
ComposablePointcut result = null;
158156
for (Class<? extends Annotation> asyncAnnotationType : asyncAnnotationTypes) {
159157
Pointcut cpc = new AnnotationMatchingPointcut(asyncAnnotationType, true);
160-
Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(asyncAnnotationType);
158+
Pointcut mpc = new AnnotationMatchingPointcut(null, asyncAnnotationType, true);
161159
if (result == null) {
162160
result = new ComposablePointcut(cpc);
163161
}

spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.concurrent.Executor;
2626
import java.util.concurrent.Future;
2727

28-
import org.junit.Ignore;
2928
import org.junit.Test;
3029
import org.mockito.Mockito;
3130

@@ -215,7 +214,7 @@ public void spr14949FindsOnInterfaceWithInterfaceProxy() throws InterruptedExcep
215214
ctx.close();
216215
}
217216

218-
@Test @Ignore // TODO
217+
@Test
219218
public void spr14949FindsOnInterfaceWithCglibProxy() throws InterruptedException {
220219
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Spr14949ConfigB.class);
221220

0 commit comments

Comments
 (0)