Browse Source

MethodIntrospector handles overriding bridge method correctly

Closes gh-30906
pull/30915/head
Juergen Hoeller 2 years ago
parent
commit
616f728afa
  1. 23
      spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java
  2. 5
      spring-core/src/main/java/org/springframework/core/MethodIntrospector.java

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

@ -517,7 +517,6 @@ class AnnotationDrivenEventListenerTests { @@ -517,7 +517,6 @@ class AnnotationDrivenEventListenerTests {
ReplyEventListener replyEventListener = this.context.getBean(ReplyEventListener.class);
TestEventListener listener = this.context.getBean(TestEventListener.class);
this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertNoEventReceived(replyEventListener);
this.context.publishEvent(event);
@ -634,6 +633,17 @@ class AnnotationDrivenEventListenerTests { @@ -634,6 +633,17 @@ class AnnotationDrivenEventListenerTests {
assertThat(listener.order).contains("first", "second", "third");
}
@Test
void publicSubclassWithInheritedEventListener() {
load(PublicSubclassWithInheritedEventListener.class);
TestEventListener listener = this.context.getBean(PublicSubclassWithInheritedEventListener.class);
this.eventCollector.assertNoEventReceived(listener);
this.context.publishEvent("test");
this.eventCollector.assertEvent(listener, "test");
this.eventCollector.assertTotalEventsCount(1);
}
@Test @Disabled // SPR-15122
void listenersReceiveEarlyEvents() {
load(EventOnPostConstruct.class, OrderedTestListener.class);
@ -646,7 +656,7 @@ class AnnotationDrivenEventListenerTests { @@ -646,7 +656,7 @@ class AnnotationDrivenEventListenerTests {
void missingListenerBeanIgnored() {
load(MissingEventListener.class);
context.getBean(UseMissingEventListener.class);
context.getBean(ApplicationEventMulticaster.class).multicastEvent(new TestEvent(this));
context.publishEvent(new TestEvent(this));
}
@ -753,7 +763,6 @@ class AnnotationDrivenEventListenerTests { @@ -753,7 +763,6 @@ class AnnotationDrivenEventListenerTests {
public void handleContextEvent(ApplicationContextEvent event) {
collectEvent(event);
}
}
@ -979,7 +988,6 @@ class AnnotationDrivenEventListenerTests { @@ -979,7 +988,6 @@ class AnnotationDrivenEventListenerTests {
}
@EventListener
@Retention(RetentionPolicy.RUNTIME)
public @interface ConditionalEvent {
@ -1031,7 +1039,7 @@ class AnnotationDrivenEventListenerTests { @@ -1031,7 +1039,7 @@ class AnnotationDrivenEventListenerTests {
}
@Configuration
@Component
static class OrderedTestListener extends TestEventListener {
public final List<String> order = new ArrayList<>();
@ -1055,6 +1063,11 @@ class AnnotationDrivenEventListenerTests { @@ -1055,6 +1063,11 @@ class AnnotationDrivenEventListenerTests {
}
@Component
public static class PublicSubclassWithInheritedEventListener extends TestEventListener {
}
static class EventOnPostConstruct {
@Autowired

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
@ -74,7 +74,8 @@ public final class MethodIntrospector { @@ -74,7 +74,8 @@ public final class MethodIntrospector {
T result = metadataLookup.inspect(specificMethod);
if (result != null) {
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
if (bridgedMethod == specificMethod || metadataLookup.inspect(bridgedMethod) == null) {
if (bridgedMethod == specificMethod || bridgedMethod == method ||
metadataLookup.inspect(bridgedMethod) == null) {
methodMap.put(specificMethod, result);
}
}

Loading…
Cancel
Save