Skip to content

Commit 52a75c7

Browse files
committed
fix Lambada BiFunction error
1 parent 20c0c8d commit 52a75c7

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

core/src/main/java/com/alibaba/fastjson2/support/LambdaMiscCodec.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ public static BiFunction createBiFunction(Method method) {
529529
method.getName(),
530530
MethodType.methodType(objectClass, param0)
531531
);
532-
methodType = MethodType.methodType(objectClass, declaringClass, param0);
532+
methodType = MethodType.methodType(objectClass, declaringClass, box(param0));
533533
}
534534

535535
CallSite callSite = LambdaMetafactory.metafactory(
@@ -567,7 +567,7 @@ public static BiFunction createBiFunction(Constructor constructor) {
567567
METHOD_TYPE_BI_FUNCTION,
568568
METHOD_TYPE_OBJECT_OBJECT_OBJECT,
569569
methodHandle,
570-
MethodType.methodType(declaringClass, param0, param1)
570+
MethodType.methodType(declaringClass, box(param0), box(param1))
571571
);
572572
return (BiFunction) callSite.getTarget().invokeExact();
573573
} catch (Throwable ignored) {
@@ -577,6 +577,34 @@ public static BiFunction createBiFunction(Constructor constructor) {
577577
return new ConstructorBiFunction(constructor);
578578
}
579579

580+
static Class<?> box(Class cls) {
581+
if (cls == int.class) {
582+
return Integer.class;
583+
}
584+
if (cls == long.class) {
585+
return Long.class;
586+
}
587+
if (cls == boolean.class) {
588+
return Boolean.class;
589+
}
590+
if (cls == short.class) {
591+
return Short.class;
592+
}
593+
if (cls == byte.class) {
594+
return Byte.class;
595+
}
596+
if (cls == char.class) {
597+
return Character.class;
598+
}
599+
if (cls == float.class) {
600+
return Float.class;
601+
}
602+
if (cls == double.class) {
603+
return Double.class;
604+
}
605+
return cls;
606+
}
607+
580608
static final class ConstructorSupplier
581609
implements Supplier {
582610
final Constructor constructor;

test-jdk17/src/test/java/com/alibaba/fastjson2/RecordTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,21 @@ public record User(boolean default_profile, String screen_name) {
6363

6464
public record Item3(User user) implements Serializable {
6565
}
66+
67+
@Test
68+
public void test4() {
69+
Item4 item = new Item4(123);
70+
String str = JSON.toJSONString(item);
71+
String str1 = JSON.toJSONString(item, JSONWriter.Feature.FieldBased);
72+
assertEquals(str, str1);
73+
74+
JSONObject object = JSON.parseObject(str);
75+
assertEquals(item.value, object.get("value"));
76+
77+
Item4 item2 = JSON.parseObject(str, Item4.class);
78+
assertEquals(item.value, item2.value);
79+
}
80+
81+
private record Item4(int value) {
82+
}
6683
}

0 commit comments

Comments
 (0)