11/*
2- * Copyright 2002-2017 the original author or authors.
2+ * Copyright 2002-2018 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -173,8 +173,9 @@ private void doInvokeListener(ApplicationListener listener, ApplicationEvent eve
173173 }
174174 catch (ClassCastException ex ) {
175175 String msg = ex .getMessage ();
176- if (msg == null || msg . startsWith ( event .getClass ().getName ())) {
176+ if (msg == null || matchesClassCastMessage ( msg , event .getClass ().getName ())) {
177177 // 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.
178179 Log logger = LogFactory .getLog (getClass ());
179180 if (logger .isDebugEnabled ()) {
180181 logger .debug ("Non-matching event type for listener: " + listener , ex );
@@ -186,4 +187,18 @@ private void doInvokeListener(ApplicationListener listener, ApplicationEvent eve
186187 }
187188 }
188189
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+
189204}
0 commit comments