Introduce ApplicationContextEvent.getSource() with covariant return type

Prior to this commit, ApplicationContextEvent inherited getSource()
from java.util.EventObject.getSource() which has an Object return type.

This commit introduces a local getSource() implementation in
ApplicationContextEvent with an ApplicationContext covariant return
type, analogous to TestContextEvent in spring-test.

Closes gh-35197
This commit is contained in:
Sam Brannen
2025-07-14 14:09:55 +02:00
parent 096670f31d
commit 5c6622fd77
@@ -20,9 +20,10 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
/**
* Base class for events raised for an {@code ApplicationContext}.
* Base class for events raised for an {@link ApplicationContext}.
*
* @author Juergen Hoeller
* @author Sam Brannen
* @since 2.5
*/
@SuppressWarnings("serial")
@@ -30,7 +31,7 @@ public abstract class ApplicationContextEvent extends ApplicationEvent {
/**
* Create a new {@code ApplicationContextEvent}.
* @param source the {@code ApplicationContext} that the event is raised for
* @param source the {@link ApplicationContext} that the event is raised for
* (must not be {@code null})
*/
public ApplicationContextEvent(ApplicationContext source) {
@@ -38,10 +39,22 @@ public abstract class ApplicationContextEvent extends ApplicationEvent {
}
/**
* Get the {@code ApplicationContext} that the event was raised for.
* Get the {@link ApplicationContext} that the event was raised for.
* @return the {@code ApplicationContext} that the event was raised for
* @since 7.0
* @see #getApplicationContext()
*/
@Override
public ApplicationContext getSource() {
return getApplicationContext();
}
/**
* Get the {@link ApplicationContext} that the event was raised for.
* @see #getSource()
*/
public final ApplicationContext getApplicationContext() {
return (ApplicationContext) getSource();
return (ApplicationContext) super.getSource();
}
}