Accept unresolvable generics as long as raw event class matches
Closes gh-30712
This commit is contained in:
@@ -168,12 +168,17 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,8 @@ import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
* @author Juergen Hoeller
|
||||
* @author Simon Baslé
|
||||
*/
|
||||
public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEventListenerTests {
|
||||
|
||||
@@ -81,6 +83,13 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
|
||||
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);
|
||||
@@ -347,7 +356,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
|
||||
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)))
|
||||
@@ -378,7 +387,7 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
|
||||
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)))
|
||||
|
||||
Reference in New Issue
Block a user