18
18
19
19
import java .lang .reflect .InvocationTargetException ;
20
20
import java .lang .reflect .Method ;
21
+ import java .lang .reflect .Proxy ;
21
22
import java .lang .reflect .UndeclaredThrowableException ;
22
23
import java .util .ArrayList ;
23
24
import java .util .Collection ;
27
28
import org .apache .commons .logging .Log ;
28
29
import org .apache .commons .logging .LogFactory ;
29
30
31
+ import org .springframework .aop .support .AopUtils ;
30
32
import org .springframework .context .ApplicationContext ;
31
33
import org .springframework .context .ApplicationEvent ;
32
34
import org .springframework .context .PayloadApplicationEvent ;
35
37
import org .springframework .core .ResolvableType ;
36
38
import org .springframework .core .annotation .AnnotatedElementUtils ;
37
39
import org .springframework .core .annotation .Order ;
38
- import org .springframework .expression .EvaluationContext ;
39
40
import org .springframework .lang .Nullable ;
40
41
import org .springframework .util .Assert ;
41
- import org .springframework .util .ClassUtils ;
42
42
import org .springframework .util .ObjectUtils ;
43
43
import org .springframework .util .ReflectionUtils ;
44
44
import org .springframework .util .StringUtils ;
@@ -66,9 +66,9 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
66
66
67
67
private final Method method ;
68
68
69
- private final Class <?> targetClass ;
69
+ private final Method targetMethod ;
70
70
71
- private final Method bridgedMethod ;
71
+ private final AnnotatedElementKey methodKey ;
72
72
73
73
private final List <ResolvableType > declaredEventTypes ;
74
74
@@ -77,8 +77,6 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
77
77
78
78
private final int order ;
79
79
80
- private final AnnotatedElementKey methodKey ;
81
-
82
80
@ Nullable
83
81
private ApplicationContext applicationContext ;
84
82
@@ -88,18 +86,15 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
88
86
89
87
public ApplicationListenerMethodAdapter (String beanName , Class <?> targetClass , Method method ) {
90
88
this .beanName = beanName ;
91
- this .method = method ;
92
- this .targetClass = targetClass ;
93
- this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
94
-
95
- Method targetMethod = ClassUtils .getMostSpecificMethod (method , targetClass );
96
- EventListener ann = AnnotatedElementUtils .findMergedAnnotation (targetMethod , EventListener .class );
89
+ this .method = BridgeMethodResolver .findBridgedMethod (method );
90
+ this .targetMethod = (!Proxy .isProxyClass (targetClass ) ?
91
+ AopUtils .getMostSpecificMethod (method , targetClass ) : this .method );
92
+ this .methodKey = new AnnotatedElementKey (this .targetMethod , targetClass );
97
93
94
+ EventListener ann = AnnotatedElementUtils .findMergedAnnotation (this .targetMethod , EventListener .class );
98
95
this .declaredEventTypes = resolveDeclaredEventTypes (method , ann );
99
96
this .condition = (ann != null ? ann .condition () : null );
100
97
this .order = resolveOrder (method );
101
-
102
- this .methodKey = new AnnotatedElementKey (method , targetClass );
103
98
}
104
99
105
100
@@ -109,20 +104,23 @@ private List<ResolvableType> resolveDeclaredEventTypes(Method method, @Nullable
109
104
throw new IllegalStateException (
110
105
"Maximum one parameter is allowed for event listener method: " + method );
111
106
}
112
- if (ann != null && ann .classes ().length > 0 ) {
113
- List <ResolvableType > types = new ArrayList <>(ann .classes ().length );
114
- for (Class <?> eventType : ann .classes ()) {
115
- types .add (ResolvableType .forClass (eventType ));
107
+
108
+ if (ann != null ) {
109
+ Class <?>[] classes = ann .classes ();
110
+ if (classes .length > 0 ) {
111
+ List <ResolvableType > types = new ArrayList <>(classes .length );
112
+ for (Class <?> eventType : classes ) {
113
+ types .add (ResolvableType .forClass (eventType ));
114
+ }
115
+ return types ;
116
116
}
117
- return types ;
118
117
}
119
- else {
120
- if (count == 0 ) {
121
- throw new IllegalStateException (
122
- "Event parameter is mandatory for event listener method: " + method );
123
- }
124
- return Collections .singletonList (ResolvableType .forMethodParameter (method , 0 ));
118
+
119
+ if (count == 0 ) {
120
+ throw new IllegalStateException (
121
+ "Event parameter is mandatory for event listener method: " + method );
125
122
}
123
+ return Collections .singletonList (ResolvableType .forMethodParameter (method , 0 ));
126
124
}
127
125
128
126
private int resolveOrder (Method method ) {
@@ -245,10 +243,9 @@ private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) {
245
243
}
246
244
String condition = getCondition ();
247
245
if (StringUtils .hasText (condition )) {
248
- Assert .notNull (this .evaluator , "EventExpressionEvaluator must no be null" );
249
- EvaluationContext evaluationContext = this .evaluator .createEvaluationContext (
250
- event , this .targetClass , this .method , args , this .applicationContext );
251
- return this .evaluator .condition (condition , this .methodKey , evaluationContext );
246
+ Assert .notNull (this .evaluator , "EventExpressionEvaluator must not be null" );
247
+ return this .evaluator .condition (
248
+ condition , event , this .targetMethod , this .methodKey , args , this .applicationContext );
252
249
}
253
250
return true ;
254
251
}
@@ -259,12 +256,12 @@ private boolean shouldHandle(ApplicationEvent event, @Nullable Object[] args) {
259
256
@ Nullable
260
257
protected Object doInvoke (Object ... args ) {
261
258
Object bean = getTargetBean ();
262
- ReflectionUtils .makeAccessible (this .bridgedMethod );
259
+ ReflectionUtils .makeAccessible (this .method );
263
260
try {
264
- return this .bridgedMethod .invoke (bean , args );
261
+ return this .method .invoke (bean , args );
265
262
}
266
263
catch (IllegalArgumentException ex ) {
267
- assertTargetBean (this .bridgedMethod , bean , args );
264
+ assertTargetBean (this .method , bean , args );
268
265
throw new IllegalStateException (getInvocationErrorMessage (bean , ex .getMessage (), args ), ex );
269
266
}
270
267
catch (IllegalAccessException ex ) {
@@ -311,7 +308,7 @@ protected String getDetailedErrorMessage(Object bean, String message) {
311
308
StringBuilder sb = new StringBuilder (message ).append ("\n " );
312
309
sb .append ("HandlerMethod details: \n " );
313
310
sb .append ("Bean [" ).append (bean .getClass ().getName ()).append ("]\n " );
314
- sb .append ("Method [" ).append (this .bridgedMethod .toGenericString ()).append ("]\n " );
311
+ sb .append ("Method [" ).append (this .method .toGenericString ()).append ("]\n " );
315
312
return sb .toString ();
316
313
}
317
314
0 commit comments