Skip to content

Commit 3854861

Browse files
committed
Polishing
1 parent 3dd0fbf commit 3854861

File tree

5 files changed

+43
-61
lines changed

5 files changed

+43
-61
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/AbstractAspectJAdvice.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,26 @@ public int getDeclarationOrder() {
248248
}
249249

250250
/**
251-
* Set by creator of this advice object if the argument names are known.
252-
* <p>This could be for example because they have been explicitly specified in XML,
251+
* Set by the creator of this advice object if the argument names are known.
252+
* <p>This could be for example because they have been explicitly specified in XML
253253
* or in an advice annotation.
254-
* @param argNames comma delimited list of arg names
254+
* @param argumentNames comma delimited list of argument names
255255
*/
256-
public void setArgumentNames(String argNames) {
257-
String[] tokens = StringUtils.commaDelimitedListToStringArray(argNames);
256+
public void setArgumentNames(String argumentNames) {
257+
String[] tokens = StringUtils.commaDelimitedListToStringArray(argumentNames);
258258
setArgumentNamesFromStringArray(tokens);
259259
}
260260

261-
public void setArgumentNamesFromStringArray(String... args) {
262-
this.argumentNames = new String[args.length];
263-
for (int i = 0; i < args.length; i++) {
264-
this.argumentNames[i] = args[i].strip();
261+
/**
262+
* Set by the creator of this advice object if the argument names are known.
263+
* <p>This could be for example because they have been explicitly specified in XML
264+
* or in an advice annotation.
265+
* @param argumentNames list of argument names
266+
*/
267+
public void setArgumentNamesFromStringArray(String... argumentNames) {
268+
this.argumentNames = new String[argumentNames.length];
269+
for (int i = 0; i < argumentNames.length; i++) {
270+
this.argumentNames[i] = argumentNames[i].strip();
265271
if (!isVariableName(this.argumentNames[i])) {
266272
throw new IllegalArgumentException(
267273
"'argumentNames' property of AbstractAspectJAdvice contains an argument name '" +

spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -52,8 +52,8 @@
5252
*
5353
* <p>Implementing ApplicationEventMulticaster's actual {@link #multicastEvent} method
5454
* is left to subclasses. {@link SimpleApplicationEventMulticaster} simply multicasts
55-
* all events to all registered listeners, invoking them in the calling thread.
56-
* Alternative implementations could be more sophisticated in those respects.
55+
* all events to all registered listeners, invoking them in the calling thread by
56+
* default. Alternative implementations could be more sophisticated in those respects.
5757
*
5858
* @author Juergen Hoeller
5959
* @author Stephane Nicoll
@@ -82,10 +82,10 @@ public void setBeanClassLoader(ClassLoader classLoader) {
8282

8383
@Override
8484
public void setBeanFactory(BeanFactory beanFactory) {
85-
if (!(beanFactory instanceof ConfigurableBeanFactory)) {
85+
if (!(beanFactory instanceof ConfigurableBeanFactory cbf)) {
8686
throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
8787
}
88-
this.beanFactory = (ConfigurableBeanFactory) beanFactory;
88+
this.beanFactory = cbf;
8989
if (this.beanClassLoader == null) {
9090
this.beanClassLoader = this.beanFactory.getBeanClassLoader();
9191
}
@@ -373,8 +373,8 @@ protected boolean supportsEvent(Class<?> listenerType, ResolvableType eventType)
373373
protected boolean supportsEvent(
374374
ApplicationListener<?> listener, ResolvableType eventType, @Nullable Class<?> sourceType) {
375375

376-
GenericApplicationListener smartListener = (listener instanceof GenericApplicationListener ?
377-
(GenericApplicationListener) listener : new GenericApplicationListenerAdapter(listener));
376+
GenericApplicationListener smartListener = (listener instanceof GenericApplicationListener gal ? gal :
377+
new GenericApplicationListenerAdapter(listener));
378378
return (smartListener.supportsEventType(eventType) && smartListener.supportsSourceType(sourceType));
379379
}
380380

spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -111,7 +111,7 @@ void simpleEventJavaConfig() {
111111
this.eventCollector.assertTotalEventsCount(1);
112112

113113
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
114-
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().contains("TestEvent"));
114+
l instanceof SmartApplicationListener sal && sal.getListenerId().contains("TestEvent"));
115115
this.eventCollector.clear();
116116
this.context.publishEvent(event);
117117
this.eventCollector.assertNoEventReceived(listener);
@@ -132,7 +132,7 @@ void simpleEventXmlConfig() {
132132
this.eventCollector.assertTotalEventsCount(1);
133133

134134
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
135-
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().contains("TestEvent"));
135+
l instanceof SmartApplicationListener sal && sal.getListenerId().contains("TestEvent"));
136136
this.eventCollector.clear();
137137
this.context.publishEvent(event);
138138
this.eventCollector.assertNoEventReceived(listener);
@@ -150,7 +150,7 @@ void metaAnnotationIsDiscovered() {
150150
this.eventCollector.assertTotalEventsCount(1);
151151

152152
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
153-
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().equals("foo"));
153+
l instanceof SmartApplicationListener sal && sal.getListenerId().equals("foo"));
154154
this.eventCollector.clear();
155155
this.context.publishEvent(event);
156156
this.eventCollector.assertNoEventReceived(bean);

spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.springframework.context.ApplicationEventPublisherAware;
4040
import org.springframework.context.ApplicationListener;
4141
import org.springframework.context.PayloadApplicationEvent;
42-
import org.springframework.context.support.AbstractApplicationContext;
4342
import org.springframework.context.support.GenericApplicationContext;
4443
import org.springframework.context.support.StaticApplicationContext;
4544
import org.springframework.context.support.StaticMessageSource;
@@ -59,6 +58,7 @@
5958
import static org.mockito.Mockito.mock;
6059
import static org.mockito.Mockito.times;
6160
import static org.mockito.Mockito.verify;
61+
import static org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME;
6262

6363
/**
6464
* Unit and integration tests for the ApplicationContext event support.
@@ -163,7 +163,7 @@ public void simpleApplicationEventMulticasterWithException() {
163163
willThrow(thrown).given(listener).onApplicationEvent(evt);
164164
assertThatRuntimeException()
165165
.isThrownBy(() -> smc.multicastEvent(evt))
166-
.satisfies(ex -> assertThat(ex).isSameAs(thrown));
166+
.isSameAs(thrown);
167167
}
168168

169169
@Test
@@ -282,20 +282,14 @@ public void listenersInApplicationContext() {
282282
context.publishEvent(event3);
283283
MyOtherEvent event4 = new MyOtherEvent(context);
284284
context.publishEvent(event4);
285-
assertThat(listener1.seenEvents.contains(event1)).isTrue();
286-
assertThat(listener1.seenEvents.contains(event2)).isTrue();
287-
assertThat(listener1.seenEvents.contains(event3)).isTrue();
288-
assertThat(listener1.seenEvents.contains(event4)).isTrue();
285+
assertThat(listener1.seenEvents).contains(event1, event2, event3, event4);
289286

290287
listener1.seenEvents.clear();
291288
context.publishEvent(event1);
292289
context.publishEvent(event2);
293290
context.publishEvent(event3);
294291
context.publishEvent(event4);
295-
assertThat(listener1.seenEvents.contains(event1)).isTrue();
296-
assertThat(listener1.seenEvents.contains(event2)).isTrue();
297-
assertThat(listener1.seenEvents.contains(event3)).isTrue();
298-
assertThat(listener1.seenEvents.contains(event4)).isTrue();
292+
assertThat(listener1.seenEvents).contains(event1, event2, event3, event4);
299293

300294
AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
301295
assertThat(multicaster.retrieverCache).hasSize(2);
@@ -314,10 +308,7 @@ public void listenersInApplicationContextWithPayloadEvents() {
314308
context.publishEvent("event2");
315309
context.publishEvent("event3");
316310
context.publishEvent("event4");
317-
assertThat(listener.seenPayloads.contains("event1")).isTrue();
318-
assertThat(listener.seenPayloads.contains("event2")).isTrue();
319-
assertThat(listener.seenPayloads.contains("event3")).isTrue();
320-
assertThat(listener.seenPayloads.contains("event4")).isTrue();
311+
assertThat(listener.seenPayloads).contains("event1", "event2", "event3", "event4");
321312

322313
AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
323314
assertThat(multicaster.retrieverCache).hasSize(2);
@@ -340,15 +331,13 @@ public void listenersInApplicationContextWithNestedChild() {
340331
MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
341332
MyEvent event1 = new MyEvent(context);
342333
context.publishEvent(event1);
343-
assertThat(listener1.seenEvents.contains(event1)).isTrue();
334+
assertThat(listener1.seenEvents).contains(event1);
344335

345-
SimpleApplicationEventMulticaster multicaster = context.getBean(
346-
AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
347-
SimpleApplicationEventMulticaster.class);
348-
assertThat(multicaster.getApplicationListeners().isEmpty()).isFalse();
336+
SimpleApplicationEventMulticaster multicaster = context.getBean(SimpleApplicationEventMulticaster.class);
337+
assertThat(multicaster.getApplicationListeners()).isNotEmpty();
349338

350339
context.close();
351-
assertThat(multicaster.getApplicationListeners().isEmpty()).isTrue();
340+
assertThat(multicaster.getApplicationListeners()).isEmpty();
352341
}
353342

354343
@Test
@@ -367,20 +356,14 @@ public void nonSingletonListenerInApplicationContext() {
367356
context.publishEvent(event3);
368357
MyOtherEvent event4 = new MyOtherEvent(context);
369358
context.publishEvent(event4);
370-
assertThat(MyNonSingletonListener.seenEvents.contains(event1)).isTrue();
371-
assertThat(MyNonSingletonListener.seenEvents.contains(event2)).isTrue();
372-
assertThat(MyNonSingletonListener.seenEvents.contains(event3)).isTrue();
373-
assertThat(MyNonSingletonListener.seenEvents.contains(event4)).isTrue();
359+
assertThat(MyNonSingletonListener.seenEvents).contains(event1, event2, event3, event4);
374360
MyNonSingletonListener.seenEvents.clear();
375361

376362
context.publishEvent(event1);
377363
context.publishEvent(event2);
378364
context.publishEvent(event3);
379365
context.publishEvent(event4);
380-
assertThat(MyNonSingletonListener.seenEvents.contains(event1)).isTrue();
381-
assertThat(MyNonSingletonListener.seenEvents.contains(event2)).isTrue();
382-
assertThat(MyNonSingletonListener.seenEvents.contains(event3)).isTrue();
383-
assertThat(MyNonSingletonListener.seenEvents.contains(event4)).isTrue();
366+
assertThat(MyNonSingletonListener.seenEvents).contains(event1, event2, event3, event4);
384367
MyNonSingletonListener.seenEvents.clear();
385368

386369
AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
@@ -433,9 +416,7 @@ public void anonymousClassAsListener() {
433416
context.publishEvent(new MyOtherEvent(context));
434417
MyEvent event2 = new MyEvent(context);
435418
context.publishEvent(event2);
436-
assertThat(seenEvents.size()).isSameAs(2);
437-
assertThat(seenEvents.contains(event1)).isTrue();
438-
assertThat(seenEvents.contains(event2)).isTrue();
419+
assertThat(seenEvents).contains(event1, event2);
439420

440421
context.close();
441422
}
@@ -453,9 +434,7 @@ public void lambdaAsListener() {
453434
context.publishEvent(new MyOtherEvent(context));
454435
MyEvent event2 = new MyEvent(context);
455436
context.publishEvent(event2);
456-
assertThat(seenEvents.size()).isSameAs(2);
457-
assertThat(seenEvents.contains(event1)).isTrue();
458-
assertThat(seenEvents.contains(event2)).isTrue();
437+
assertThat(seenEvents).contains(event1, event2);
459438

460439
context.close();
461440
}
@@ -466,8 +445,7 @@ public void lambdaAsListenerWithErrorHandler() {
466445
StaticApplicationContext context = new StaticApplicationContext();
467446
SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
468447
multicaster.setErrorHandler(ReflectionUtils::rethrowRuntimeException);
469-
context.getBeanFactory().registerSingleton(
470-
StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster);
448+
context.getBeanFactory().registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster);
471449
ApplicationListener<MyEvent> listener = seenEvents::add;
472450
context.addApplicationListener(listener);
473451
context.refresh();
@@ -477,9 +455,7 @@ public void lambdaAsListenerWithErrorHandler() {
477455
context.publishEvent(new MyOtherEvent(context));
478456
MyEvent event2 = new MyEvent(context);
479457
context.publishEvent(event2);
480-
assertThat(seenEvents.size()).isSameAs(2);
481-
assertThat(seenEvents.contains(event1)).isTrue();
482-
assertThat(seenEvents.contains(event2)).isTrue();
458+
assertThat(seenEvents).containsExactlyInAnyOrder(event1, event2);
483459

484460
context.close();
485461
}

spring-context/src/test/java/org/springframework/context/support/StaticApplicationContextMulticasterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -56,7 +56,7 @@ protected ConfigurableApplicationContext createContext() throws Exception {
5656
parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
5757
m.put("name", "Albert");
5858
parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
59-
parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
59+
parent.registerSingleton(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
6060
TestApplicationEventMulticaster.class, null);
6161
parent.refresh();
6262
parent.addApplicationListener(parentListener) ;

0 commit comments

Comments
 (0)