Add Kotlin data class components support to BindingReflectionHintsRegistrar

Closes gh-29316
This commit is contained in:
Sébastien Deleuze
2022-10-17 12:26:45 +02:00
parent 581fa1419f
commit 69cb68f7d6
2 changed files with 33 additions and 1 deletions

View File

@@ -22,6 +22,8 @@ import java.lang.reflect.Type;
import java.util.LinkedHashSet;
import java.util.Set;
import kotlin.jvm.JvmClassMappingKt;
import kotlin.reflect.KClass;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -101,6 +103,7 @@ public class BindingReflectionHintsRegistrar {
}
}
if (KotlinDetector.isKotlinType(clazz)) {
KotlinDelegate.registerComponentHints(hints, clazz);
registerKotlinSerializationHints(hints, clazz);
}
});
@@ -148,4 +151,22 @@ public class BindingReflectionHintsRegistrar {
}
}
/**
* Inner class to avoid a hard dependency on Kotlin at runtime.
*/
private static class KotlinDelegate {
public static void registerComponentHints(ReflectionHints hints, Class<?> type) {
KClass<?> kClass = JvmClassMappingKt.getKotlinClass(type);
if (kClass.isData()) {
for (Method method : type.getMethods()) {
String methodName = method.getName();
if (methodName.startsWith("component") || methodName.equals("copy")) {
hints.registerMethod(method, ExecutableMode.INVOKE);
}
}
}
}
}
}