Migrate hint registration to shortcuts

Migrate code to make use of the `MemberCategory` and `FieldMode`
shortcuts.

See gh-29011
This commit is contained in:
Phillip Webb 2022-09-01 16:30:25 -07:00
parent bc0bf1fac3
commit 505da5c602
17 changed files with 147 additions and 198 deletions

View File

@ -33,7 +33,7 @@ class ReflectionInvocationsTests {
@Test @Test
void sampleTest() { void sampleTest() {
RuntimeHints hints = new RuntimeHints(); RuntimeHints hints = new RuntimeHints();
hints.reflection().registerType(String.class, hint -> hint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
RuntimeHintsInvocations invocations = RuntimeHintsRecorder.record(() -> { RuntimeHintsInvocations invocations = RuntimeHintsRecorder.record(() -> {
SampleReflection sample = new SampleReflection(); SampleReflection sample = new SampleReflection();
@ -45,8 +45,8 @@ class ReflectionInvocationsTests {
@Test @Test
void multipleCallsTest() { void multipleCallsTest() {
RuntimeHints hints = new RuntimeHints(); RuntimeHints hints = new RuntimeHints();
hints.reflection().registerType(String.class, hint -> hint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
hints.reflection().registerType(Integer.class, hint -> hint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(Integer.class,MemberCategory.INTROSPECT_PUBLIC_METHODS);
RuntimeHintsInvocations invocations = RuntimeHintsRecorder.record(() -> { RuntimeHintsInvocations invocations = RuntimeHintsRecorder.record(() -> {
SampleReflection sample = new SampleReflection(); SampleReflection sample = new SampleReflection();
sample.multipleCalls(); // does Method[] methods = String.class.getMethods(); methods = Integer.class.getMethods(); sample.multipleCalls(); // does Method[] methods = String.class.getMethods(); methods = Integer.class.getMethods();

View File

@ -36,7 +36,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -47,8 +46,6 @@ import org.springframework.aot.generate.GeneratedMethod;
import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodReference; import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.hint.ExecutableMode; import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.FieldHint;
import org.springframework.aot.hint.FieldMode;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
@ -914,10 +911,6 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
private static final String INSTANCE_PARAMETER = "instance"; private static final String INSTANCE_PARAMETER = "instance";
private static final Consumer<FieldHint.Builder> ALLOW_WRITE = builder -> builder
.withMode(FieldMode.WRITE);
private final Class<?> target; private final Class<?> target;
private final Collection<AutowiredElement> autowiredElements; private final Collection<AutowiredElement> autowiredElements;
@ -987,7 +980,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
private CodeBlock generateMethodStatementForField(Field field, boolean required, private CodeBlock generateMethodStatementForField(Field field, boolean required,
RuntimeHints hints) { RuntimeHints hints) {
hints.reflection().registerField(field, ALLOW_WRITE); hints.reflection().registerField(field);
CodeBlock resolver = CodeBlock.of("$T.$L($S)", CodeBlock resolver = CodeBlock.of("$T.$L($S)",
AutowiredFieldValueResolver.class, AutowiredFieldValueResolver.class,
(!required) ? "forField" : "forRequiredField", field.getName()); (!required) ? "forField" : "forRequiredField", field.getName());

View File

@ -16,8 +16,6 @@
package org.springframework.scheduling.quartz; package org.springframework.scheduling.quartz;
import java.util.function.Consumer;
import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.RuntimeHintsRegistrar;
@ -45,13 +43,14 @@ class SchedulerFactoryBeanRuntimeHints implements RuntimeHintsRegistrar {
if (!ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) { if (!ClassUtils.isPresent(SCHEDULER_FACTORY_CLASS_NAME, classLoader)) {
return; return;
} }
Consumer<Builder> typeHint = type -> type
.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)
.onReachableType(SchedulerFactoryBean.class);
hints.reflection() hints.reflection()
.registerType(TypeReference.of(SCHEDULER_FACTORY_CLASS_NAME), typeHint) .registerType(TypeReference.of(SCHEDULER_FACTORY_CLASS_NAME), this::typeHint)
.registerTypes(TypeReference.listOf(ResourceLoaderClassLoadHelper.class, .registerTypes(TypeReference.listOf(ResourceLoaderClassLoadHelper.class,
LocalTaskExecutorThreadPool.class, LocalDataSourceJobStore.class), typeHint); LocalTaskExecutorThreadPool.class, LocalDataSourceJobStore.class), this::typeHint);
this.reflectiveRegistrar.registerRuntimeHints(hints, LocalTaskExecutorThreadPool.class); this.reflectiveRegistrar.registerRuntimeHints(hints, LocalTaskExecutorThreadPool.class);
} }
private void typeHint(Builder typeHint) {
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS).onReachableType(SchedulerFactoryBean.class);
}
} }

View File

@ -80,20 +80,20 @@ public class BindingReflectionHintsRegistrar {
if (clazz.isPrimitive() || clazz == Object.class) { if (clazz.isPrimitive() || clazz == Object.class) {
return; return;
} }
hints.registerType(clazz, builder -> { hints.registerType(clazz, typeHint -> {
if (seen.contains(type)) { if (seen.contains(type)) {
return; return;
} }
seen.add(type); seen.add(type);
if (shouldRegisterMembers(clazz)) { if (shouldRegisterMembers(clazz)) {
if (clazz.isRecord()) { if (clazz.isRecord()) {
builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
for (RecordComponent recordComponent : clazz.getRecordComponents()) { for (RecordComponent recordComponent : clazz.getRecordComponents()) {
registerRecordHints(hints, seen, recordComponent.getAccessor()); registerRecordHints(hints, seen, recordComponent.getAccessor());
} }
} }
else { else {
builder.withMembers( typeHint.withMembers(
MemberCategory.DECLARED_FIELDS, MemberCategory.DECLARED_FIELDS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
try { try {

View File

@ -55,39 +55,37 @@ class InstrumentedMethodTests {
void classForNameShouldMatchReflectionOnType() { void classForNameShouldMatchReflectionOnType() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_FORNAME) RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_FORNAME)
.withArgument("java.lang.String").returnValue(String.class).build(); .withArgument("java.lang.String").returnValue(String.class).build();
hints.reflection().registerType(String.class, typeHint -> { hints.reflection().registerType(String.class);
});
assertThatInvocationMatches(InstrumentedMethod.CLASS_FORNAME, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_FORNAME, invocation);
} }
@Test @Test
void classGetClassesShouldNotMatchReflectionOnType() { void classGetClassesShouldNotMatchReflectionOnType() {
hints.reflection().registerType(String.class, typeHint -> { hints.reflection().registerType(String.class);
});
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETCLASSES, this.stringGetClasses); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETCLASSES, this.stringGetClasses);
} }
@Test @Test
void classGetClassesShouldMatchPublicClasses() { void classGetClassesShouldMatchPublicClasses() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_CLASSES)); hints.reflection().registerType(String.class, MemberCategory.PUBLIC_CLASSES);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCLASSES, this.stringGetClasses); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCLASSES, this.stringGetClasses);
} }
@Test @Test
void classGetClassesShouldMatchDeclaredClasses() { void classGetClassesShouldMatchDeclaredClasses() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_CLASSES)); hints.reflection().registerType(String.class, MemberCategory.DECLARED_CLASSES);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCLASSES, this.stringGetClasses); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCLASSES, this.stringGetClasses);
} }
@Test @Test
void classGetDeclaredClassesShouldMatchDeclaredClassesHint() { void classGetDeclaredClassesShouldMatchDeclaredClassesHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_CLASSES)); hints.reflection().registerType(String.class, MemberCategory.DECLARED_CLASSES);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCLASSES, this.stringGetDeclaredClasses); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCLASSES, this.stringGetDeclaredClasses);
} }
@Test @Test
void classGetDeclaredClassesShouldNotMatchPublicClassesHint() { void classGetDeclaredClassesShouldNotMatchPublicClassesHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_CLASSES)); hints.reflection().registerType(String.class, MemberCategory.PUBLIC_CLASSES);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDCLASSES, this.stringGetDeclaredClasses); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDCLASSES, this.stringGetDeclaredClasses);
} }
@ -96,8 +94,7 @@ class InstrumentedMethodTests {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASSLOADER_LOADCLASS) RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASSLOADER_LOADCLASS)
.onInstance(ClassLoader.getSystemClassLoader()) .onInstance(ClassLoader.getSystemClassLoader())
.withArgument(PublicField.class.getCanonicalName()).returnValue(PublicField.class).build(); .withArgument(PublicField.class.getCanonicalName()).returnValue(PublicField.class).build();
hints.reflection().registerType(PublicField.class, builder -> { hints.reflection().registerType(PublicField.class);
});
assertThatInvocationMatches(InstrumentedMethod.CLASSLOADER_LOADCLASS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASSLOADER_LOADCLASS, invocation);
} }
@ -127,25 +124,25 @@ class InstrumentedMethodTests {
@Test @Test
void classGetConstructorShouldMatchInstrospectPublicConstructorsHint() { void classGetConstructorShouldMatchInstrospectPublicConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
} }
@Test @Test
void classGetConstructorShouldMatchInvokePublicConstructorsHint() { void classGetConstructorShouldMatchInvokePublicConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
} }
@Test @Test
void classGetConstructorShouldMatchIntrospectDeclaredConstructorsHint() { void classGetConstructorShouldMatchIntrospectDeclaredConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
} }
@Test @Test
void classGetConstructorShouldMatchInvokeDeclaredConstructorsHint() { void classGetConstructorShouldMatchInvokeDeclaredConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
} }
@ -159,43 +156,43 @@ class InstrumentedMethodTests {
@Test @Test
void classGetConstructorShouldMatchInvokeConstructorHint() { void classGetConstructorShouldMatchInvokeConstructorHint() {
hints.reflection().registerType(String.class, typeHint -> hints.reflection().registerType(String.class, typeHint ->
typeHint.withConstructor(Collections.emptyList(),ExecutableMode.INVOKE)); typeHint.withConstructor(Collections.emptyList(), ExecutableMode.INVOKE));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
} }
@Test @Test
void classGetConstructorsShouldMatchIntrospectPublicConstructorsHint() { void classGetConstructorsShouldMatchIntrospectPublicConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTORS, this.stringGetConstructors); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTORS, this.stringGetConstructors);
} }
@Test @Test
void classGetConstructorsShouldMatchInvokePublicConstructorsHint() { void classGetConstructorsShouldMatchInvokePublicConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTORS, this.stringGetConstructors); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTORS, this.stringGetConstructors);
} }
@Test @Test
void classGetConstructorsShouldMatchIntrospectDeclaredConstructorsHint() { void classGetConstructorsShouldMatchIntrospectDeclaredConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTORS, this.stringGetConstructors); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTORS, this.stringGetConstructors);
} }
@Test @Test
void classGetConstructorsShouldMatchInvokeDeclaredConstructorsHint() { void classGetConstructorsShouldMatchInvokeDeclaredConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTORS, this.stringGetConstructors); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTORS, this.stringGetConstructors);
} }
@Test @Test
void classGetDeclaredConstructorShouldMatchIntrospectDeclaredConstructorsHint() { void classGetDeclaredConstructorShouldMatchIntrospectDeclaredConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor);
} }
@Test @Test
void classGetDeclaredConstructorShouldNotMatchIntrospectPublicConstructorsHint() { void classGetDeclaredConstructorShouldNotMatchIntrospectPublicConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor);
} }
@ -215,19 +212,19 @@ class InstrumentedMethodTests {
@Test @Test
void classGetDeclaredConstructorsShouldMatchIntrospectDeclaredConstructorsHint() { void classGetDeclaredConstructorsShouldMatchIntrospectDeclaredConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTORS, this.stringGetDeclaredConstructors); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTORS, this.stringGetDeclaredConstructors);
} }
@Test @Test
void classGetDeclaredConstructorsShouldMatchInvokeDeclaredConstructorsHint() { void classGetDeclaredConstructorsShouldMatchInvokeDeclaredConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTORS, this.stringGetDeclaredConstructors); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTORS, this.stringGetDeclaredConstructors);
} }
@Test @Test
void classGetDeclaredConstructorsShouldNotMatchIntrospectPublicConstructorsHint() { void classGetDeclaredConstructorsShouldNotMatchIntrospectPublicConstructorsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTORS, this.stringGetDeclaredConstructors); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTORS, this.stringGetDeclaredConstructors);
} }
@ -271,13 +268,13 @@ class InstrumentedMethodTests {
@Test @Test
void classGetDeclaredMethodShouldMatchIntrospectDeclaredMethodsHint() { void classGetDeclaredMethodShouldMatchIntrospectDeclaredMethodsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
} }
@Test @Test
void classGetDeclaredMethodShouldNotMatchIntrospectPublicMethodsHint() { void classGetDeclaredMethodShouldNotMatchIntrospectPublicMethodsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
} }
@ -299,79 +296,79 @@ class InstrumentedMethodTests {
@Test @Test
void classGetDeclaredMethodsShouldMatchIntrospectDeclaredMethodsHint() { void classGetDeclaredMethodsShouldMatchIntrospectDeclaredMethodsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHODS, this.stringGetScaleMethod); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHODS, this.stringGetScaleMethod);
} }
@Test @Test
void classGetDeclaredMethodsShouldMatchInvokeDeclaredMethodsHint() { void classGetDeclaredMethodsShouldMatchInvokeDeclaredMethodsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDMETHODS).onInstance(String.class).build(); RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDMETHODS).onInstance(String.class).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHODS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHODS, invocation);
} }
@Test @Test
void classGetDeclaredMethodsShouldMatchIntrospectPublicMethodsHint() { void classGetDeclaredMethodsShouldMatchIntrospectPublicMethodsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDMETHODS, this.stringGetScaleMethod); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDMETHODS, this.stringGetScaleMethod);
} }
@Test @Test
void classGetMethodsShouldMatchInstrospectDeclaredMethodsHint() { void classGetMethodsShouldMatchInstrospectDeclaredMethodsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build(); RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHODS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHODS, invocation);
} }
@Test @Test
void classGetMethodsShouldMatchInstrospectPublicMethodsHint() { void classGetMethodsShouldMatchInstrospectPublicMethodsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build(); RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build();
hints.reflection().registerType(String.class, hint -> hint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHODS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHODS, invocation);
} }
@Test @Test
void classGetMethodsShouldMatchInvokeDeclaredMethodsHint() { void classGetMethodsShouldMatchInvokeDeclaredMethodsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build(); RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHODS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHODS, invocation);
} }
@Test @Test
void classGetMethodsShouldMatchInvokePublicMethodsHint() { void classGetMethodsShouldMatchInvokePublicMethodsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build(); RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build();
hints.reflection().registerType(String.class, hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_PUBLIC_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHODS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHODS, invocation);
} }
@Test @Test
void classGetMethodsShouldNotMatchForWrongType() { void classGetMethodsShouldNotMatchForWrongType() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build(); RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETMETHODS).onInstance(String.class).build();
hints.reflection().registerType(Integer.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(Integer.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETMETHODS, invocation); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETMETHODS, invocation);
} }
@Test @Test
void classGetMethodShouldMatchInstrospectPublicMethodsHint() throws NoSuchMethodException { void classGetMethodShouldMatchInstrospectPublicMethodsHint() throws NoSuchMethodException {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
} }
@Test @Test
void classGetMethodShouldMatchInvokePublicMethodsHint() throws NoSuchMethodException { void classGetMethodShouldMatchInvokePublicMethodsHint() throws NoSuchMethodException {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_PUBLIC_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
} }
@Test @Test
void classGetMethodShouldMatchInstrospectDeclaredMethodsHint() throws NoSuchMethodException { void classGetMethodShouldMatchInstrospectDeclaredMethodsHint() throws NoSuchMethodException {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
} }
@Test @Test
void classGetMethodShouldMatchInvokeDeclaredMethodsHint() { void classGetMethodShouldMatchInvokeDeclaredMethodsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
} }
@ -391,19 +388,19 @@ class InstrumentedMethodTests {
@Test @Test
void classGetMethodShouldNotMatchInstrospectPublicMethodsHintWhenPrivate() throws Exception { void classGetMethodShouldNotMatchInstrospectPublicMethodsHintWhenPrivate() throws Exception {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetScaleMethod); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetScaleMethod);
} }
@Test @Test
void classGetMethodShouldMatchInstrospectDeclaredMethodsHintWhenPrivate() { void classGetMethodShouldMatchInstrospectDeclaredMethodsHintWhenPrivate() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); hints.reflection().registerType(String.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetScaleMethod); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetScaleMethod);
} }
@Test @Test
void classGetMethodShouldNotMatchForWrongType() { void classGetMethodShouldNotMatchForWrongType() {
hints.reflection().registerType(Integer.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); hints.reflection().registerType(Integer.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
} }
@ -445,7 +442,7 @@ class InstrumentedMethodTests {
@Test @Test
void classGetDeclaredFieldShouldMatchDeclaredFieldsHint() { void classGetDeclaredFieldShouldMatchDeclaredFieldsHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); hints.reflection().registerType(String.class, MemberCategory.DECLARED_FIELDS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDFIELD, this.stringGetDeclaredField); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDFIELD, this.stringGetDeclaredField);
} }
@ -457,41 +454,39 @@ class InstrumentedMethodTests {
@Test @Test
void classGetDeclaredFieldShouldMatchFieldHint() { void classGetDeclaredFieldShouldMatchFieldHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withField("value", builder -> { hints.reflection().registerType(String.class, typeHint -> typeHint.withField("value"));
}));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDFIELD, this.stringGetDeclaredField); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDFIELD, this.stringGetDeclaredField);
} }
@Test @Test
void classGetDeclaredFieldsShouldMatchDeclaredFieldsHint() { void classGetDeclaredFieldsShouldMatchDeclaredFieldsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDFIELDS).onInstance(String.class).build(); RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDFIELDS).onInstance(String.class).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); hints.reflection().registerType(String.class, MemberCategory.DECLARED_FIELDS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDFIELDS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDFIELDS, invocation);
} }
@Test @Test
void classGetDeclaredFieldsShouldNotMatchPublicFieldsHint() { void classGetDeclaredFieldsShouldNotMatchPublicFieldsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDFIELDS).onInstance(String.class).build(); RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETDECLAREDFIELDS).onInstance(String.class).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); hints.reflection().registerType(String.class, MemberCategory.PUBLIC_FIELDS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDFIELDS, invocation); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETDECLAREDFIELDS, invocation);
} }
@Test @Test
void classGetFieldShouldMatchPublicFieldsHint() { void classGetFieldShouldMatchPublicFieldsHint() {
hints.reflection().registerType(PublicField.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); hints.reflection().registerType(PublicField.class, MemberCategory.PUBLIC_FIELDS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELD, this.getPublicField); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELD, this.getPublicField);
} }
@Test @Test
void classGetFieldShouldMatchDeclaredFieldsHint() { void classGetFieldShouldMatchDeclaredFieldsHint() {
hints.reflection().registerType(PublicField.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); hints.reflection().registerType(PublicField.class, MemberCategory.DECLARED_FIELDS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELD, this.getPublicField); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELD, this.getPublicField);
} }
@Test @Test
void classGetFieldShouldMatchFieldHint() { void classGetFieldShouldMatchFieldHint() {
hints.reflection().registerType(PublicField.class, typeHint -> typeHint.withField("field", builder -> { hints.reflection().registerType(PublicField.class, typeHint -> typeHint.withField("field"));
}));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELD, this.getPublicField); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELD, this.getPublicField);
} }
@ -499,7 +494,7 @@ class InstrumentedMethodTests {
void classGetFieldShouldNotMatchPublicFieldsHintWhenPrivate() throws NoSuchFieldException { void classGetFieldShouldNotMatchPublicFieldsHintWhenPrivate() throws NoSuchFieldException {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELD) RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELD)
.onInstance(String.class).withArgument("value").returnValue(null).build(); .onInstance(String.class).withArgument("value").returnValue(null).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); hints.reflection().registerType(String.class, MemberCategory.PUBLIC_FIELDS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETFIELD, invocation); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETFIELD, invocation);
} }
@ -507,7 +502,7 @@ class InstrumentedMethodTests {
void classGetFieldShouldMatchDeclaredFieldsHintWhenPrivate() throws NoSuchFieldException { void classGetFieldShouldMatchDeclaredFieldsHintWhenPrivate() throws NoSuchFieldException {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELD) RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELD)
.onInstance(String.class).withArgument("value").returnValue(String.class.getDeclaredField("value")).build(); .onInstance(String.class).withArgument("value").returnValue(String.class.getDeclaredField("value")).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); hints.reflection().registerType(String.class, MemberCategory.DECLARED_FIELDS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELD, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELD, invocation);
} }
@ -515,7 +510,7 @@ class InstrumentedMethodTests {
void classGetFieldShouldNotMatchForWrongType() throws Exception { void classGetFieldShouldNotMatchForWrongType() throws Exception {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELD) RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELD)
.onInstance(String.class).withArgument("value").returnValue(null).build(); .onInstance(String.class).withArgument("value").returnValue(null).build();
hints.reflection().registerType(Integer.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); hints.reflection().registerType(Integer.class, MemberCategory.DECLARED_FIELDS);
assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETFIELD, invocation); assertThatInvocationDoesNotMatch(InstrumentedMethod.CLASS_GETFIELD, invocation);
} }
@ -523,7 +518,7 @@ class InstrumentedMethodTests {
void classGetFieldsShouldMatchPublicFieldsHint() { void classGetFieldsShouldMatchPublicFieldsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELDS) RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELDS)
.onInstance(PublicField.class).build(); .onInstance(PublicField.class).build();
hints.reflection().registerType(PublicField.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); hints.reflection().registerType(PublicField.class, MemberCategory.PUBLIC_FIELDS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELDS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELDS, invocation);
} }
@ -531,7 +526,7 @@ class InstrumentedMethodTests {
void classGetFieldsShouldMatchDeclaredFieldsHint() { void classGetFieldsShouldMatchDeclaredFieldsHint() {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELDS) RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CLASS_GETFIELDS)
.onInstance(PublicField.class).build(); .onInstance(PublicField.class).build();
hints.reflection().registerType(PublicField.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); hints.reflection().registerType(PublicField.class, MemberCategory.DECLARED_FIELDS);
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELDS, invocation); assertThatInvocationMatches(InstrumentedMethod.CLASS_GETFIELDS, invocation);
} }

