Refine Kotlin Serialization hint registration
This commit adds support for serializer methods with a parameter. Closes gh-34979
This commit is contained in:
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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;
|
String companionClassName = clazz.getCanonicalName() + KOTLIN_COMPANION_SUFFIX;
|
||||||
if (ClassUtils.isPresent(companionClassName, null)) {
|
if (ClassUtils.isPresent(companionClassName, null)) {
|
||||||
Class<?> companionClass = ClassUtils.resolveClassName(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) {
|
if (serializerMethod != null) {
|
||||||
hints.registerMethod(serializerMethod, ExecutableMode.INVOKE);
|
hints.registerMethod(serializerMethod, ExecutableMode.INVOKE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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)
|
assertThat(RuntimeHintsPredicates.reflection().onType(SampleClass::class.java)
|
||||||
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS)).accepts(hints)
|
.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
|
@kotlinx.serialization.Serializable
|
||||||
@@ -89,3 +95,17 @@ class SampleSerializableClass(val name: String)
|
|||||||
data class SampleDataClass(val name: String, val isNonNullable: Boolean, val isNullable: Boolean?)
|
data class SampleDataClass(val name: String, val isNonNullable: Boolean, val isNullable: Boolean?)
|
||||||
|
|
||||||
class SampleClass(val name: String)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user