Skip to content

Commit 4c343ef

Browse files
committed
Add missing reflection hints on Kotlin classes
This commit adds the relevant reflection hints required by `KotlinDetector`; this class is trying to detect both `kotlin.Metadata` and `kotlin.reflect.full.KClasses`. Fixes gh- 31269
1 parent a1f4cdf commit 4c343ef

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aot.hint.support;
18+
19+
import org.springframework.aot.hint.RuntimeHints;
20+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
21+
import org.springframework.aot.hint.TypeReference;
22+
23+
/**
24+
* {@link RuntimeHintsRegistrar} to register hints for {@link org.springframework.core.KotlinDetector}.
25+
*
26+
* @author Brian Clozel
27+
* @since 6.1
28+
*/
29+
class KotlinDetectorRuntimeHints implements RuntimeHintsRegistrar {
30+
31+
@Override
32+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
33+
hints.reflection().registerType(TypeReference.of("kotlin.Metadata"))
34+
.registerType(TypeReference.of("kotlin.reflect.full.KClasses"));
35+
}
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2+
org.springframework.aot.hint.support.KotlinDetectorRuntimeHints,\
23
org.springframework.aot.hint.support.ObjectToObjectConverterRuntimeHints,\
34
org.springframework.aot.hint.support.SpringFactoriesLoaderRuntimeHints
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aot.hint.support;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.aot.hint.RuntimeHints;
23+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
24+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
25+
import org.springframework.core.io.support.SpringFactoriesLoader;
26+
import org.springframework.util.ClassUtils;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link KotlinDetectorRuntimeHints}.
32+
* @author Brian Clozel
33+
*/
34+
class KotlinDetectorRuntimeHintsTests {
35+
36+
private RuntimeHints hints;
37+
38+
@BeforeEach
39+
void setup() {
40+
this.hints = new RuntimeHints();
41+
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
42+
.load(RuntimeHintsRegistrar.class).forEach(registrar -> registrar
43+
.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
44+
}
45+
46+
@Test
47+
void kotlinMetadataHasHints() {
48+
assertThat(RuntimeHintsPredicates.reflection().onType(kotlin.Metadata.class)).accepts(this.hints);
49+
}
50+
51+
@Test
52+
void kotlinReflectHasHints() {
53+
assertThat(RuntimeHintsPredicates.reflection().onType(kotlin.reflect.full.KClasses.class)).accepts(this.hints);
54+
}
55+
56+
}

0 commit comments

Comments
 (0)