View File

@ -55,7 +55,7 @@ public class SimpleReflectiveProcessor implements ReflectiveProcessor {
* @param type the class to process * @param type the class to process
*/ */
protected void registerTypeHint(ReflectionHints hints, Class<?> type) { protected void registerTypeHint(ReflectionHints hints, Class<?> type) {
hints.registerType(type, hint -> {}); hints.registerType(type);
} }
/** /**

View File

@ -18,7 +18,6 @@ package org.springframework.aot.hint.support;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -26,7 +25,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.log.LogMessage; import org.springframework.core.log.LogMessage;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
@ -45,9 +43,6 @@ class SpringFactoriesLoaderRuntimeHints implements RuntimeHintsRegistrar {
private static final List<String> RESOURCE_LOCATIONS = private static final List<String> RESOURCE_LOCATIONS =
List.of(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION); List.of(SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION);
private static final Consumer<TypeHint.Builder> HINT = builder ->
builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
private static final Log logger = LogFactory.getLog(SpringFactoriesLoaderRuntimeHints.class); private static final Log logger = LogFactory.getLog(SpringFactoriesLoaderRuntimeHints.class);
@ -78,7 +73,7 @@ class SpringFactoriesLoaderRuntimeHints implements RuntimeHintsRegistrar {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(LogMessage.format("Processing factories for [%s]", factoryClassName)); logger.trace(LogMessage.format("Processing factories for [%s]", factoryClassName));
} }
hints.reflection().registerType(factoryClass, HINT); hints.reflection().registerType(factoryClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
for (String implementationClassName : implementationClassNames) { for (String implementationClassName : implementationClassNames) {
Class<?> implementationType = resolveClassName(classLoader, implementationClassName); Class<?> implementationType = resolveClassName(classLoader, implementationClassName);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
@ -87,7 +82,7 @@ class SpringFactoriesLoaderRuntimeHints implements RuntimeHintsRegistrar {
implementationClassName)); implementationClassName));
} }
if (implementationType != null) { if (implementationType != null) {
hints.reflection().registerType(implementationType, HINT); hints.reflection().registerType(implementationType, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
} }
} }
} }

View File

@ -73,8 +73,7 @@ class ReflectionHintsTests {
@Test @Test
void getTypeUsingTypeReference() { void getTypeUsingTypeReference() {
this.reflectionHints.registerType(String.class, this.reflectionHints.registerType(String.class, MemberCategory.DECLARED_FIELDS);
hint -> hint.withMembers(MemberCategory.DECLARED_FIELDS));
assertThat(this.reflectionHints.getTypeHint(TypeReference.of(String.class))).satisfies( assertThat(this.reflectionHints.getTypeHint(TypeReference.of(String.class))).satisfies(
typeWithMemberCategories(String.class, MemberCategory.DECLARED_FIELDS)); typeWithMemberCategories(String.class, MemberCategory.DECLARED_FIELDS));
} }
@ -87,7 +86,7 @@ class ReflectionHintsTests {
@Test @Test
void registerTypeReuseBuilder() { void registerTypeReuseBuilder() {
this.reflectionHints.registerType(TypeReference.of(String.class), this.reflectionHints.registerType(TypeReference.of(String.class),
typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)); MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
Field field = ReflectionUtils.findField(String.class, "value"); Field field = ReflectionUtils.findField(String.class, "value");
assertThat(field).isNotNull(); assertThat(field).isNotNull();
this.reflectionHints.registerField(field); this.reflectionHints.registerField(field);
@ -100,8 +99,7 @@ class ReflectionHintsTests {
@Test @Test
void registerClass() { void registerClass() {
this.reflectionHints.registerType(Integer.class, this.reflectionHints.registerType(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertThat(this.reflectionHints.typeHints()).singleElement().satisfies( assertThat(this.reflectionHints.typeHints()).singleElement().satisfies(
typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
} }
@ -117,7 +115,7 @@ class ReflectionHintsTests {
@Test @Test
void registerTypesApplyTheSameHints() { void registerTypesApplyTheSameHints() {
this.reflectionHints.registerTypes(TypeReference.listOf(Integer.class, String.class, Double.class), this.reflectionHints.registerTypes(TypeReference.listOf(Integer.class, String.class, Double.class),
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)); TypeHint.builtWith(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertThat(this.reflectionHints.typeHints()) assertThat(this.reflectionHints.typeHints())
.anySatisfy( .anySatisfy(
typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS)) typeWithMemberCategories(Integer.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
@ -180,8 +178,8 @@ class ReflectionHintsTests {
void registerFieldWithCustomizersCannotDowngradeWrite() { void registerFieldWithCustomizersCannotDowngradeWrite() {
Field field = ReflectionUtils.findField(TestType.class, "field"); Field field = ReflectionUtils.findField(TestType.class, "field");
assertThat(field).isNotNull(); assertThat(field).isNotNull();
this.reflectionHints.registerField(field, fieldHint -> fieldHint.withMode(FieldMode.WRITE)); this.reflectionHints.registerField(field, FieldMode.WRITE);
this.reflectionHints.registerField(field, fieldHint -> fieldHint.withMode(FieldMode.READ)); this.reflectionHints.registerField(field, FieldMode.READ);
assertTestTypeFieldHint(fieldHint -> { assertTestTypeFieldHint(fieldHint -> {
assertThat(fieldHint.getName()).isEqualTo("field"); assertThat(fieldHint.getName()).isEqualTo("field");
assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE); assertThat(fieldHint.getMode()).isEqualTo(FieldMode.WRITE);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2021 the original author or authors. * Copyright 2002-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,8 +33,7 @@ class RuntimeHintsTests {
@Test @Test
void reflectionHintWithClass() { void reflectionHintWithClass() {
this.hints.reflection().registerType(String.class, this.hints.reflection().registerType(String.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
hint -> hint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertThat(this.hints.reflection().typeHints()).singleElement().satisfies(typeHint -> { assertThat(this.hints.reflection().typeHints()).singleElement().satisfies(typeHint -> {
assertThat(typeHint.getType().getCanonicalName()).isEqualTo(String.class.getCanonicalName()); assertThat(typeHint.getType().getCanonicalName()).isEqualTo(String.class.getCanonicalName());
assertThat(typeHint.fields()).isEmpty(); assertThat(typeHint.fields()).isEmpty();

View File

@ -279,8 +279,7 @@ class ReflectiveRuntimeHintsRegistrarTests {
@Override @Override
protected void registerMethodHint(ReflectionHints hints, Method method) { protected void registerMethodHint(ReflectionHints hints, Method method) {
super.registerMethodHint(hints, method); super.registerMethodHint(hints, method);
hints.registerType(method.getReturnType(), type -> hints.registerType(method.getReturnType(), MemberCategory.INVOKE_DECLARED_METHODS);
type.withMembers(MemberCategory.INVOKE_DECLARED_METHODS));
} }
} }

View File

@ -66,81 +66,77 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void reflectionOnClassShouldMatchIntrospection() { void reflectionOnClassShouldMatchIntrospection() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> {}); runtimeHints.reflection().registerType(SampleClass.class);
assertPredicateMatches(reflection.onType(SampleClass.class)); assertPredicateMatches(reflection.onType(SampleClass.class));
} }
@Test @Test
void reflectionOnTypeReferenceShouldMatchIntrospection() { void reflectionOnTypeReferenceShouldMatchIntrospection() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> {}); runtimeHints.reflection().registerType(SampleClass.class);
assertPredicateMatches(reflection.onType(TypeReference.of(SampleClass.class))); assertPredicateMatches(reflection.onType(TypeReference.of(SampleClass.class)));
} }
@Test @Test
void reflectionOnDifferentClassShouldNotMatchIntrospection() { void reflectionOnDifferentClassShouldNotMatchIntrospection() {
runtimeHints.reflection().registerType(Integer.class, builder -> {}); runtimeHints.reflection().registerType(Integer.class);
assertPredicateDoesNotMatch(reflection.onType(TypeReference.of(SampleClass.class))); assertPredicateDoesNotMatch(reflection.onType(TypeReference.of(SampleClass.class)));
} }
@Test @Test
void typeWithMemberCategoryFailsWithNullCategory() { void typeWithMemberCategoryFailsWithNullCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->
reflection.onType(SampleClass.class).withMemberCategory(null)); reflection.onType(SampleClass.class).withMemberCategory(null));
} }
@Test @Test
void typeWithMemberCategoryMatchesCategory() { void typeWithMemberCategoryMatchesCategory() {
runtimeHints.reflection().registerType(SampleClass.class, runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertPredicateMatches(reflection.onType(SampleClass.class) assertPredicateMatches(reflection.onType(SampleClass.class)
.withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS)); .withMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS));
} }
@Test @Test
void typeWithMemberCategoryDoesNotMatchOtherCategory() { void typeWithMemberCategoryDoesNotMatchOtherCategory() {
runtimeHints.reflection().registerType(SampleClass.class, runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class) assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS)); .withMemberCategory(MemberCategory.INVOKE_PUBLIC_METHODS));
} }
@Test @Test
void typeWithMemberCategoriesMatchesCategories() { void typeWithMemberCategoriesMatchesCategories() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS,
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS)); MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateMatches(reflection.onType(SampleClass.class) assertPredicateMatches(reflection.onType(SampleClass.class)
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS)); .withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
} }
@Test @Test
void typeWithMemberCategoriesDoesNotMatchMissingCategory() { void typeWithMemberCategoriesDoesNotMatchMissingCategory() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class) assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS)); .withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS, MemberCategory.INTROSPECT_PUBLIC_METHODS));
} }
@Test @Test
void typeWithAnyMemberCategoryFailsWithNullCategories() { void typeWithAnyMemberCategoryFailsWithNullCategories() {
runtimeHints.reflection().registerType(SampleClass.class, builder -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS));
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->
reflection.onType(SampleClass.class).withAnyMemberCategory(new MemberCategory[0])); reflection.onType(SampleClass.class).withAnyMemberCategory(new MemberCategory[0]));
} }
@Test @Test
void typeWithAnyMemberCategoryMatchesCategory() { void typeWithAnyMemberCategoryMatchesCategory() {
runtimeHints.reflection().registerType(SampleClass.class, runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS,
builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS)); MemberCategory.INVOKE_PUBLIC_METHODS);
assertPredicateMatches(reflection.onType(SampleClass.class) assertPredicateMatches(reflection.onType(SampleClass.class)
.withAnyMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS)); .withAnyMemberCategory(MemberCategory.INTROSPECT_PUBLIC_METHODS));
} }
@Test @Test
void typeWithAnyMemberCategoryDoesNotMatchOtherCategory() { void typeWithAnyMemberCategoryDoesNotMatchOtherCategory() {
runtimeHints.reflection().registerType(SampleClass.class, runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS,
builder -> builder.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INVOKE_PUBLIC_METHODS)); MemberCategory.INVOKE_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onType(SampleClass.class) assertPredicateDoesNotMatch(reflection.onType(SampleClass.class)
.withAnyMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)); .withAnyMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS));
} }
@ -164,29 +160,25 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void constructorIntrospectionMatchesIntrospectPublicConstructors() { void constructorIntrospectionMatchesIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
} }
@Test @Test
void constructorIntrospectionMatchesInvokePublicConstructors() { void constructorIntrospectionMatchesInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
} }
@Test @Test
void constructorIntrospectionMatchesIntrospectDeclaredConstructors() { void constructorIntrospectionMatchesIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
} }
@Test @Test
void constructorIntrospectionMatchesInvokeDeclaredConstructors() { void constructorIntrospectionMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect()); assertPredicateMatches(reflection.onConstructor(publicConstructor).introspect());
} }
@ -206,29 +198,25 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void constructorInvocationDoesNotMatchIntrospectPublicConstructors() { void constructorInvocationDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke()); assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke());
} }
@Test @Test
void constructorInvocationMatchesInvokePublicConstructors() { void constructorInvocationMatchesInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke()); assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke());
} }
@Test @Test
void constructorInvocationDoesNotMatchIntrospectDeclaredConstructors() { void constructorInvocationDoesNotMatchIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke()); assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).invoke());
} }
@Test @Test
void constructorInvocationMatchesInvokeDeclaredConstructors() { void constructorInvocationMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke()); assertPredicateMatches(reflection.onConstructor(publicConstructor).invoke());
} }
@ -241,29 +229,25 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void privateConstructorIntrospectionDoesNotMatchIntrospectPublicConstructors() { void privateConstructorIntrospectionDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect()); assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect());
} }
@Test @Test
void privateConstructorIntrospectionDoesNotMatchInvokePublicConstructors() { void privateConstructorIntrospectionDoesNotMatchInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect()); assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).introspect());
} }
@Test @Test
void privateConstructorIntrospectionMatchesIntrospectDeclaredConstructors() { void privateConstructorIntrospectionMatchesIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect()); assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect());
} }
@Test @Test
void privateConstructorIntrospectionMatchesInvokeDeclaredConstructors() { void privateConstructorIntrospectionMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect()); assertPredicateMatches(reflection.onConstructor(privateConstructor).introspect());
} }
@ -283,29 +267,25 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void privateConstructorInvocationDoesNotMatchIntrospectPublicConstructors() { void privateConstructorInvocationDoesNotMatchIntrospectPublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
} }
@Test @Test
void privateConstructorInvocationDoesNotMatchInvokePublicConstructors() { void privateConstructorInvocationDoesNotMatchInvokePublicConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
} }
@Test @Test
void privateConstructorInvocationDoesNotMatchIntrospectDeclaredConstructors() { void privateConstructorInvocationDoesNotMatchIntrospectDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_CONSTRUCTORS));
assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke()); assertPredicateDoesNotMatch(reflection.onConstructor(privateConstructor).invoke());
} }
@Test @Test
void privateConstructorInvocationMatchesInvokeDeclaredConstructors() { void privateConstructorInvocationMatchesInvokeDeclaredConstructors() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke()); assertPredicateMatches(reflection.onConstructor(privateConstructor).invoke());
} }
@ -323,25 +303,25 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void methodIntrospectionMatchesIntrospectPublicMethods() { void methodIntrospectionMatchesIntrospectPublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
} }
@Test @Test
void methodIntrospectionMatchesInvokePublicMethods() { void methodIntrospectionMatchesInvokePublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
} }
@Test @Test
void methodIntrospectionMatchesIntrospectDeclaredMethods() { void methodIntrospectionMatchesIntrospectDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
} }
@Test @Test
void methodIntrospectionMatchesInvokeDeclaredMethods() { void methodIntrospectionMatchesInvokeDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
} }
@ -361,25 +341,25 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void methodInvocationDoesNotMatchIntrospectPublicMethods() { void methodInvocationDoesNotMatchIntrospectPublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke());
} }
@Test @Test
void methodInvocationMatchesInvokePublicMethods() { void methodInvocationMatchesInvokePublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke());
} }
@Test @Test
void methodInvocationDoesNotMatchIntrospectDeclaredMethods() { void methodInvocationDoesNotMatchIntrospectDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke());
} }
@Test @Test
void methodInvocationMatchesInvokeDeclaredMethods() { void methodInvocationMatchesInvokeDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke());
} }
@ -392,25 +372,25 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void privateMethodIntrospectionDoesNotMatchIntrospectPublicMethods() { void privateMethodIntrospectionDoesNotMatchIntrospectPublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").introspect());
} }
@Test @Test
void privateMethodIntrospectionDoesNotMatchInvokePublicMethods() { void privateMethodIntrospectionDoesNotMatchInvokePublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").introspect());
} }
@Test @Test
void privateMethodIntrospectionMatchesIntrospectDeclaredMethods() { void privateMethodIntrospectionMatchesIntrospectDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect());
} }
@Test @Test
void privateMethodIntrospectionMatchesInvokeDeclaredMethods() { void privateMethodIntrospectionMatchesInvokeDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").introspect());
} }
@ -430,25 +410,25 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void privateMethodInvocationDoesNotMatchIntrospectPublicMethods() { void privateMethodInvocationDoesNotMatchIntrospectPublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke());
} }
@Test @Test
void privateMethodInvocationDoesNotMatchInvokePublicMethods() { void privateMethodInvocationDoesNotMatchInvokePublicMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_PUBLIC_METHODS);
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke());
} }
@Test @Test
void privateMethodInvocationDoesNotMatchIntrospectDeclaredMethods() { void privateMethodInvocationDoesNotMatchIntrospectDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INTROSPECT_DECLARED_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "privateMethod").invoke());
} }
@Test @Test
void privateMethodInvocationMatchesInvokeDeclaredMethods() { void privateMethodInvocationMatchesInvokeDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").invoke()); assertPredicateMatches(reflection.onMethod(SampleClass.class, "privateMethod").invoke());
} }
@ -464,29 +444,27 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void fieldReflectionMatchesFieldHint() { void fieldReflectionMatchesFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField"));
}));
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); assertPredicateMatches(reflection.onField(SampleClass.class, "publicField"));
} }
@Test @Test
void fieldWriteReflectionDoesNotMatchFieldHint() { void fieldWriteReflectionDoesNotMatchFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField",
fieldHint -> fieldHint.withMode(FieldMode.WRITE))); FieldMode.READ));
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowWrite()); assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowWrite());
} }
@Test @Test
void fieldUnsafeReflectionDoesNotMatchFieldHint() { void fieldUnsafeReflectionDoesNotMatchFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField", fieldHint -> { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("publicField"));
}));
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowUnsafeAccess()); assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField").allowUnsafeAccess());
} }
@Test @Test
void fieldWriteReflectionMatchesFieldHintWithWrite() { void fieldWriteReflectionMatchesFieldHintWithWrite() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> runtimeHints.reflection().registerType(SampleClass.class, typeHint ->
typeHint.withField("publicField", fieldHint -> fieldHint.withMode(FieldMode.WRITE))); typeHint.withField("publicField", FieldMode.WRITE));
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite()); assertPredicateMatches(reflection.onField(SampleClass.class, "publicField").allowWrite());
} }
@ -499,32 +477,31 @@ class ReflectionHintsPredicatesTests {
@Test @Test
void fieldReflectionMatchesPublicFieldsHint() { void fieldReflectionMatchesPublicFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.PUBLIC_FIELDS);
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); assertPredicateMatches(reflection.onField(SampleClass.class, "publicField"));
} }
@Test @Test
void fieldReflectionMatchesDeclaredFieldsHint() { void fieldReflectionMatchesDeclaredFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.DECLARED_FIELDS);
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField")); assertPredicateMatches(reflection.onField(SampleClass.class, "publicField"));
} }
@Test @Test
void privateFieldReflectionMatchesFieldHint() { void privateFieldReflectionMatchesFieldHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("privateField", fieldHint -> { runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withField("privateField"));
}));
assertPredicateMatches(reflection.onField(SampleClass.class, "privateField")); assertPredicateMatches(reflection.onField(SampleClass.class, "privateField"));
} }
@Test @Test
void privateFieldReflectionDoesNotMatchPublicFieldsHint() { void privateFieldReflectionDoesNotMatchPublicFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.PUBLIC_FIELDS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.PUBLIC_FIELDS);
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "privateField")); assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "privateField"));
} }
@Test @Test
void privateFieldReflectionMatchesDeclaredFieldsHint() { void privateFieldReflectionMatchesDeclaredFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withMembers(MemberCategory.DECLARED_FIELDS)); runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.DECLARED_FIELDS);
assertPredicateMatches(reflection.onField(SampleClass.class, "privateField")); assertPredicateMatches(reflection.onField(SampleClass.class, "privateField"));
} }

View File

@ -60,7 +60,7 @@ class MessageMappingReflectiveProcessor implements ReflectiveProcessor {
} }
protected void registerTypeHints(ReflectionHints hints, Class<?> type) { protected void registerTypeHints(ReflectionHints hints, Class<?> type) {
hints.registerType(type, hint -> {}); hints.registerType(type);
} }
protected void registerMethodHints(ReflectionHints hints, Method method) { protected void registerMethodHints(ReflectionHints hints, Method method) {

View File

@ -24,6 +24,7 @@ import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints; import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.support.RuntimeHintsUtils; import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -148,8 +149,7 @@ class TestContextRuntimeHints implements RuntimeHintsRegistrar {
} }
private static void registerPublicConstructors(ReflectionHints reflectionHints, Iterable<TypeReference> types) { private static void registerPublicConstructors(ReflectionHints reflectionHints, Iterable<TypeReference> types) {
reflectionHints.registerTypes(types, reflectionHints.registerTypes(types, TypeHint.builtWith(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
builder -> builder.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
} }
private static void registerDeclaredConstructors(ReflectionHints reflectionHints, Class<?>... types) { private static void registerDeclaredConstructors(ReflectionHints reflectionHints, Class<?>... types) {
@ -161,8 +161,7 @@ class TestContextRuntimeHints implements RuntimeHintsRegistrar {
} }
private static void registerDeclaredConstructors(ReflectionHints reflectionHints, Iterable<TypeReference> types) { private static void registerDeclaredConstructors(ReflectionHints reflectionHints, Iterable<TypeReference> types) {
reflectionHints.registerTypes(types, reflectionHints.registerTypes(types, TypeHint.builtWith(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS));
} }
private static List<TypeReference> listOf(String... classNames) { private static List<TypeReference> listOf(String... classNames) {
@ -179,8 +178,7 @@ class TestContextRuntimeHints implements RuntimeHintsRegistrar {
} }
private static void registerAnnotation(ReflectionHints reflectionHints, Class<? extends Annotation> annotationType) { private static void registerAnnotation(ReflectionHints reflectionHints, Class<? extends Annotation> annotationType) {
reflectionHints.registerType(annotationType, reflectionHints.registerType(annotationType, MemberCategory.INVOKE_DECLARED_METHODS);
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_METHODS));
} }
} }

View File

@ -85,8 +85,7 @@ class TransactionBeanRegistrationAotProcessor implements BeanRegistrationAotProc
return; return;
} }
for (Class<?> proxyInterface : proxyInterfaces) { for (Class<?> proxyInterface : proxyInterfaces) {
runtimeHints.reflection().registerType(proxyInterface, runtimeHints.reflection().registerType(proxyInterface, MemberCategory.INVOKE_DECLARED_METHODS);
builder -> builder.withMembers(MemberCategory.INVOKE_DECLARED_METHODS));
} }
} }
} }

View File

@ -19,6 +19,7 @@ package org.springframework.transaction.annotation;
import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.support.RuntimeHintsUtils; import org.springframework.aot.hint.support.RuntimeHintsUtils;
import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionDefinition;
@ -37,7 +38,8 @@ class TransactionRuntimeHints implements RuntimeHintsRegistrar {
public void registerHints(RuntimeHints hints, ClassLoader classLoader) { public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
RuntimeHintsUtils.registerSynthesizedAnnotation(hints, Transactional.class); RuntimeHintsUtils.registerSynthesizedAnnotation(hints, Transactional.class);
hints.reflection().registerTypes(TypeReference.listOf( hints.reflection().registerTypes(TypeReference.listOf(
Isolation.class, Propagation.class, TransactionDefinition.class), Isolation.class, Propagation.class, TransactionDefinition.class),
builder -> builder.withMembers(MemberCategory.DECLARED_FIELDS)); TypeHint.builtWith(MemberCategory.DECLARED_FIELDS));
} }
} }

View File

@ -16,12 +16,9 @@
package org.springframework.http.codec; package org.springframework.http.codec;
import java.util.function.Consumer;
import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar; import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint.Builder;
import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.TypeReference;
import org.springframework.http.codec.support.DefaultClientCodecConfigurer; import org.springframework.http.codec.support.DefaultClientCodecConfigurer;
import org.springframework.http.codec.support.DefaultServerCodecConfigurer; import org.springframework.http.codec.support.DefaultServerCodecConfigurer;
@ -37,16 +34,14 @@ import org.springframework.lang.Nullable;
*/ */
class CodecConfigurerRuntimeHints implements RuntimeHintsRegistrar { class CodecConfigurerRuntimeHints implements RuntimeHintsRegistrar {
private static final Consumer<Builder> CODEC_HINT = type -> type
.onReachableType(CodecConfigurerFactory.class)
.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
@Override @Override
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
hints.resources().registerPattern("org/springframework/http/codec/CodecConfigurer.properties"); hints.resources().registerPattern(
hints.reflection().registerTypes(TypeReference.listOf( "org/springframework/http/codec/CodecConfigurer.properties");
DefaultClientCodecConfigurer.class, DefaultServerCodecConfigurer.class), hints.reflection().registerTypes(
CODEC_HINT); TypeReference.listOf(DefaultClientCodecConfigurer.class, DefaultServerCodecConfigurer.class),
typeHint -> typeHint.onReachableType(CodecConfigurerFactory.class)
.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));
} }
} }

View File

@ -59,7 +59,7 @@ class RequestMappingReflectiveProcessor implements ReflectiveProcessor {
} }
protected void registerTypeHints(ReflectionHints hints, Class<?> type) { protected void registerTypeHints(ReflectionHints hints, Class<?> type) {
hints.registerType(type, hint -> {}); hints.registerType(type);
} }
protected void registerMethodHints(ReflectionHints hints, Method method) { protected void registerMethodHints(ReflectionHints hints, Method method) {