From 0e539429c9a337ccf63305e1a90ab8bb321e3193 Mon Sep 17 00:00:00 2001 From: Donghyeon Kim Date: Fri, 4 Aug 2023 22:05:32 +0900 Subject: [PATCH 1/3] Use kotlin jvm inline class synthetic constructors --- .../jackson/databind/ext/KotlinSupport.java | 15 +++++++++++++++ .../introspect/AnnotatedCreatorCollector.java | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/fasterxml/jackson/databind/ext/KotlinSupport.java diff --git a/src/main/java/com/fasterxml/jackson/databind/ext/KotlinSupport.java b/src/main/java/com/fasterxml/jackson/databind/ext/KotlinSupport.java new file mode 100644 index 0000000000..73e428511a --- /dev/null +++ b/src/main/java/com/fasterxml/jackson/databind/ext/KotlinSupport.java @@ -0,0 +1,15 @@ +package com.fasterxml.jackson.databind.ext; + +import java.lang.reflect.Constructor; + +public class KotlinSupport { + public static boolean isJvmInlineClassSyntheticConstructor(Constructor ctor) { + Class[] params = ctor.getParameterTypes(); + if (params.length == 0) { + return false; + } + + Class lastParam = params[params.length - 1]; + return ctor.isSynthetic() && lastParam.getName().equals("kotlin.jvm.internal.DefaultConstructorMarker"); + } +} diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java index b5c3f39d47..97ed3729d1 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java @@ -11,6 +11,7 @@ import com.fasterxml.jackson.databind.AnnotationIntrospector; import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ext.KotlinSupport; import com.fasterxml.jackson.databind.introspect.AnnotatedClass.Creators; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.util.ClassUtil; @@ -406,6 +407,6 @@ private final AnnotationMap collectAnnotations(AnnotatedElement main, AnnotatedE // for [databind#1005]: do not use or expose synthetic constructors private static boolean isIncludableConstructor(Constructor c) { - return !c.isSynthetic(); + return !c.isSynthetic() || KotlinSupport.isJvmInlineClassSyntheticConstructor(c); } } From 02f3fd29880a394ef9d61cbd31face4a8d14dc2c Mon Sep 17 00:00:00 2001 From: Donghyeon Kim Date: Fri, 4 Aug 2023 22:21:09 +0900 Subject: [PATCH 2/3] Use box-impl function as factory for kotlin jvm inline class --- .../com/fasterxml/jackson/databind/ext/KotlinSupport.java | 8 ++++++++ .../databind/introspect/AnnotatedCreatorCollector.java | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/main/java/com/fasterxml/jackson/databind/ext/KotlinSupport.java b/src/main/java/com/fasterxml/jackson/databind/ext/KotlinSupport.java index 73e428511a..c4349e51a1 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ext/KotlinSupport.java +++ b/src/main/java/com/fasterxml/jackson/databind/ext/KotlinSupport.java @@ -1,6 +1,8 @@ package com.fasterxml.jackson.databind.ext; import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; public class KotlinSupport { public static boolean isJvmInlineClassSyntheticConstructor(Constructor ctor) { @@ -12,4 +14,10 @@ public static boolean isJvmInlineClassSyntheticConstructor(Constructor ctor) Class lastParam = params[params.length - 1]; return ctor.isSynthetic() && lastParam.getName().equals("kotlin.jvm.internal.DefaultConstructorMarker"); } + + public static boolean isJvmInlineClassSyntheticBoxingFunction(Method method) { + return Modifier.isStatic(method.getModifiers()) + && method.isSynthetic() + && method.getName().equals("box-impl"); + } } diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java index 97ed3729d1..5dbf3c1545 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java @@ -278,6 +278,10 @@ private List _findPotentialFactories(TypeFactory typeFactory, private static boolean _isIncludableFactoryMethod(Method m) { + if (KotlinSupport.isJvmInlineClassSyntheticBoxingFunction(m)) { + return true; + } + return Modifier.isStatic(m.getModifiers()) // 09-Nov-2020, ckozak: Avoid considering synthetic methods such as // lambdas used within methods because they're not relevant. From ff989c461e23ff87b59afc33ae09917dfd04ce5e Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 2 Sep 2023 10:31:57 -0700 Subject: [PATCH 3/3] Add more comments --- .../databind/introspect/AnnotatedCreatorCollector.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java index 8ef569b055..25a0b9ff70 100644 --- a/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java +++ b/src/main/java/com/fasterxml/jackson/databind/introspect/AnnotatedCreatorCollector.java @@ -409,8 +409,10 @@ private final AnnotationMap collectAnnotations(AnnotatedElement main, AnnotatedE return c.asAnnotationMap(); } - // for [databind#1005]: do not use or expose synthetic constructors private static boolean isIncludableConstructor(Constructor c) { - return !c.isSynthetic() || KotlinSupport.isJvmInlineClassSyntheticConstructor(c); + // for [databind#1005]: do not use or expose synthetic constructors + return !c.isSynthetic() + // 02-Sep-2023: As per [databind#4066] Kotlin needs some synthetic constructors + || KotlinSupport.isJvmInlineClassSyntheticConstructor(c); } }