Skip to content

Commit 505da5c

Browse files
committed
Migrate hint registration to shortcuts
Migrate code to make use of the `MemberCategory` and `FieldMode` shortcuts. See gh-29011
1 parent bc0bf1f commit 505da5c

File tree

17 files changed

+147
-198
lines changed

17 files changed

+147
-198
lines changed

integration-tests/src/test/java/org/springframework/aot/test/ReflectionInvocationsTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ReflectionInvocationsTests {
3333
@Test
3434
void sampleTest() {
3535
RuntimeHints hints = new RuntimeHints();
36-
hints.reflection().registerType(String.class, hint -> hint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
36+
hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
3737

3838
RuntimeHintsInvocations invocations = RuntimeHintsRecorder.record(() -> {
3939
SampleReflection sample = new SampleReflection();
@@ -45,8 +45,8 @@ void sampleTest() {
4545
@Test
4646
void multipleCallsTest() {
4747
RuntimeHints hints = new RuntimeHints();
48-
hints.reflection().registerType(String.class, hint -> hint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
49-
hints.reflection().registerType(Integer.class, hint -> hint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
48+
hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
49+
hints.reflection().registerType(Integer.class,MemberCategory.INTROSPECT_PUBLIC_METHODS);
5050
RuntimeHintsInvocations invocations = RuntimeHintsRecorder.record(() -> {
5151
SampleReflection sample = new SampleReflection();
5252
sample.multipleCalls(); // does Method[] methods = String.class.getMethods(); methods = Integer.class.getMethods();

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Map;
3737
import java.util.Set;
3838
import java.util.concurrent.ConcurrentHashMap;
39-
import java.util.function.Consumer;
4039

4140
import org.apache.commons.logging.Log;
4241
import org.apache.commons.logging.LogFactory;
@@ -47,8 +46,6 @@
4746
import org.springframework.aot.generate.GenerationContext;
4847
import org.springframework.aot.generate.MethodReference;
4948
import org.springframework.aot.hint.ExecutableMode;
50-
import org.springframework.aot.hint.FieldHint;
51-
import org.springframework.aot.hint.FieldMode;
5249
import org.springframework.aot.hint.RuntimeHints;
5350
import org.springframework.beans.BeanUtils;
5451
import org.springframework.beans.BeansException;
@@ -914,10 +911,6 @@ private static class AotContribution implements BeanRegistrationAotContribution
914911

915912
private static final String INSTANCE_PARAMETER = "instance";
916913

917-
private static final Consumer<FieldHint.Builder> ALLOW_WRITE = builder -> builder
918-
.withMode(FieldMode.WRITE);
919-
920-
921914
private final Class<?> target;
922915

923916
private final Collection<AutowiredElement> autowiredElements;
@@ -987,7 +980,7 @@ private CodeBlock generateMethodStatementForElement(
987980
private CodeBlock generateMethodStatementForField(Field field, boolean required,
988981
RuntimeHints hints) {
989982

990-
hints.reflection().registerField(field, ALLOW_WRITE);
983+
hints.reflection().registerField(field);
991984
CodeBlock resolver = CodeBlock.of("$T.$L($S)",
992985
AutowiredFieldValueResolver.class,
993986
(!required) ? "forField" : "forRequiredField", field.getName());

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBeanRuntimeHints.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.scheduling.quartz;
1818

19-
import java.util.function.Consumer;
20-
2119
import org.springframework.aot.hint.MemberCategory;
2220
import org.springframework.aot.hint.RuntimeHints;
2321
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -45,13 +43,14 @@ public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
4543
if (!ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) {
4644
return;
4745
}
48-
Consumer<Builder> typeHint = type -> type
49-
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
50-
.onReachableType(SchedulerFactoryBean.class);
5146
hints.reflection()
52-
.registerType(TypeReference.of(SCHEDULER_FACTORY_CLASS_NAME), typeHint)
47+
.registerType(TypeReference.of(SCHEDULER_FACTORY_CLASS_NAME), this::typeHint)
5348
.registerTypes(TypeReference.listOf(ResourceLoaderClassLoadHelper.class,
54-
LocalTaskExecutorThreadPool.class, LocalDataSourceJobStore.class), typeHint);
49+
LocalTaskExecutorThreadPool.class, LocalDataSourceJobStore.class), this::typeHint);
5550
this.reflectiveRegistrar.registerRuntimeHints(hints, LocalTaskExecutorThreadPool.class);
5651
}
52+
53+
private void typeHint(Builder typeHint) {
54+
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS).onReachableType(SchedulerFactoryBean.class);
55+
}
5756
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,20 @@ private void registerReflectionHints(ReflectionHints hints, Set<Type> seen, Type
8080
if (clazz.isPrimitive() || clazz == Object.class) {
8181
return;
8282
}
83-
hints.registerType(clazz, builder -> {
83+
hints.registerType(clazz, typeHint -> {
8484
if (seen.contains(type)) {
8585
return;
8686
}
8787
seen.add(type);
8888
if (shouldRegisterMembers(clazz)) {
8989
if (clazz.isRecord()) {
90-
builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
90+
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
9191
for (RecordComponent recordComponent : clazz.getRecordComponents()) {
9292
registerRecordHints(hints, seen, recordComponent.getAccessor());
9393
}
9494
}
9595
else {
96-
builder.withMembers(
96+
typeHint.withMembers(
9797
MemberCategory.DECLARED_FIELDS,
9898
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
9999
try {

0 commit comments

Comments
 (0)