Commit 0ca8f108 authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent 29599582
...@@ -188,9 +188,22 @@ public final class LambdaSafe { ...@@ -188,9 +188,22 @@ public final class LambdaSafe {
return false; return false;
} }
Class<? extends Object> argumentType = argument.getClass(); Class<? extends Object> argumentType = argument.getClass();
// On Java 8, the message starts with the class name: "java.lang.String cannot
// be cast..."
if (message.startsWith(argumentType.getName())) { if (message.startsWith(argumentType.getName())) {
return true; 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) { if (CLASS_GET_MODULE != null) {
Object module = ReflectionUtils.invokeMethod(CLASS_GET_MODULE, Object module = ReflectionUtils.invokeMethod(CLASS_GET_MODULE,
argumentType); argumentType);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment