Skip to content

Commit

Permalink
Accept unresolvable generics as long as raw event class matches
Browse files Browse the repository at this point in the history
Closes gh-30712
  • Loading branch information
jhoeller committed Jun 21, 2023
1 parent adcdefc commit 714c3c5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Expand Up @@ -168,12 +168,17 @@ public void onApplicationEvent(ApplicationEvent event) {
@Override
public boolean supportsEventType(ResolvableType eventType) {
for (ResolvableType declaredEventType : this.declaredEventTypes) {
if (declaredEventType.isAssignableFrom(eventType)) {
if (eventType.hasUnresolvableGenerics() ?
declaredEventType.toClass().isAssignableFrom(eventType.toClass()) :
declaredEventType.isAssignableFrom(eventType)) {
return true;
}
if (PayloadApplicationEvent.class.isAssignableFrom(eventType.toClass())) {
if (eventType.hasUnresolvableGenerics()) {
return true;
}
ResolvableType payloadType = eventType.as(PayloadApplicationEvent.class).getGeneric();
if (declaredEventType.isAssignableFrom(payloadType) || eventType.hasUnresolvableGenerics()) {
if (declaredEventType.isAssignableFrom(payloadType)) {
return true;
}
}
Expand Down
Expand Up @@ -47,6 +47,8 @@

/**
* @author Stephane Nicoll
* @author Juergen Hoeller
* @author Simon Baslé
*/
public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEventListenerTests {

Expand Down Expand Up @@ -81,6 +83,13 @@ public void genericListenerWrongParameterizedType() {
supportsEventType(false, method, ResolvableType.forClassWithGenerics(GenericTestEvent.class, Long.class));
}

@Test
public void genericListenerWithUnresolvedGenerics() {
Method method = ReflectionUtils.findMethod(
SampleEvents.class, "handleGenericString", GenericTestEvent.class);
supportsEventType(true, method, ResolvableType.forClass(GenericTestEvent.class));
}

@Test
public void listenerWithPayloadAndGenericInformation() {
Method method = ReflectionUtils.findMethod(SampleEvents.class, "handleString", String.class);
Expand Down Expand Up @@ -347,7 +356,7 @@ void genericPayloadDoesNotSupportArbitraryGenericEventType() throws Exception {
var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method);

assertThat(adapter.supportsEventType(ResolvableType.forClass(EntityWrapper.class)))
.as("handleGenericStringPayload(EntityWrapper<String>) with EntityWrapper<?>").isFalse();
.as("handleGenericStringPayload(EntityWrapper<String>) with EntityWrapper<?>").isTrue();
assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(EntityWrapper.class, Integer.class)))
.as("handleGenericStringPayload(EntityWrapper<String>) with EntityWrapper<Integer>").isFalse();
assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(EntityWrapper.class, String.class)))
Expand Down Expand Up @@ -378,7 +387,7 @@ void genericApplicationEventSupportsSpecificType() throws Exception {
var adapter = new ApplicationListenerMethodAdapter(null, ApplicationListenerMethodAdapterTests.class, method);

assertThat(adapter.supportsEventType(ResolvableType.forClass(GenericTestEvent.class)))
.as("handleGenericString(GenericTestEvent<String>) with GenericTestEvent<?>").isFalse();
.as("handleGenericString(GenericTestEvent<String>) with GenericTestEvent<?>").isTrue();
assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(GenericTestEvent.class, Integer.class)))
.as("handleGenericString(GenericTestEvent<String>) with GenericTestEvent<Integer>").isFalse();
assertThat(adapter.supportsEventType(ResolvableType.forClassWithGenerics(GenericTestEvent.class, String.class)))
Expand Down

0 comments on commit 714c3c5

Please sign in to comment.