Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for explicit generic type in PayloadApplicationEvent #24599

Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
Expand All @@ -26,6 +27,7 @@
* <p>Mainly intended for internal use within the framework.
*
* @author Stephane Nicoll
* @author Qimiao Chen
* @since 4.2
* @param <T> the payload type of the event
*/
Expand All @@ -34,22 +36,38 @@ public class PayloadApplicationEvent<T> extends ApplicationEvent implements Reso

private final T payload;

@Nullable
private ResolvableType payloadType;

/**
* Create a new PayloadApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
* @param payload the payload object (never {@code null})
*/
public PayloadApplicationEvent(Object source, T payload) {
this(source, payload, null);
}

/**
* Create a new PayloadApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
* @param payload the payload object (never {@code null})
* @param payloadType the type object of payload object (can be {@code null})
*/
public PayloadApplicationEvent(Object source, T payload, @Nullable ResolvableType payloadType) {
super(source);
Assert.notNull(payload, "Payload must not be null");
this.payload = payload;
this.payloadType = payloadType;
}


@Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(getPayload()));
if(this.payloadType == null){
chenqimiao marked this conversation as resolved.
Show resolved Hide resolved
this.payloadType = ResolvableType.forInstance(getPayload());
}
return ResolvableType.forClassWithGenerics(getClass(), this.payloadType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
applicationEvent = (ApplicationEvent) event;
}
else {
applicationEvent = new PayloadApplicationEvent<>(this, event);
applicationEvent = new PayloadApplicationEvent<>(this, event, eventType);
if (eventType == null) {
eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.springframework.context.event.test.Identifiable;
import org.springframework.context.event.test.TestEvent;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
Expand Down Expand Up @@ -605,6 +606,17 @@ public void conditionDoesNotMatch() {
this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertTotalEventsCount(0);
}
@Test
public void publishEventWithGeneric() throws NoSuchMethodException {
MyAnnotationConfigApplicationContext ctx = new MyAnnotationConfigApplicationContext();
context = ctx;
ctx.register(MyEventWithGenericListener.class);
ctx.refresh();
ResolvableType resolvableType = ResolvableType.forMethodParameter(MyEventWithGenericListener.class.getMethod("onMyEventWithGeneric",MyEventWithGeneric.class),0);
ctx.publishEvent(new MyEventWithGeneric<String>(),resolvableType);
assertThat(MyEventWithGenericListener.count).isEqualTo(1);
}


@Test
public void orderedListeners() {
Expand Down Expand Up @@ -1117,4 +1129,29 @@ public UseMissingEventListener(Optional<MyEventListener> notHere) {
}
}

public static class MyAnnotationConfigApplicationContext extends AnnotationConfigApplicationContext{

@Override
public void publishEvent(Object event, ResolvableType eventType) {
super.publishEvent(event, eventType);
}
}

public static class MyEventWithGeneric<T> {

}

@Component
public static class MyEventWithGenericListener{

public static int count ;

@EventListener
public void onMyEventWithGeneric(MyEventWithGeneric<String> event){
count ++;
}


}

}