From e458777925176e3cbfb597e234eadd76f8498912 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 26 Jul 2018 14:41:53 +0200 Subject: [PATCH] Properly identify event-related ClassCastExceptions on JDK 11 Issue: SPR-17093 --- .../event/SimpleApplicationEventMulticaster.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java b/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java index eace00211a..d62de6834e 100644 --- a/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java +++ b/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java @@ -173,7 +173,7 @@ public class SimpleApplicationEventMulticaster extends AbstractApplicationEventM } catch (ClassCastException ex) { String msg = ex.getMessage(); - if (msg == null || matchesClassCastMessage(msg, event.getClass().getName())) { + if (msg == null || matchesClassCastMessage(msg, event.getClass())) { // Possibly a lambda-defined listener which we could not resolve the generic event type for // -> let's suppress the exception and just log a debug message. Log logger = LogFactory.getLog(getClass()); @@ -187,14 +187,18 @@ public class SimpleApplicationEventMulticaster extends AbstractApplicationEventM } } - private boolean matchesClassCastMessage(String classCastMessage, String eventClassName) { - // On Java 8, the message simply starts with the class name: "java.lang.String cannot be cast..." - if (classCastMessage.startsWith(eventClassName)) { + private boolean matchesClassCastMessage(String classCastMessage, Class eventClass) { + // On Java 8, the message starts with the class name: "java.lang.String cannot be cast..." + if (classCastMessage.startsWith(eventClass.getName())) { return true; } - // On Java 9, the message contains the module name: "java.base/java.lang.String cannot be cast..." + // On Java 11, the message starts with "class ..." a.k.a. Class.toString() + if (classCastMessage.startsWith(eventClass.toString())) { + return true; + } + // On Java 9, the message used to contain the module name: "java.base/java.lang.String cannot be cast..." int moduleSeparatorIndex = classCastMessage.indexOf('/'); - if (moduleSeparatorIndex != -1 && classCastMessage.startsWith(eventClassName, moduleSeparatorIndex + 1)) { + if (moduleSeparatorIndex != -1 && classCastMessage.startsWith(eventClass.getName(), moduleSeparatorIndex + 1)) { return true; } // Assuming an unrelated class cast failure...