Skip to content

Commit 7bb19fc

Browse files
committed
Refine Kotlin Serialization hint registration
This commit adds support for serializer methods with a parameter. Closes gh-34979
1 parent 18d6a55 commit 7bb19fc

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -150,7 +150,8 @@ private void registerKotlinSerializationHints(ReflectionHints hints, Class<?> cl
150150
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
151151
if (ClassUtils.isPresent(companionClassName, null)) {
152152
Class<?> companionClass = ClassUtils.resolveClassName(companionClassName, null);
153-
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer");
153+
Method serializerMethod = ClassUtils.getMethodIfAvailable(companionClass, "serializer",
154+
(Class<?>[]) null);
154155
if (serializerMethod != null) {
155156
hints.registerMethod(serializerMethod, ExecutableMode.INVOKE);
156157
}

spring-core/src/test/kotlin/org/springframework/aot/hint/BindingReflectionHintsRegistrarKotlinTests.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,6 +81,12 @@ class BindingReflectionHintsRegistrarKotlinTests {
8181
assertThat(RuntimeHintsPredicates.reflection().onType(SampleClass::class.java)
8282
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS)).accepts(hints)
8383
}
84+
85+
@Test
86+
fun `Register reflection hints on serializer function with parameter`() {
87+
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleResult::class.java)
88+
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleResult.Companion::class.java, "serializer")).accepts(hints)
89+
}
8490
}
8591

8692
@kotlinx.serialization.Serializable
@@ -89,3 +95,17 @@ class SampleSerializableClass(val name: String)
8995
data class SampleDataClass(val name: String, val isNonNullable: Boolean, val isNullable: Boolean?)
9096

9197
class SampleClass(val name: String)
98+
99+
@kotlinx.serialization.Serializable
100+
data class SampleResult <T>(
101+
val code: Int,
102+
val message: String,
103+
val data: T,
104+
) {
105+
companion object {
106+
private const val SUCCESS: Int = 200
107+
private const val FAILURE: Int = 500
108+
fun <T> success(message: String, data: T) = SampleResult<T>(code = SUCCESS, message = message, data = data)
109+
fun <T> failure(message: String, data: T) = SampleResult<T>(code = FAILURE, message = message, data = data)
110+
}
111+
}

0 commit comments

Comments
 (0)