SmartApplicationListener getSource should Never be null,ApplicationEvent is extend from EventObject source is set by Constructor Spring ApplicationEvent#getSource does not need to be guarded against a null source

Comment From: snicoll

@liaozan sharing screenshot like this isn't very helpful I am afraid. What getSource method are you talking about? There isn't one on SmartApplicationListener.

Comment From: refeccd

sorry about sharing screenshot. getSource is not directly present on SmartApplicationListener, It is used as a supportsSourceType method on SmartApplicationListener and it annoted with Nullable, but I looked at the code and found that source comes from EventObject, and it will never be Null because he made a judgment in the construction method.

protected Collection<ApplicationListener<?>> getApplicationListeners(
                  ApplicationEvent event, ResolvableType eventType) {

    Object source = event.getSource();
    Class<?> sourceType = (source != null ? source.getClass() : null);
    ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);
    // other code
    return retrieveApplicationListeners(eventType, sourceType, newRetriever);
}

Comment From: snicoll

Those are actually two different things. The code above is from AbstractApplicationEventMulticaster and the call on getClass could indeed be simplified but the supportsSourceType is disconnected from that and it is intentional that we want it to be nullable. I'll use this issue to simplify the code in AbstractApplicationEventMulticaster.

Comment From: jhoeller

It's worth noting that the getSource() method may be overridden in custom ApplicationEvent subclasses, potentially returning null. Not that this is advisable but it may still happen for certain user event instances. This might be reason enough to keep the defensive check there.

Comment From: snicoll

Ah, good point. Let's keep things as they are then.