1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -173,8 +173,9 @@ private void doInvokeListener(ApplicationListener listener, ApplicationEvent eve
173
173
}
174
174
catch (ClassCastException ex ) {
175
175
String msg = ex .getMessage ();
176
- if (msg == null || msg . startsWith ( event .getClass ().getName ())) {
176
+ if (msg == null || matchesClassCastMessage ( msg , event .getClass ().getName ())) {
177
177
// Possibly a lambda-defined listener which we could not resolve the generic event type for
178
+ // -> let's suppress the exception and just log a debug message.
178
179
Log logger = LogFactory .getLog (getClass ());
179
180
if (logger .isDebugEnabled ()) {
180
181
logger .debug ("Non-matching event type for listener: " + listener , ex );
@@ -186,4 +187,18 @@ private void doInvokeListener(ApplicationListener listener, ApplicationEvent eve
186
187
}
187
188
}
188
189
190
+ private boolean matchesClassCastMessage (String classCastMessage , String eventClassName ) {
191
+ // On Java 8, the message simply starts with the class name: "java.lang.String cannot be cast..."
192
+ if (classCastMessage .startsWith (eventClassName )) {
193
+ return true ;
194
+ }
195
+ // On Java 9, the message contains the module name: "java.base/java.lang.String cannot be cast..."
196
+ int moduleSeparatorIndex = classCastMessage .indexOf ('/' );
197
+ if (moduleSeparatorIndex != -1 && classCastMessage .startsWith (eventClassName , moduleSeparatorIndex + 1 )) {
198
+ return true ;
199
+ }
200
+ // Assuming an unrelated class cast failure...
201
+ return false ;
202
+ }
203
+
189
204
}
0 commit comments