Skip to content

Commit ca2b3c1

Browse files
committed
Add Kotlin hints for enclosing class
This is needed by Kotlin reflection in order to be able to list class members on native. Closes gh-32472
1 parent 019ce44 commit ca2b3c1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

spring-context/src/main/java/org/springframework/context/aot/KotlinReflectionBeanRegistrationAotProcessor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -66,6 +66,10 @@ private void registerHints(Class<?> type, RuntimeHints runtimeHints) {
6666
if (superClass != null) {
6767
registerHints(superClass, runtimeHints);
6868
}
69+
Class<?> enclosingClass = type.getEnclosingClass();
70+
if (enclosingClass != null) {
71+
runtimeHints.reflection().registerType(enclosingClass);
72+
}
6973
}
7074
}
7175

spring-context/src/test/kotlin/org/springframework/context/aot/KotlinReflectionBeanRegistrationAotProcessorTests.kt

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -66,6 +66,17 @@ class KotlinReflectionBeanRegistrationAotProcessorTests {
6666
assertThat(generationContext.runtimeHints.reflection().typeHints()).isEmpty()
6767
}
6868

69+
@Test
70+
fun shouldGenerateOuterClassHints() {
71+
process(OuterBean.NestedBean::class.java)
72+
assertThat(
73+
RuntimeHintsPredicates.reflection()
74+
.onType(OuterBean.NestedBean::class.java)
75+
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS)
76+
.and(RuntimeHintsPredicates.reflection().onType(OuterBean::class.java))
77+
).accepts(generationContext.runtimeHints)
78+
}
79+
6980
private fun process(beanClass: Class<*>) {
7081
createContribution(beanClass)?.applyTo(generationContext, Mockito.mock(BeanRegistrationCode::class.java))
7182
}
@@ -87,4 +98,8 @@ class KotlinReflectionBeanRegistrationAotProcessorTests {
8798
}
8899
}
89100

101+
class OuterBean {
102+
class NestedBean
103+
}
104+
90105
}

0 commit comments

Comments
 (0)