Skip to content

Commit af213a0

Browse files
committed
Polishing
1 parent dbec212 commit af213a0

File tree

4 files changed

+82
-80
lines changed

4 files changed

+82
-80
lines changed

Diff for: spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java

+41-36
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
8181

8282
private EventListener eventListener;
8383

84+
8485
public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method) {
8586
this.beanName = beanName;
8687
this.method = method;
@@ -90,6 +91,7 @@ public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, M
9091
this.methodKey = new AnnotatedElementKey(this.method, this.targetClass);
9192
}
9293

94+
9395
/**
9496
* Initialize this instance.
9597
*/
@@ -98,11 +100,40 @@ void init(ApplicationContext applicationContext, EventExpressionEvaluator evalua
98100
this.evaluator = evaluator;
99101
}
100102

103+
101104
@Override
102105
public void onApplicationEvent(ApplicationEvent event) {
103106
processEvent(event);
104107
}
105108

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+
106137
/**
107138
* Process the specified {@link ApplicationEvent}, checking if the condition
108139
* match and handling non-null result, if any.
@@ -144,7 +175,6 @@ protected Object[] resolveArguments(ApplicationEvent event) {
144175
}
145176

146177
protected void handleResult(Object result) {
147-
Assert.notNull(this.applicationContext, "ApplicationContext must no be null.");
148178
if (result.getClass().isArray()) {
149179
Object[] events = ObjectUtils.toObjectArray(result);
150180
for (Object event : events) {
@@ -164,6 +194,7 @@ else if (result instanceof Collection<?>) {
164194

165195
private void publishEvent(Object event) {
166196
if (event != null) {
197+
Assert.notNull(this.applicationContext, "ApplicationContext must no be null");
167198
this.applicationContext.publishEvent(event);
168199
}
169200
}
@@ -174,41 +205,14 @@ private boolean shouldHandle(ApplicationEvent event, Object[] args) {
174205
}
175206
String condition = getCondition();
176207
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);
180211
return this.evaluator.condition(condition, this.methodKey, evaluationContext);
181212
}
182213
return true;
183214
}
184215

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-
212216
protected <A extends Annotation> A getMethodAnnotation(Class<A> annotationType) {
213217
return AnnotationUtils.findAnnotation(this.method, annotationType);
214218
}
@@ -246,7 +250,7 @@ protected Object doInvoke(Object... args) {
246250
* Return the target bean instance to use.
247251
*/
248252
protected Object getTargetBean() {
249-
Assert.notNull(this.applicationContext, "ApplicationContext must no be null.");
253+
Assert.notNull(this.applicationContext, "ApplicationContext must no be null");
250254
return this.applicationContext.getBean(this.beanName);
251255
}
252256

@@ -346,8 +350,8 @@ private ResolvableType getResolvableType(ApplicationEvent event) {
346350
private List<ResolvableType> resolveDeclaredEventTypes() {
347351
int count = this.method.getParameterTypes().length;
348352
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);
351355
}
352356
EventListener ann = getEventListener();
353357
if (ann != null && ann.classes().length > 0) {
@@ -359,13 +363,14 @@ private List<ResolvableType> resolveDeclaredEventTypes() {
359363
}
360364
else {
361365
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);
364368
}
365369
return Collections.singletonList(ResolvableType.forMethodParameter(this.method, 0));
366370
}
367371
}
368372

