From 0ca8f1083a4a7cfb2b604f4c483b69eb01773a5f Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 10 Aug 2018 09:48:22 +0200 Subject: [PATCH] Fix ClassCastException message detection on Java 11 This commit also fixes the detection of a ClassCastException that can be safely ignored on the module path with Java 9 Closes gh-14033 --- .../org/springframework/boot/util/LambdaSafe.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java index d8476c2f9ba..78267f85525 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/util/LambdaSafe.java @@ -188,9 +188,22 @@ public final class LambdaSafe { return false; } Class argumentType = argument.getClass(); + // On Java 8, the message starts with the class name: "java.lang.String cannot + // be cast..." if (message.startsWith(argumentType.getName())) { return true; } + // On Java 11, the message starts with "class ..." a.k.a. Class.toString() + if (message.startsWith(argumentType.toString())) { + return true; + } + // On Java 9, the message used to contain the module name: + // "java.base/java.lang.String cannot be cast..." + int moduleSeparatorIndex = message.indexOf('/'); + if (moduleSeparatorIndex != -1 && message.startsWith(argumentType.getName(), + moduleSeparatorIndex + 1)) { + return true; + } if (CLASS_GET_MODULE != null) { Object module = ReflectionUtils.invokeMethod(CLASS_GET_MODULE, argumentType);