@@ -81,6 +81,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
81
81
82
82
private EventListener eventListener ;
83
83
84
+
84
85
public ApplicationListenerMethodAdapter (String beanName , Class <?> targetClass , Method method ) {
85
86
this .beanName = beanName ;
86
87
this .method = method ;
@@ -90,6 +91,7 @@ public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, M
90
91
this .methodKey = new AnnotatedElementKey (this .method , this .targetClass );
91
92
}
92
93
94
+
93
95
/**
94
96
* Initialize this instance.
95
97
*/
@@ -98,11 +100,40 @@ void init(ApplicationContext applicationContext, EventExpressionEvaluator evalua
98
100
this .evaluator = evaluator ;
99
101
}
100
102
103
+
101
104
@ Override
102
105
public void onApplicationEvent (ApplicationEvent event ) {
103
106
processEvent (event );
104
107
}
105
108
109
+ @ Override
110
+ public boolean supportsEventType (ResolvableType eventType ) {
111
+ for (ResolvableType declaredEventType : this .declaredEventTypes ) {
112
+ if (declaredEventType .isAssignableFrom (eventType )) {
113
+ return true ;
114
+ }
115
+ else if (PayloadApplicationEvent .class .isAssignableFrom (eventType .getRawClass ())) {
116
+ ResolvableType payloadType = eventType .as (PayloadApplicationEvent .class ).getGeneric ();
117
+ if (declaredEventType .isAssignableFrom (payloadType )) {
118
+ return true ;
119
+ }
120
+ }
121
+ }
122
+ return eventType .hasUnresolvableGenerics ();
123
+ }
124
+
125
+ @ Override
126
+ public boolean supportsSourceType (Class <?> sourceType ) {
127
+ return true ;
128
+ }
129
+
130
+ @ Override
131
+ public int getOrder () {
132
+ Order order = getMethodAnnotation (Order .class );
133
+ return (order != null ? order .value () : 0 );
134
+ }
135
+
136
+
106
137
/**
107
138
* Process the specified {@link ApplicationEvent}, checking if the condition
108
139
* match and handling non-null result, if any.
@@ -144,7 +175,6 @@ protected Object[] resolveArguments(ApplicationEvent event) {
144
175
}
145
176
146
177
protected void handleResult (Object result ) {
147
- Assert .notNull (this .applicationContext , "ApplicationContext must no be null." );
148
178
if (result .getClass ().isArray ()) {
149
179
Object [] events = ObjectUtils .toObjectArray (result );
150
180
for (Object event : events ) {
@@ -164,6 +194,7 @@ else if (result instanceof Collection<?>) {
164
194
165
195
private void publishEvent (Object event ) {
166
196
if (event != null ) {
197
+ Assert .notNull (this .applicationContext , "ApplicationContext must no be null" );
167
198
this .applicationContext .publishEvent (event );
168
199
}
169
200
}
@@ -174,41 +205,14 @@ private boolean shouldHandle(ApplicationEvent event, Object[] args) {
174
205
}
175
206
String condition = getCondition ();
176
207
if (StringUtils .hasText (condition )) {
177
- Assert .notNull (this .evaluator , "Evaluator must no be null. " );
178
- EvaluationContext evaluationContext = this .evaluator .createEvaluationContext (event ,
179
- this .targetClass , this .method , args );
208
+ Assert .notNull (this .evaluator , "EventExpressionEvaluator must no be null" );
209
+ EvaluationContext evaluationContext = this .evaluator .createEvaluationContext (
210
+ event , this .targetClass , this .method , args );
180
211
return this .evaluator .condition (condition , this .methodKey , evaluationContext );
181
212
}
182
213
return true ;
183
214
}
184
215
185
- @ Override
186
- public boolean supportsEventType (ResolvableType eventType ) {
187
- for (ResolvableType declaredEventType : this .declaredEventTypes ) {
188
- if (declaredEventType .isAssignableFrom (eventType )) {
189
- return true ;
190
- }
191
- else if (PayloadApplicationEvent .class .isAssignableFrom (eventType .getRawClass ())) {
192
- ResolvableType payloadType = eventType .as (PayloadApplicationEvent .class ).getGeneric ();
193
- if (declaredEventType .isAssignableFrom (payloadType )) {
194
- return true ;
195
- }
196
- }
197
- }
198
- return eventType .hasUnresolvableGenerics ();
199
- }
200
-
201
- @ Override
202
- public boolean supportsSourceType (Class <?> sourceType ) {
203
- return true ;
204
- }
205
-
206
- @ Override
207
- public int getOrder () {
208
- Order order = getMethodAnnotation (Order .class );
209
- return (order != null ? order .value () : 0 );
210
- }
211
-
212
216
protected <A extends Annotation > A getMethodAnnotation (Class <A > annotationType ) {
213
217
return AnnotationUtils .findAnnotation (this .method , annotationType );
214
218
}
@@ -246,7 +250,7 @@ protected Object doInvoke(Object... args) {
246
250
* Return the target bean instance to use.
247
251
*/
248
252
protected Object getTargetBean () {
249
- Assert .notNull (this .applicationContext , "ApplicationContext must no be null. " );
253
+ Assert .notNull (this .applicationContext , "ApplicationContext must no be null" );
250
254
return this .applicationContext .getBean (this .beanName );
251
255
}
252
256
@@ -346,8 +350,8 @@ private ResolvableType getResolvableType(ApplicationEvent event) {
346
350
private List <ResolvableType > resolveDeclaredEventTypes () {
347
351
int count = this .method .getParameterTypes ().length ;
348
352
if (count > 1 ) {
349
- throw new IllegalStateException ("Maximum one parameter is allowed " +
350
- "for event listener method: " + method );
353
+ throw new IllegalStateException (
354
+ "Maximum one parameter is allowed for event listener method: " + this . method );
351
355
}
352
356
EventListener ann = getEventListener ();
353
357
if (ann != null && ann .classes ().length > 0 ) {
@@ -359,13 +363,14 @@ private List<ResolvableType> resolveDeclaredEventTypes() {
359
363
}
360
364
else {
361
365
if (count == 0 ) {
362
- throw new IllegalStateException ("Event parameter is mandatory " +
363
- "for event listener method: " + method );
366
+ throw new IllegalStateException (
367
+ "Event parameter is mandatory for event listener method: " + this . method );
364
368
}
365
369
return Collections .singletonList (ResolvableType .forMethodParameter (this .method , 0 ));
366
370
}
367
371
}
368
372
373
+
369
374
@ Override
370
375
public String toString () {
371
376
return this .method .toGenericString ();
0 commit comments