373+
369374
@Override
370375
public String toString() {
371376
return this.method.toGenericString();

Diff for: spring-context/src/main/java/org/springframework/context/event/EventListener.java

+12-13
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
* Annotation that marks a method as a listener for application events.
3030
*
3131
* <p>If an annotated method supports a single event type, the method may
32-
* declare a single parameter that reflects the event type to listen to. If
33-
* an annotated method supports multiple event types, this annotation may
32+
* declare a single parameter that reflects the event type to listen to.
33+
* If an annotated method supports multiple event types, this annotation may
3434
* refer to one or more supported event types using the {@code classes}
3535
* attribute. See {@link #classes} for further details.
3636
*
@@ -42,20 +42,19 @@
4242
* when using Java config or manually via the {@code <context:annotation-driven/>}
4343
* element when using XML config.
4444
*
45-
* <p>Annotated methods may have a non-{@code void} return type. When they
46-
* do, the result of the method invocation is sent as a new event. If the
47-
* return type is either an array or a collection, each element is sent as
48-
* a new event.
45+
* <p>Annotated methods may have a non-{@code void} return type. When they do,
46+
* the result of the method invocation is sent as a new event. If the return type
47+
* is either an array or a collection, each element is sent as a new event.
4948
*
50-
* <p>It is also possible to define the order in which listeners for a
51-
* certain event are invoked. To do so, add a regular
49+
* <p>It is also possible to define the order in which listeners for a certain
50+
* event are invoked. To do so, add Spring's common
5251
* {@link org.springframework.core.annotation.Order @Order} annotation
5352
* alongside this annotation.
5453
*
55-
* <p>While it is possible for an event listener to declare that it throws
56-
* arbitrary exception types, any checked exceptions thrown from an event
57-
* listener will be wrapped in a {@link java.lang.reflect.UndeclaredThrowableException}
58-
* since the caller can only handle runtime exceptions.
54+
* <p>While it is possible for an event listener to declare that it throws arbitrary
55+
* exception types, any checked exceptions thrown from an event listener will be
56+
* wrapped in an {@link java.lang.reflect.UndeclaredThrowableException} since
57+
* the caller can only handle runtime exceptions.
5958
*
6059
* @author Stephane Nicoll
6160
* @since 4.2
@@ -89,4 +88,4 @@
8988
*/
9089
String condition() default "";
9190

92-
}
91+
}

Diff for: spring-jms/src/main/java/org/springframework/jms/annotation/JmsListener.java

+23-25
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,20 @@
2626
import org.springframework.messaging.handler.annotation.MessageMapping;
2727

