Skip to content

Commit b387d9b

Browse files
committed
MethodIntrospector handles overriding bridge method correctly
Closes gh-30906 (cherry picked from commit 616f728)
1 parent 0f33f79 commit b387d9b

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

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

Lines changed: 23 additions & 10 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.
@@ -164,12 +164,12 @@ void contextEventsAreReceived() {
164164
ContextEventListener listener = this.context.getBean(ContextEventListener.class);
165165

166166
List<Object> events = this.eventCollector.getEvents(listener);
167-
assertThat(events.size()).as("Wrong number of initial context events").isEqualTo(1);
167+
assertThat(events).as("Wrong number of initial context events").hasSize(1);
168168
assertThat(events.get(0).getClass()).isEqualTo(ContextRefreshedEvent.class);
169169

170170
this.context.stop();
171171
List<Object> eventsAfterStop = this.eventCollector.getEvents(listener);
172-
assertThat(eventsAfterStop.size()).as("Wrong number of context events on shutdown").isEqualTo(2);
172+
assertThat(eventsAfterStop).as("Wrong number of context events on shutdown").hasSize(2);
173173
assertThat(eventsAfterStop.get(1).getClass()).isEqualTo(ContextStoppedEvent.class);
174174
this.eventCollector.assertTotalEventsCount(2);
175175
}
@@ -334,7 +334,7 @@ void eventListenerWorksWithSimpleInterfaceProxy() {
334334
load(ScopedProxyTestBean.class);
335335

336336
SimpleService proxy = this.context.getBean(SimpleService.class);
337-
assertThat(proxy instanceof Advised).as("bean should be a proxy").isTrue();
337+
assertThat(proxy).as("bean should be a proxy").isInstanceOf(Advised.class);
338338
this.eventCollector.assertNoEventReceived(proxy.getId());
339339

340340
this.context.publishEvent(new ContextRefreshedEvent(this.context));
@@ -351,7 +351,7 @@ void eventListenerWorksWithAnnotatedInterfaceProxy() {
351351
load(AnnotatedProxyTestBean.class);
352352

353353
AnnotatedSimpleService proxy = this.context.getBean(AnnotatedSimpleService.class);
354-
assertThat(proxy instanceof Advised).as("bean should be a proxy").isTrue();
354+
assertThat(proxy).as("bean should be a proxy").isInstanceOf(Advised.class);
355355
this.eventCollector.assertNoEventReceived(proxy.getId());
356356

357357
this.context.publishEvent(new ContextRefreshedEvent(this.context));
@@ -517,7 +517,6 @@ void replyWithPayload() {
517517
ReplyEventListener replyEventListener = this.context.getBean(ReplyEventListener.class);
518518
TestEventListener listener = this.context.getBean(TestEventListener.class);
519519

520-
521520
this.eventCollector.assertNoEventReceived(listener);
522521
this.eventCollector.assertNoEventReceived(replyEventListener);
523522
this.context.publishEvent(event);
@@ -634,6 +633,17 @@ void orderedListeners() {
634633
assertThat(listener.order).contains("first", "second", "third");
635634
}
636635

636+
@Test
637+
void publicSubclassWithInheritedEventListener() {
638+
load(PublicSubclassWithInheritedEventListener.class);
639+
TestEventListener listener = this.context.getBean(PublicSubclassWithInheritedEventListener.class);
640+
641+
this.eventCollector.assertNoEventReceived(listener);
642+
this.context.publishEvent("test");
643+
this.eventCollector.assertEvent(listener, "test");
644+
this.eventCollector.assertTotalEventsCount(1);
645+
}
646+
637647
@Test @Disabled // SPR-15122
638648
void listenersReceiveEarlyEvents() {
639649
load(EventOnPostConstruct.class, OrderedTestListener.class);
@@ -646,7 +656,7 @@ void listenersReceiveEarlyEvents() {
646656
void missingListenerBeanIgnored() {
647657
load(MissingEventListener.class);
648658
context.getBean(UseMissingEventListener.class);
649-
context.getBean(ApplicationEventMulticaster.class).multicastEvent(new TestEvent(this));
659+
context.publishEvent(new TestEvent(this));
650660
}
651661

652662

@@ -753,7 +763,6 @@ static class ContextEventListener extends AbstractTestEventListener {
753763
public void handleContextEvent(ApplicationContextEvent event) {
754764
collectEvent(event);
755765
}
756-
757766
}
758767

759768

@@ -980,7 +989,6 @@ public void handleString(GenericEventPojo<String> value) {
980989
}
981990

982991

983-
984992
@EventListener
985993
@Retention(RetentionPolicy.RUNTIME)
986994
public @interface ConditionalEvent {
@@ -1032,7 +1040,7 @@ public void handleRatio(Double ratio) {
10321040
}
10331041

10341042

1035-
@Configuration
1043+
@Component
10361044
static class OrderedTestListener extends TestEventListener {
10371045

10381046
public final List<String> order = new ArrayList<>();
@@ -1056,6 +1064,11 @@ public void handleSecond(String payload) {
10561064
}
10571065

10581066

1067+
@Component
1068+
public static class PublicSubclassWithInheritedEventListener extends TestEventListener {
1069+
}
1070+
1071+
10591072
static class EventOnPostConstruct {
10601073

10611074
@Autowired

spring-core/src/main/java/org/springframework/core/MethodIntrospector.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -74,7 +74,8 @@ public static <T> Map<Method, T> selectMethods(Class<?> targetType, final Metada
7474
T result = metadataLookup.inspect(specificMethod);
7575
if (result != null) {
7676
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
77-
if (bridgedMethod == specificMethod || metadataLookup.inspect(bridgedMethod) == null) {
77+
if (bridgedMethod == specificMethod || bridgedMethod == method ||
78+
metadataLookup.inspect(bridgedMethod) == null) {
7879
methodMap.put(specificMethod, result);
7980
}
8081
}

0 commit comments

Comments
 (0)