Skip to content

Commit 071d6a2

Browse files
committed
Introduce hints for finding hints by conventions
Closes gh-30491
1 parent d7d0292 commit 071d6a2

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

spring-beans/spring-beans.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies {
1212
testImplementation(testFixtures(project(":spring-core")))
1313
testImplementation(project(":spring-core-test"))
1414
testImplementation("jakarta.annotation:jakarta.annotation-api")
15+
testImplementation(project(":spring-web"))
1516
testFixturesApi("org.junit.jupiter:junit-jupiter-api")
1617
testFixturesImplementation("org.assertj:assertj-core")
1718
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.beans;
18+
19+
import org.springframework.aot.hint.MemberCategory;
20+
import org.springframework.aot.hint.RuntimeHints;
21+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
22+
import org.springframework.lang.Nullable;
23+
24+
/**
25+
* {@link RuntimeHintsRegistrar} to register hints for popular conventions in
26+
* {@link BeanUtils#findEditorByConvention(Class)}.
27+
*
28+
* @author Sebastien Deleuze
29+
* @since 6.0.10
30+
*/
31+
class BeanUtilsRuntimeHints implements RuntimeHintsRegistrar {
32+
33+
@Override
34+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
35+
hints.reflection().registerTypeIfPresent(classLoader, "org.springframework.http.MediaTypeEditor",
36+
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
37+
}
38+
39+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
org.springframework.aot.hint.RuntimeHintsRegistrar=\
2-
org.springframework.beans.factory.annotation.JakartaAnnotationsRuntimeHints
2+
org.springframework.beans.factory.annotation.JakartaAnnotationsRuntimeHints,\
3+
org.springframework.beans.BeanUtilsRuntimeHints
34

45
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=\
56
org.springframework.beans.factory.aot.BeanRegistrationsAotProcessor
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.beans;
18+
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.aot.hint.MemberCategory;
23+
import org.springframework.aot.hint.RuntimeHints;
24+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
25+
import org.springframework.aot.hint.TypeReference;
26+
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
27+
import org.springframework.core.io.support.SpringFactoriesLoader;
28+
import org.springframework.util.ClassUtils;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Tests for {@link BeanUtilsRuntimeHints}.
34+
*
35+
* @author Sebastien Deleuze
36+
*/
37+
class BeanUtilsRuntimeHintsTests {
38+
39+
private RuntimeHints hints;
40+
41+
@BeforeEach
42+
void setup() {
43+
this.hints = new RuntimeHints();
44+
SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories")
45+
.load(RuntimeHintsRegistrar.class).forEach(registrar -> registrar
46+
.registerHints(this.hints, ClassUtils.getDefaultClassLoader()));
47+
}
48+
49+
@Test
50+
void mediaTypeEditorHasHints() {
51+
assertThat(RuntimeHintsPredicates.reflection().onType(TypeReference.of("org.springframework.http.MediaTypeEditor"))
52+
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)