Skip to content

Commit 5af1a69

Browse files
committed
DefaultAdvisorChainFactory never passes null into ClassFilter, enabling async advisor to work without target class as well
Issue: SPR-11910 (cherry picked from commit a9b650f)
1 parent 9f967b1 commit 5af1a69

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/AdvisorChainFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -32,10 +32,10 @@ public interface AdvisorChainFactory {
3232
* for the given advisor chain configuration.
3333
* @param config the AOP configuration in the form of an Advised object
3434
* @param method the proxied method
35-
* @param targetClass the target class
35+
* @param targetClass the target class (may be {@code null} to indicate a proxy without
36+
* target object, in which case the method's declaring class is the next best option)
3637
* @return List of MethodInterceptors (may also include InterceptorAndDynamicMethodMatchers)
3738
*/
38-
List<Object> getInterceptorsAndDynamicInterceptionAdvice(
39-
Advised config, Method method, Class<?> targetClass);
39+
List<Object> getInterceptorsAndDynamicInterceptionAdvice(Advised config, Method method, Class<?> targetClass);
4040

4141
}

spring-aop/src/main/java/org/springframework/aop/framework/DefaultAdvisorChainFactory.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -50,19 +50,21 @@ public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializ
5050
public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
5151
Advised config, Method method, Class<?> targetClass) {
5252

53-
// This is somewhat tricky... we have to process introductions first,
53+
// This is somewhat tricky... We have to process introductions first,
5454
// but we need to preserve order in the ultimate list.
5555
List<Object> interceptorList = new ArrayList<Object>(config.getAdvisors().length);
56-
boolean hasIntroductions = hasMatchingIntroductions(config, targetClass);
56+
Class<?> actualClass = (targetClass != null ? targetClass : method.getDeclaringClass());
57+
boolean hasIntroductions = hasMatchingIntroductions(config, actualClass);
5758
AdvisorAdapterRegistry registry = GlobalAdvisorAdapterRegistry.getInstance();
59+
5860
for (Advisor advisor : config.getAdvisors()) {
5961
if (advisor instanceof PointcutAdvisor) {
6062
// Add it conditionally.
6163
PointcutAdvisor pointcutAdvisor = (PointcutAdvisor) advisor;
62-
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(targetClass)) {
64+
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
6365
MethodInterceptor[] interceptors = registry.getInterceptors(advisor);
6466
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
65-
if (MethodMatchers.matches(mm, method, targetClass, hasIntroductions)) {
67+
if (MethodMatchers.matches(mm, method, actualClass, hasIntroductions)) {
6668
if (mm.isRuntime()) {
6769
// Creating a new object instance in the getInterceptors() method
6870
// isn't a problem as we normally cache created chains.
@@ -78,7 +80,7 @@ public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
7880
}
7981
else if (advisor instanceof IntroductionAdvisor) {
8082
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
81-
if (config.isPreFiltered() || ia.getClassFilter().matches(targetClass)) {
83+
if (config.isPreFiltered() || ia.getClassFilter().matches(actualClass)) {
8284
Interceptor[] interceptors = registry.getInterceptors(advisor);
8385
interceptorList.addAll(Arrays.asList(interceptors));
8486
}
@@ -88,18 +90,19 @@ else if (advisor instanceof IntroductionAdvisor) {
8890
interceptorList.addAll(Arrays.asList(interceptors));
8991
}
9092
}
93+
9194
return interceptorList;
9295
}
9396

9497
/**
9598
* Determine whether the Advisors contain matching introductions.
9699
*/
97-
private static boolean hasMatchingIntroductions(Advised config, Class<?> targetClass) {
100+
private static boolean hasMatchingIntroductions(Advised config, Class<?> actualClass) {
98101
for (int i = 0; i < config.getAdvisors().length; i++) {
99102
Advisor advisor = config.getAdvisors()[i];
100103
if (advisor instanceof IntroductionAdvisor) {
101104
IntroductionAdvisor ia = (IntroductionAdvisor) advisor;
102-
if (ia.getClassFilter().matches(targetClass)) {
105+
if (ia.getClassFilter().matches(actualClass)) {
103106
return true;
104107
}
105108
}

0 commit comments

Comments
 (0)