Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Types#isLambdaType work on JDK >= 21 #349

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions xstream/src/java/com/thoughtworks/xstream/core/util/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah the "fix" for https://bugs.openjdk.org/browse/JDK-8254979 was just change in documentation. So I think we don't need to handle Class.isSimpleName() returning empty then.

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;
}

}
6 changes: 4 additions & 2 deletions xstream/src/test/com/thoughtworks/acceptance/LambdaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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( "<implMethodKind>\\d+</implMethodKind>", "<implMethodKind>5</implMethodKind>" );
}

public void testTreeSetWithLambda() {
Expand Down