Polishing

This commit is contained in:
Sam Brannen
2023-03-03 15:31:27 +01:00
parent 3dd0fbfb57
commit 3854861a8a
5 changed files with 43 additions and 61 deletions

View File

@@ -248,20 +248,26 @@ public abstract class AbstractAspectJAdvice implements Advice, AspectJPrecedence
}
/**
* Set by creator of this advice object if the argument names are known.
* <p>This could be for example because they have been explicitly specified in XML,
* Set by the creator of this advice object if the argument names are known.
* <p>This could be for example because they have been explicitly specified in XML
* or in an advice annotation.
* @param argNames comma delimited list of arg names
* @param argumentNames comma delimited list of argument names
*/
public void setArgumentNames(String argNames) {
String[] tokens = StringUtils.commaDelimitedListToStringArray(argNames);
public void setArgumentNames(String argumentNames) {
String[] tokens = StringUtils.commaDelimitedListToStringArray(argumentNames);
setArgumentNamesFromStringArray(tokens);
}
public void setArgumentNamesFromStringArray(String... args) {
this.argumentNames = new String[args.length];
for (int i = 0; i < args.length; i++) {
this.argumentNames[i] = args[i].strip();
/**
* Set by the creator of this advice object if the argument names are known.
* <p>This could be for example because they have been explicitly specified in XML
* or in an advice annotation.
* @param argumentNames list of argument names
*/
public void setArgumentNamesFromStringArray(String... argumentNames) {
this.argumentNames = new String[argumentNames.length];
for (int i = 0; i < argumentNames.length; i++) {
this.argumentNames[i] = argumentNames[i].strip();
if (!isVariableName(this.argumentNames[i])) {
throw new IllegalArgumentException(
"'argumentNames' property of AbstractAspectJAdvice contains an argument name '" +

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,8 +52,8 @@ import org.springframework.util.ObjectUtils;
*
* <p>Implementing ApplicationEventMulticaster's actual {@link #multicastEvent} method
* is left to subclasses. {@link SimpleApplicationEventMulticaster} simply multicasts
* all events to all registered listeners, invoking them in the calling thread.
* Alternative implementations could be more sophisticated in those respects.
* all events to all registered listeners, invoking them in the calling thread by
* default. Alternative implementations could be more sophisticated in those respects.
*
* @author Juergen Hoeller
* @author Stephane Nicoll
@@ -82,10 +82,10 @@ public abstract class AbstractApplicationEventMulticaster
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof ConfigurableBeanFactory)) {
if (!(beanFactory instanceof ConfigurableBeanFactory cbf)) {
throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
}
this.beanFactory = (ConfigurableBeanFactory) beanFactory;
this.beanFactory = cbf;
if (this.beanClassLoader == null) {
this.beanClassLoader = this.beanFactory.getBeanClassLoader();
}
@@ -373,8 +373,8 @@ public abstract class AbstractApplicationEventMulticaster
protected boolean supportsEvent(
ApplicationListener<?> listener, ResolvableType eventType, @Nullable Class<?> sourceType) {
GenericApplicationListener smartListener = (listener instanceof GenericApplicationListener ?
(GenericApplicationListener) listener : new GenericApplicationListenerAdapter(listener));
GenericApplicationListener smartListener = (listener instanceof GenericApplicationListener gal ? gal :
new GenericApplicationListenerAdapter(listener));
return (smartListener.supportsEventType(eventType) && smartListener.supportsSourceType(sourceType));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -111,7 +111,7 @@ class AnnotationDrivenEventListenerTests {
this.eventCollector.assertTotalEventsCount(1);
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().contains("TestEvent"));
l instanceof SmartApplicationListener sal && sal.getListenerId().contains("TestEvent"));
this.eventCollector.clear();
this.context.publishEvent(event);
this.eventCollector.assertNoEventReceived(listener);
@@ -132,7 +132,7 @@ class AnnotationDrivenEventListenerTests {
this.eventCollector.assertTotalEventsCount(1);
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().contains("TestEvent"));
l instanceof SmartApplicationListener sal && sal.getListenerId().contains("TestEvent"));
this.eventCollector.clear();
this.context.publishEvent(event);
this.eventCollector.assertNoEventReceived(listener);
@@ -150,7 +150,7 @@ class AnnotationDrivenEventListenerTests {
this.eventCollector.assertTotalEventsCount(1);
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
l instanceof SmartApplicationListener && ((SmartApplicationListener) l).getListenerId().equals("foo"));
l instanceof SmartApplicationListener sal && sal.getListenerId().equals("foo"));
this.eventCollector.clear();
this.context.publishEvent(event);
this.eventCollector.assertNoEventReceived(bean);

View File

@@ -39,7 +39,6 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.PayloadApplicationEvent;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.context.support.StaticMessageSource;
@@ -59,6 +58,7 @@ import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME;
/**
* Unit and integration tests for the ApplicationContext event support.
@@ -163,7 +163,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
willThrow(thrown).given(listener).onApplicationEvent(evt);
assertThatRuntimeException()
.isThrownBy(() -> smc.multicastEvent(evt))
.satisfies(ex -> assertThat(ex).isSameAs(thrown));
.isSameAs(thrown);
}
@Test
@@ -282,20 +282,14 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
context.publishEvent(event3);
MyOtherEvent event4 = new MyOtherEvent(context);
context.publishEvent(event4);
assertThat(listener1.seenEvents.contains(event1)).isTrue();
assertThat(listener1.seenEvents.contains(event2)).isTrue();
assertThat(listener1.seenEvents.contains(event3)).isTrue();
assertThat(listener1.seenEvents.contains(event4)).isTrue();
assertThat(listener1.seenEvents).contains(event1, event2, event3, event4);
listener1.seenEvents.clear();
context.publishEvent(event1);
context.publishEvent(event2);
context.publishEvent(event3);
context.publishEvent(event4);
assertThat(listener1.seenEvents.contains(event1)).isTrue();
assertThat(listener1.seenEvents.contains(event2)).isTrue();
assertThat(listener1.seenEvents.contains(event3)).isTrue();
assertThat(listener1.seenEvents.contains(event4)).isTrue();
assertThat(listener1.seenEvents).contains(event1, event2, event3, event4);
AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
assertThat(multicaster.retrieverCache).hasSize(2);
@@ -314,10 +308,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
context.publishEvent("event2");
context.publishEvent("event3");
context.publishEvent("event4");
assertThat(listener.seenPayloads.contains("event1")).isTrue();
assertThat(listener.seenPayloads.contains("event2")).isTrue();
assertThat(listener.seenPayloads.contains("event3")).isTrue();
assertThat(listener.seenPayloads.contains("event4")).isTrue();
assertThat(listener.seenPayloads).contains("event1", "event2", "event3", "event4");
AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
assertThat(multicaster.retrieverCache).hasSize(2);
@@ -340,15 +331,13 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
MyOrderedListener1 listener1 = context.getBean("listener1", MyOrderedListener1.class);
MyEvent event1 = new MyEvent(context);
context.publishEvent(event1);
assertThat(listener1.seenEvents.contains(event1)).isTrue();
assertThat(listener1.seenEvents).contains(event1);
SimpleApplicationEventMulticaster multicaster = context.getBean(
AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
SimpleApplicationEventMulticaster.class);
assertThat(multicaster.getApplicationListeners().isEmpty()).isFalse();
SimpleApplicationEventMulticaster multicaster = context.getBean(SimpleApplicationEventMulticaster.class);
assertThat(multicaster.getApplicationListeners()).isNotEmpty();
context.close();
assertThat(multicaster.getApplicationListeners().isEmpty()).isTrue();
assertThat(multicaster.getApplicationListeners()).isEmpty();
}
@Test
@@ -367,20 +356,14 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
context.publishEvent(event3);
MyOtherEvent event4 = new MyOtherEvent(context);
context.publishEvent(event4);
assertThat(MyNonSingletonListener.seenEvents.contains(event1)).isTrue();
assertThat(MyNonSingletonListener.seenEvents.contains(event2)).isTrue();
assertThat(MyNonSingletonListener.seenEvents.contains(event3)).isTrue();
assertThat(MyNonSingletonListener.seenEvents.contains(event4)).isTrue();
assertThat(MyNonSingletonListener.seenEvents).contains(event1, event2, event3, event4);
MyNonSingletonListener.seenEvents.clear();
context.publishEvent(event1);
context.publishEvent(event2);
context.publishEvent(event3);
context.publishEvent(event4);
assertThat(MyNonSingletonListener.seenEvents.contains(event1)).isTrue();
assertThat(MyNonSingletonListener.seenEvents.contains(event2)).isTrue();
assertThat(MyNonSingletonListener.seenEvents.contains(event3)).isTrue();
assertThat(MyNonSingletonListener.seenEvents.contains(event4)).isTrue();
assertThat(MyNonSingletonListener.seenEvents).contains(event1, event2, event3, event4);
MyNonSingletonListener.seenEvents.clear();
AbstractApplicationEventMulticaster multicaster = context.getBean(AbstractApplicationEventMulticaster.class);
@@ -433,9 +416,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
context.publishEvent(new MyOtherEvent(context));
MyEvent event2 = new MyEvent(context);
context.publishEvent(event2);
assertThat(seenEvents.size()).isSameAs(2);
assertThat(seenEvents.contains(event1)).isTrue();
assertThat(seenEvents.contains(event2)).isTrue();
assertThat(seenEvents).contains(event1, event2);
context.close();
}
@@ -453,9 +434,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
context.publishEvent(new MyOtherEvent(context));
MyEvent event2 = new MyEvent(context);
context.publishEvent(event2);
assertThat(seenEvents.size()).isSameAs(2);
assertThat(seenEvents.contains(event1)).isTrue();
assertThat(seenEvents.contains(event2)).isTrue();
assertThat(seenEvents).contains(event1, event2);
context.close();
}
@@ -466,8 +445,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
StaticApplicationContext context = new StaticApplicationContext();
SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
multicaster.setErrorHandler(ReflectionUtils::rethrowRuntimeException);
context.getBeanFactory().registerSingleton(
StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster);
context.getBeanFactory().registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, multicaster);
ApplicationListener<MyEvent> listener = seenEvents::add;
context.addApplicationListener(listener);
context.refresh();
@@ -477,9 +455,7 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
context.publishEvent(new MyOtherEvent(context));
MyEvent event2 = new MyEvent(context);
context.publishEvent(event2);
assertThat(seenEvents.size()).isSameAs(2);
assertThat(seenEvents.contains(event1)).isTrue();
assertThat(seenEvents.contains(event2)).isTrue();
assertThat(seenEvents).containsExactlyInAnyOrder(event1, event2);
context.close();
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public class StaticApplicationContextMulticasterTests extends AbstractApplicatio
parent.registerPrototype("rod", TestBean.class, new MutablePropertyValues(m));
m.put("name", "Albert");
parent.registerPrototype("father", TestBean.class, new MutablePropertyValues(m));
parent.registerSingleton(StaticApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
parent.registerSingleton(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
TestApplicationEventMulticaster.class, null);
parent.refresh();
parent.addApplicationListener(parentListener) ;