Skip to content

Commit

Permalink
MethodIntrospector handles overriding bridge method correctly
Browse files Browse the repository at this point in the history
Closes gh-30906

(cherry picked from commit 616f728)
  • Loading branch information
jhoeller committed Jul 18, 2023
1 parent 0f33f79 commit b387d9b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
@@ -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.
Expand Down Expand Up @@ -164,12 +164,12 @@ void contextEventsAreReceived() {
ContextEventListener listener = this.context.getBean(ContextEventListener.class);

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

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

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

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

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

this.context.publishEvent(new ContextRefreshedEvent(this.context));
Expand Down Expand Up @@ -517,7 +517,6 @@ void replyWithPayload() {
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);
Expand Down Expand Up @@ -634,6 +633,17 @@ void orderedListeners() {
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);
Expand All @@ -646,7 +656,7 @@ void listenersReceiveEarlyEvents() {
void missingListenerBeanIgnored() {
load(MissingEventListener.class);
context.getBean(UseMissingEventListener.class);
context.getBean(ApplicationEventMulticaster.class).multicastEvent(new TestEvent(this));
context.publishEvent(new TestEvent(this));
}


Expand Down Expand Up @@ -753,7 +763,6 @@ static class ContextEventListener extends AbstractTestEventListener {
public void handleContextEvent(ApplicationContextEvent event) {
collectEvent(event);
}

}


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



@EventListener
@Retention(RetentionPolicy.RUNTIME)
public @interface ConditionalEvent {
Expand Down Expand Up @@ -1032,7 +1040,7 @@ public void handleRatio(Double ratio) {
}


@Configuration
@Component
static class OrderedTestListener extends TestEventListener {

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


@Component
public static class PublicSubclassWithInheritedEventListener extends TestEventListener {
}


static class EventOnPostConstruct {

@Autowired
Expand Down
@@ -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.
Expand Down Expand Up @@ -74,7 +74,8 @@ public static <T> Map<Method, T> selectMethods(Class<?> targetType, final Metada
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);
}
}
Expand Down

0 comments on commit b387d9b

Please sign in to comment.