16
16
17
17
package org .springframework .context .event ;
18
18
19
- import java .lang .annotation .Annotation ;
20
19
import java .lang .reflect .InvocationTargetException ;
21
20
import java .lang .reflect .Method ;
22
21
import java .lang .reflect .UndeclaredThrowableException ;
53
52
* evaluated prior to invoking the underlying method.
54
53
*
55
54
* @author Stephane Nicoll
55
+ * @author Juergen Hoeller
56
56
* @author Sam Brannen
57
57
* @since 4.2
58
58
*/
@@ -70,26 +70,58 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
70
70
71
71
private final List <ResolvableType > declaredEventTypes ;
72
72
73
+ private final String condition ;
74
+
75
+ private final int order ;
76
+
73
77
private final AnnotatedElementKey methodKey ;
74
78
75
79
private ApplicationContext applicationContext ;
76
80
77
81
private EventExpressionEvaluator evaluator ;
78
82
79
- private String condition ;
80
-
81
- private EventListener eventListener ;
82
-
83
83
84
84
public ApplicationListenerMethodAdapter (String beanName , Class <?> targetClass , Method method ) {
85
85
this .beanName = beanName ;
86
86
this .method = method ;
87
87
this .targetClass = targetClass ;
88
88
this .bridgedMethod = BridgeMethodResolver .findBridgedMethod (method );
89
- this .declaredEventTypes = resolveDeclaredEventTypes ();
90
- this .methodKey = new AnnotatedElementKey (this .method , this .targetClass );
89
+
90
+ EventListener ann = AnnotatedElementUtils .findMergedAnnotation (method , EventListener .class );
91
+ this .declaredEventTypes = resolveDeclaredEventTypes (method , ann );
92
+ this .condition = (ann != null ? ann .condition () : null );
93
+ this .order = resolveOrder (method );
94
+
95
+ this .methodKey = new AnnotatedElementKey (method , targetClass );
96
+ }
97
+
98
+
99
+ private List <ResolvableType > resolveDeclaredEventTypes (Method method , EventListener ann ) {
100
+ int count = method .getParameterTypes ().length ;
101
+ if (count > 1 ) {
102
+ throw new IllegalStateException (
103
+ "Maximum one parameter is allowed for event listener method: " + method );
104
+ }
105
+ if (ann != null && ann .classes ().length > 0 ) {
106
+ List <ResolvableType > types = new ArrayList <ResolvableType >(ann .classes ().length );
107
+ for (Class <?> eventType : ann .classes ()) {
108
+ types .add (ResolvableType .forClass (eventType ));
109
+ }
110
+ return types ;
111
+ }
112
+ else {
113
+ if (count == 0 ) {
114
+ throw new IllegalStateException (
115
+ "Event parameter is mandatory for event listener method: " + method );
116
+ }
117
+ return Collections .singletonList (ResolvableType .forMethodParameter (method , 0 ));
118
+ }
91
119
}
92
120
121
+ private int resolveOrder (Method method ) {
122
+ Order ann = AnnotatedElementUtils .findMergedAnnotation (method , Order .class );
123
+ return (ann != null ? ann .value () : 0 );
124
+ }
93
125
94
126
/**
95
127
* Initialize this instance.
@@ -128,8 +160,7 @@ public boolean supportsSourceType(Class<?> sourceType) {
128
160
129
161
@ Override
130
162
public int getOrder () {
131
- Order order = getMethodAnnotation (Order .class );
132
- return (order != null ? order .value () : 0 );
163
+ return this .order ;
133
164
}
134
165
135
166
@@ -164,8 +195,8 @@ protected Object[] resolveArguments(ApplicationEvent event) {
164
195
if (this .method .getParameterTypes ().length == 0 ) {
165
196
return new Object [0 ];
166
197
}
167
- if (!ApplicationEvent .class .isAssignableFrom (declaredEventType .getRawClass ())
168
- && event instanceof PayloadApplicationEvent ) {
198
+ if (!ApplicationEvent .class .isAssignableFrom (declaredEventType .getRawClass ()) &&
199
+ event instanceof PayloadApplicationEvent ) {
169
200
return new Object [] {((PayloadApplicationEvent ) event ).getPayload ()};
170
201
}
171
202
else {
@@ -212,10 +243,6 @@ private boolean shouldHandle(ApplicationEvent event, Object[] args) {
212
243
return true ;
213
244
}
214
245
215
- protected <A extends Annotation > A getMethodAnnotation (Class <A > annotationType ) {
216
- return AnnotatedElementUtils .findMergedAnnotation (this .method , annotationType );
217
- }
218
-
219
246
/**
220
247
* Invoke the event listener method with the given argument values.
221
248
*/
@@ -253,26 +280,13 @@ protected Object getTargetBean() {
253
280
return this .applicationContext .getBean (this .beanName );
254
281
}
255
282
256
- protected EventListener getEventListener () {
257
- if (this .eventListener == null ) {
258
- this .eventListener = AnnotatedElementUtils .findMergedAnnotation (this .method , EventListener .class );
259
- }
260
- return this .eventListener ;
261
- }
262
-
263
283
/**
264
284
* Return the condition to use.
265
285
* <p>Matches the {@code condition} attribute of the {@link EventListener}
266
286
* annotation or any matching attribute on a composed annotation that
267
287
* is meta-annotated with {@code @EventListener}.
268
288
*/
269
289
protected String getCondition () {
270
- if (this .condition == null ) {
271
- EventListener eventListener = AnnotatedElementUtils .findMergedAnnotation (this .method , EventListener .class );
272
- if (eventListener != null ) {
273
- this .condition = eventListener .condition ();
274
- }
275
- }
276
290
return this .condition ;
277
291
}
278
292
@@ -346,29 +360,6 @@ private ResolvableType getResolvableType(ApplicationEvent event) {
346
360
return null ;
347
361
}
348
362
349
- private List <ResolvableType > resolveDeclaredEventTypes () {
350
- int count = this .method .getParameterTypes ().length ;
351
- if (count > 1 ) {
352
- throw new IllegalStateException (
353
- "Maximum one parameter is allowed for event listener method: " + this .method );
354
- }
355
- EventListener ann = getEventListener ();
356
- if (ann != null && ann .classes ().length > 0 ) {
357
- List <ResolvableType > types = new ArrayList <ResolvableType >();
358
- for (Class <?> eventType : ann .classes ()) {
359
- types .add (ResolvableType .forClass (eventType ));
360
- }
361
- return types ;
362
- }
363
- else {
364
- if (count == 0 ) {
365
- throw new IllegalStateException (
366
- "Event parameter is mandatory for event listener method: " + this .method );
367
- }
368
- return Collections .singletonList (ResolvableType .forMethodParameter (this .method , 0 ));
369
- }
370
- }
371
-
372
363
373
364
@ Override
374
365
public String toString () {
0 commit comments