You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SimpleApplicationEventMulticaster should not rely on ClassCastException having a message [SPR-15145] #19711
SimpleApplicationEventMulticaster in this commit driven by #19412 changed the ClassCastException catch logic to:
catch (ClassCastExceptionex) {
if (ex.getMessage().startsWith(event.getClass().getName())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type forLogFactory.getLog(getClass()).debug("Non-matching event type for listener: " + listener, ex);
} else {
throwex;
}
}
Note that if ex.getMessage() returns NULL whole method will throw NPE.
To speed up execution of methods that frequently throw exceptions, the C2 compiler generates code that uses a pre-built exception (pre-built at compile time). The pre-built exception does not contain neither a stack trace nor a message detailing the exception.
This means that after some time the compiler can replace normal ClassCastException with empty one, without message nor stack trace.
More detailed explanation of this behaviour can be found here:
I'm not convinced if the check you've made is a good idea:
catch (ClassCastExceptionex) {
Stringmsg = ex.getMessage();
if (msg != null && msg.startsWith(event.getClass().getName())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type forLoglogger = LogFactory.getLog(getClass());
if (logger.isDebugEnabled()) {
logger.debug("Non-matching event type for listener: " + listener, ex);
}
} else {
throwex;
}
}
Current behaviour will work only until compiler decides to start throwing pre-build exception. After that the catch logic will completely change, throwing exception instead of swallowing it, which may be even harder to debug.
Honestly I'm not sure if this issue can be solved here. Maybe Spring should prohibit registration of ApplicationListener<?> and throw exceptions during startup instead? For example, the correct way to register listener could look like this, if possible:
Good point. It's actually closer to our intentions if we turn that check into (msg == null || msg.startsWith(event.getClass().getName())), rather over-swallowing than under-swallowing, closer to how we had it originally. If there is some unrelated ClassCastException involved, developers are going to notice it on first appearance anyway. I'll do that right away.
As for other ways of identifying lambda-defined listeners, we got a few related JIRA tickets already. For the time being, we cannot change the rules there since we're in the middle of the 4.3.x line. I'm afraid we can only fine-tune the current approach.
Activity
spring-projects-issues commentedon Jan 16, 2017
Juergen Hoeller commented
Good catch! Fixed in master and to be backported to 4.3.6.
spring-projects-issues commentedon Jan 17, 2017
Mariusz Luciow commented
Hi Juergen,
I'm not convinced if the check you've made is a good idea:
Current behaviour will work only until compiler decides to start throwing pre-build exception. After that the catch logic will completely change, throwing exception instead of swallowing it, which may be even harder to debug.
Honestly I'm not sure if this issue can be solved here. Maybe Spring should prohibit registration of ApplicationListener<?> and throw exceptions during startup instead? For example, the correct way to register listener could look like this, if possible:
spring-projects-issues commentedon Jan 17, 2017
Juergen Hoeller commented
Good point. It's actually closer to our intentions if we turn that check into
(msg == null || msg.startsWith(event.getClass().getName())), rather over-swallowing than under-swallowing, closer to how we had it originally. If there is some unrelatedClassCastExceptioninvolved, developers are going to notice it on first appearance anyway. I'll do that right away.As for other ways of identifying lambda-defined listeners, we got a few related JIRA tickets already. For the time being, we cannot change the rules there since we're in the middle of the 4.3.x line. I'm afraid we can only fine-tune the current approach.