Skip to content

Commit

Permalink
Refine BindingReflectionHintsRegistrar Kotlin support
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze authored and mdeinum committed Jun 29, 2023
1 parent 06dfb82 commit ff388c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -111,6 +111,8 @@ else if ((methodName.startsWith("get") && method.getParameterCount() == 0 && met
if (KotlinDetector.isKotlinType(clazz)) {
KotlinDelegate.registerComponentHints(hints, clazz);
registerKotlinSerializationHints(hints, clazz);
// For Kotlin reflection
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS);
}
});
}
Expand Down Expand Up @@ -192,7 +194,7 @@ public static void registerComponentHints(ReflectionHints hints, Class<?> type)
if (kClass.isData()) {
for (Method method : type.getMethods()) {
String methodName = method.getName();
if (methodName.startsWith("component") || methodName.equals("copy")) {
if (methodName.startsWith("component") || methodName.equals("copy") || methodName.equals("copy$default")) {
hints.registerMethod(method, ExecutableMode.INVOKE);
}
}
Expand Down
Expand Up @@ -20,6 +20,7 @@ import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.ThrowingConsumer
import org.junit.jupiter.api.Test
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates
import java.lang.reflect.Method

/**
* Tests for Kotlin support in [BindingReflectionHintsRegistrar].
Expand Down Expand Up @@ -67,10 +68,22 @@ class KotlinBindingReflectionHintsRegistrarTests {
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleDataClass::class.java, "component1")).accepts(hints)
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleDataClass::class.java, "copy")).accepts(hints)
assertThat(RuntimeHintsPredicates.reflection().onMethod(SampleDataClass::class.java, "getName")).accepts(hints)
val copyDefault: Method = SampleDataClass::class.java.getMethod("copy\$default", SampleDataClass::class.java,
String::class.java , Int::class.java, Object::class.java)
assertThat(RuntimeHintsPredicates.reflection().onMethod(copyDefault)).accepts(hints)
}

@Test
fun `Register reflection hints on declared methods for Kotlin class`() {
bindingRegistrar.registerReflectionHints(hints.reflection(), SampleClass::class.java)
assertThat(RuntimeHintsPredicates.reflection().onType(SampleClass::class.java)
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS)).accepts(hints)
}
}

@kotlinx.serialization.Serializable
class SampleSerializableClass(val name: String)

data class SampleDataClass(val name: String)

class SampleClass(val name: String)

0 comments on commit ff388c5

Please sign in to comment.