Skip to content

SimpleApplicationEventMulticaster should not generally suppress ClassCastException [SPR-14846] #19412

Closed
@spring-projects-issues

Description

@spring-projects-issues

Damien Hollis opened SPR-14846 and commented

We have a situation where an ApplicationListener was throwing a ClassCastException but we were never aware of this issue because invokeListener defaults to catching and suppressing ClassCastExceptions:

protected void invokeListener(ApplicationListener listener, ApplicationEvent event) {
     ErrorHandler errorHandler = getErrorHandler();
     if (errorHandler != null) {
          try {
               listener.onApplicationEvent(event);
          }
          catch (Throwable err) {
               errorHandler.handleError(err);
          }
     }
     else {
          try {
               listener.onApplicationEvent(event);
          }
          catch (ClassCastException ex) {
               // Possibly a lambda-defined listener which we could not resolve the generic event type for
               LogFactory.getLog(getClass()).debug("Non-matching event type for listener: " + listener, ex);
          }
     }
}

This seems like a very bad idea and in our case led to a very subtle bug (luckily we had other checks and balances that showed we had an issue but it took a long time to track it down). I'm not sure what can happen with a lambda-defined listener, so I can't suggest a better solution than what you have.

We have now implemented our own ErrorHandler, so this issue is resolved for us but other users may not be aware of ClassCastExceptions in their own listeners.


Affects: 4.2.8, 4.3.3

Issue Links:

Backported to: 4.2.9

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions