Browse Source

Refine Kotlin Serialization hint registration

This commit adds support for serializer methods with a parameter.

Closes gh-34979
pull/35405/head
Sébastien Deleuze 6 months ago
parent
commit
7bb19fcde8
  1. 5
      spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java
  2. 22
      spring-core/src/test/kotlin/org/springframework/aot/hint/BindingReflectionHintsRegistrarKotlinTests.kt

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

@ -1,5 +1,5 @@ @@ -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 { @@ -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);
}

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

@ -1,5 +1,5 @@ @@ -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 { @@ -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) @@ -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)
}
}

Loading…
Cancel
Save