2828
/**
29-
* Annotation that marks a method to be the target of a JMS message
30-
* listener on the specified {@link #destination}. The {@link #containerFactory}
31-
* identifies the {@link org.springframework.jms.config.JmsListenerContainerFactory
32-
* JmsListenerContainerFactory} to use to build the JMS listener container. If not
33-
* set, a <em>default</em> container factory is assumed to be available with a bean
34-
* name of {@code jmsListenerContainerFactory} unless an explicit default has been
35-
* provided through configuration.
29+
* Annotation that marks a method to be the target of a JMS message listener on the
30+
* specified {@link #destination}. The {@link #containerFactory} identifies the
31+
* {@link org.springframework.jms.config.JmsListenerContainerFactory} to use to build
32+
* the JMS listener container. If not set, a <em>default</em> container factory is
33+
* assumed to be available with a bean name of {@code jmsListenerContainerFactory}
34+
* unless an explicit default has been provided through configuration.
3635
*
37-
* <p>Processing of {@code @JmsListener} annotations is performed by
38-
* registering a {@link JmsListenerAnnotationBeanPostProcessor}. This can be
39-
* done manually or, more conveniently, through the {@code <jms:annotation-driven/>}
40-
* element or {@link EnableJms @EnableJms} annotation.
36+
* <p>Processing of {@code @JmsListener} annotations is performed by registering a
37+
* {@link JmsListenerAnnotationBeanPostProcessor}. This can be done manually or,
38+
* more conveniently, through the {@code <jms:annotation-driven/>} element or
39+
* {@link EnableJms @EnableJms} annotation.
4140
*
42-
* <p>Annotated methods are allowed to have flexible signatures similar to what
43-
* {@link MessageMapping} provides:
41+
* <p>Annotated JMS listener methods are allowed to have flexible signatures similar
42+
* to what {@link MessageMapping} provides:
4443
* <ul>
4544
* <li>{@link javax.jms.Session} to get access to the JMS session</li>
4645
* <li>{@link javax.jms.Message} or one of its subclasses to get access to the raw JMS message</li>
@@ -49,15 +48,15 @@
4948
* arguments, including support for validation</li>
5049
* <li>{@link org.springframework.messaging.handler.annotation.Header @Header}-annotated method
5150
* arguments to extract specific header values, including standard JMS headers defined by
52-
* {@link org.springframework.jms.support.JmsHeaders JmsHeaders}</li>
51+
* {@link org.springframework.jms.support.JmsHeaders}</li>
5352
* <li>{@link org.springframework.messaging.handler.annotation.Headers @Headers}-annotated
54-
* method argument that must also be assignable to {@link java.util.Map} for obtaining access to all
55-
* headers</li>
56-
* <li>{@link org.springframework.messaging.MessageHeaders MessageHeaders} arguments for
57-
* obtaining access to all headers</li>
58-
* <li>{@link org.springframework.messaging.support.MessageHeaderAccessor MessageHeaderAccessor}
59-
* or {@link org.springframework.jms.support.JmsMessageHeaderAccessor JmsMessageHeaderAccessor}
60-
* for convenient access to all method arguments</li>
53+
* method argument that must also be assignable to {@link java.util.Map} for obtaining
54+
* access to all headers</li>
55+
* <li>{@link org.springframework.messaging.MessageHeaders} arguments for obtaining
56+
* access to all headers</li>
57+
* <li>{@link org.springframework.messaging.support.MessageHeaderAccessor} or
58+
* {@link org.springframework.jms.support.JmsMessageHeaderAccessor} for convenient
59+
* access to all method arguments</li>
6160
* </ul>
6261
*
6362
* <p>Annotated methods may have a non-{@code void} return type. When they do,
@@ -75,9 +74,9 @@
7574
*/
7675
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
7776
@Retention(RetentionPolicy.RUNTIME)
78-
@MessageMapping
7977
@Documented
8078
@Repeatable(JmsListeners.class)
79+
@MessageMapping
8180
public @interface JmsListener {
8281

8382
/**
@@ -88,16 +87,15 @@
8887
String id() default "";
8988

9089
/**
91-
* The bean name of the {@link org.springframework.jms.config.JmsListenerContainerFactory JmsListenerContainerFactory}
90+
* The bean name of the {@link org.springframework.jms.config.JmsListenerContainerFactory}
9291
* to use to create the message listener container responsible for serving this endpoint.
9392
* <p>If not specified, the default container factory is used, if any.
9493
*/
9594
String containerFactory() default "";
9695

9796
/**
9897
* The destination name for this listener, resolved through the container-wide
99-
* {@link org.springframework.jms.support.destination.DestinationResolver DestinationResolver}
100-
* strategy.
98+
* {@link org.springframework.jms.support.destination.DestinationResolver} strategy.
10199
*/
102100
String destination();
103101

Diff for: spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@
5454
/**
5555
* Bean post-processor that registers methods annotated with {@link JmsListener}
5656
* to be invoked by a JMS message listener container created under the cover
57-
* by a {@link org.springframework.jms.config.JmsListenerContainerFactory} according
58-
* to the parameters of the annotation.
57+
* by a {@link org.springframework.jms.config.JmsListenerContainerFactory}
58+
* according to the attributes of the annotation.
5959
*
6060
* <p>Annotated methods can use flexible arguments as defined by {@link JmsListener}.
6161
*
6262
* <p>This post-processor is automatically registered by Spring's
6363
* {@code <jms:annotation-driven>} XML element, and also by the {@link EnableJms}
6464
* annotation.
6565
*
66-
* <p>Auto-detect any {@link JmsListenerConfigurer} instances in the container,
66+
* <p>Autodetects any {@link JmsListenerConfigurer} instances in the container,
6767
* allowing for customization of the registry to be used, the default container
68-
* factory or for fine-grained control over endpoints registration. See
69-
* {@link EnableJms} Javadoc for complete usage details.
68+
* factory or for fine-grained control over endpoints registration. See the
69+
* {@link EnableJms} javadocs for complete usage details.
7070
*
7171
* @author Stephane Nicoll
7272
* @author Juergen Hoeller
@@ -282,7 +282,7 @@ private String getEndpointId(JmsListener jmsListener) {
282282
return resolve(jmsListener.id());
283283
}
284284
else {
285-
return "org.springframework.jms.JmsListenerEndpointContainer#" + counter.getAndIncrement();
285+
return "org.springframework.jms.JmsListenerEndpointContainer#" + this.counter.getAndIncrement();
286286
}
287287
}
288288

0 commit comments

Comments
 (0)