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

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

Closed
spring-projects-issues opened this issue Oct 26, 2016 · 1 comment

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Oct 26, 2016

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:

  • #18681 Wrong detection of event type on ApplicationListener<> when using lambdas (ClassCast Exception)
  • #20981 Lambda error detection might not work on JDK 9
  • #19711 SimpleApplicationEventMulticaster should not rely on ClassCastException having a message
  • #21630 Classification of ClassCastExceptions doesn't work in JDK 11 (OpenJDK)

Backported to: 4.2.9

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 27, 2016

Juergen Hoeller commented

As of 4.3.4 / 4.2.9, we're now just swallowing a ClassCastException if it affects the passed-in event class directly (as in the case of a lambda-defined listener for a specific event type where we don't know the declared type upfront but the lambda-generated implementation immediately casts the given event to a specific type). This is hopefully an acceptable compromise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants