Refine Kotlin Serialization hint registration

This commit adds support for serializer methods with a parameter.

Closes gh-34979
This commit is contained in:
Sébastien Deleuze
2025-06-09 16:48:10 +02:00
parent 18d6a55e3e
commit 7bb19fcde8
2 changed files with 24 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -150,7 +150,8 @@ public class BindingReflectionHintsRegistrar {
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
if (ClassUtils.isPresent(companionClassName, null)) {
Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null);
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer");
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer",
(Class<?>[]) null);
if (serializerMethod != null) {
hints.registerMethod(serializerMethod, ExecutableMode.INVOKE);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -81,6 +81,12 @@ class BindingReflectionHintsRegistrarKotlinTests {
assertThat(RuntimeHintsPredicates.reflection().onType(SampleClass::class.java)
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS)).accepts(hints)
}
@Test
fun `Register reflection hints on serializer function with parameter`() {
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleResult::class.java)
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleResult.Companion::class.java, "serializer")).accepts(hints)
}
}
@kotlinx.serialization.Serializable
@@ -89,3 +95,17 @@ class SampleSerializableClass(val name: String)
data class SampleDataClass(val name: String, val isNonNullable: Boolean, val isNullable: Boolean?)
class SampleClass(val name: String)
@kotlinx.serialization.Serializable
data class SampleResult <T>(
val code: Int,
val message: String,
val data: T,
) {
companion object {
private const val SUCCESS: Int = 200
private const val FAILURE: Int = 500
fun <T> success(message: String, data: T) = SampleResult<T>(code = SUCCESS, message = message, data = data)
fun <T> failure(message: String, data: T) = SampleResult<T>(code = FAILURE, message = message, data = data)
}
}