From b29fdc5058d6174f72ddf09cf0dfd2fafd3c657f Mon Sep 17 00:00:00 2001 From: Tobias Gierke Date: Fri, 13 Oct 2023 18:45:09 +0200 Subject: [PATCH] Make Types#isLambdaType work on JDK >= 21 --- .../com/thoughtworks/xstream/core/util/Types.java | 12 +++++++++--- .../test/com/thoughtworks/acceptance/LambdaTest.java | 6 ++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/xstream/src/java/com/thoughtworks/xstream/core/util/Types.java b/xstream/src/java/com/thoughtworks/xstream/core/util/Types.java index 5168be7a3..9ea639f37 100644 --- a/xstream/src/java/com/thoughtworks/xstream/core/util/Types.java +++ b/xstream/src/java/com/thoughtworks/xstream/core/util/Types.java @@ -16,10 +16,16 @@ * @since 1.4.8 */ public class Types { - private static final Pattern lambdaPattern = Pattern.compile(".*\\$\\$Lambda\\$[0-9]+/.*"); + private static final Pattern lambdaPattern = Pattern.compile(".*\\$\\$Lambda(?:\\$[0-9]+|)/.*"); public static final boolean isLambdaType(final Class type) { - return type != null && type.isSynthetic() && lambdaPattern.matcher(type.getSimpleName()).matches(); + if (type != null && type.isSynthetic()) { + String typeName = type.getSimpleName(); + if (typeName.length() == 0) { // JDK >= 17: JDK-8254979 makes getSimpleName() return "" for lambdas + typeName = type.getName(); + } + return lambdaPattern.matcher(typeName).matches(); + } + return false; } - } diff --git a/xstream/src/test/com/thoughtworks/acceptance/LambdaTest.java b/xstream/src/test/com/thoughtworks/acceptance/LambdaTest.java index e0c6c8024..01d2b8eda 100644 --- a/xstream/src/test/com/thoughtworks/acceptance/LambdaTest.java +++ b/xstream/src/test/com/thoughtworks/acceptance/LambdaTest.java @@ -199,8 +199,10 @@ private void assertBothWaysNormalized(final Object original, final String expect } private String normalizeLambda(final String xml) { - // unify compiler specific name for implMethodName, Eclipse uses always "lambda$0" - return xml.replaceAll(">lambda\\$[^<]+<", ">lambda\\$0<"); + // unify compiler specific name for implMethodName (Eclipse uses always "lambda$0") + // and implMethodKind (varies between at least JDK17 and JDK21 in some cases) + return xml.replaceAll( ">lambda\\$[^<]+<", ">lambda\\$0<" ) + .replaceAll( "\\d+", "5" ); } public void testTreeSetWithLambda() {