Skip to content

Commit f85752a

Browse files
committed
Fix hints and predicates for Field reflective access
This commit revisits the arrangement for Field hints after changes made in gh-34239. Closes gh-34294
1 parent 3302bc4 commit f85752a

File tree

21 files changed

+106
-136
lines changed

21 files changed

+106
-136
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJAdvisorBeanRegistrationAotProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public AspectJAdvisorContribution(Class<?> beanClass) {
7474

7575
@Override
7676
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
77-
generationContext.getRuntimeHints().reflection().registerType(this.beanClass, MemberCategory.INVOKE_DECLARED_FIELDS);
77+
generationContext.getRuntimeHints().reflection().registerType(this.beanClass, MemberCategory.ACCESS_DECLARED_FIELDS);
7878
}
7979
}
8080

spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectJAdvisorBeanRegistrationAotProcessorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AspectJAdvisorBeanRegistrationAotProcessorTests {
4848
@Test
4949
void shouldProcessAspectJClass() {
5050
process(AspectJClass.class);
51-
assertThat(reflection().onType(AspectJClass.class).withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS))
51+
assertThat(reflection().onType(AspectJClass.class).withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS))
5252
.accepts(this.runtimeHints);
5353
}
5454

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,10 @@ private void registerReflectionHints(RootBeanDefinition beanDefinition, Method w
253253
// ReflectionUtils#findField searches recursively in the type hierarchy
254254
Class<?> searchType = beanDefinition.getTargetType();
255255
while (searchType != null && searchType != writeMethod.getDeclaringClass()) {
256-
this.hints.reflection().registerType(searchType, MemberCategory.INVOKE_DECLARED_FIELDS);
256+
this.hints.reflection().registerType(searchType, MemberCategory.ACCESS_DECLARED_FIELDS);
257257
searchType = searchType.getSuperclass();
258258
}
259-
this.hints.reflection().registerType(writeMethod.getDeclaringClass(), MemberCategory.INVOKE_DECLARED_FIELDS);
259+
this.hints.reflection().registerType(writeMethod.getDeclaringClass(), MemberCategory.ACCESS_DECLARED_FIELDS);
260260
}
261261

262262
private void addQualifiers(CodeBlock.Builder code, RootBeanDefinition beanDefinition) {

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertiesCodeGeneratorTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ private void assertHasMethodInvokeHints(Class<?> beanType, String... methodNames
574574

575575
private void assertHasDeclaredFieldsHint(Class<?> beanType) {
576576
assertThat(RuntimeHintsPredicates.reflection()
577-
.onType(beanType).withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS))
577+
.onType(beanType).withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS))
578578
.accepts(this.generationContext.getRuntimeHints());
579579
}
580580

spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public AotContribution(Collection<Class<?>> validatedClasses,
233233
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
234234
ReflectionHints hints = generationContext.getRuntimeHints().reflection();
235235
for (Class<?> validatedClass : this.validatedClasses) {
236-
hints.registerType(validatedClass, MemberCategory.INVOKE_DECLARED_FIELDS);
236+
hints.registerType(validatedClass, MemberCategory.ACCESS_DECLARED_FIELDS);
237237
}
238238
for (Class<? extends ConstraintValidator<?, ?>> constraintValidatorClass : this.constraintValidatorClasses) {
239239
hints.registerType(constraintValidatorClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);

spring-context/src/test/java/org/springframework/context/annotation/AnnotationConfigApplicationContextTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ void refreshForAotRegisterHintsForCglibProxy() {
534534
TypeReference cglibType = TypeReference.of(CglibConfiguration.class.getName() + "$$SpringCGLIB$$0");
535535
assertThat(RuntimeHintsPredicates.reflection().onType(cglibType)
536536
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
537-
MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.INVOKE_DECLARED_FIELDS))
537+
MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.ACCESS_DECLARED_FIELDS))
538538
.accepts(runtimeHints);
539539
assertThat(RuntimeHintsPredicates.reflection().onType(CglibConfiguration.class)
540540
.withMemberCategories(MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS))

spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void shouldProcessMethodParameterLevelConstraint() {
7979
process(MethodParameterLevelConstraint.class);
8080
assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).hasSize(2);
8181
assertThat(RuntimeHintsPredicates.reflection().onType(MethodParameterLevelConstraint.class)
82-
.withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
82+
.withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
8383
assertThat(RuntimeHintsPredicates.reflection().onType(ExistsValidator.class)
8484
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.generationContext.getRuntimeHints());
8585
}
@@ -89,7 +89,7 @@ void shouldProcessConstructorParameterLevelConstraint() {
8989
process(ConstructorParameterLevelConstraint.class);
9090
assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).hasSize(2);
9191
assertThat(RuntimeHintsPredicates.reflection().onType(ConstructorParameterLevelConstraint.class)
92-
.withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
92+
.withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
9393
assertThat(RuntimeHintsPredicates.reflection().onType(ExistsValidator.class)
9494
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.generationContext.getRuntimeHints());
9595
}
@@ -99,7 +99,7 @@ void shouldProcessPropertyLevelConstraint() {
9999
process(PropertyLevelConstraint.class);
100100
assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).hasSize(2);
101101
assertThat(RuntimeHintsPredicates.reflection().onType(PropertyLevelConstraint.class)
102-
.withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
102+
.withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
103103
assertThat(RuntimeHintsPredicates.reflection().onType(ExistsValidator.class)
104104
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.generationContext.getRuntimeHints());
105105
}
@@ -109,7 +109,7 @@ void shouldProcessGenericTypeLevelConstraint() {
109109
process(GenericTypeLevelConstraint.class);
110110
assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).hasSize(2);
111111
assertThat(RuntimeHintsPredicates.reflection().onType(GenericTypeLevelConstraint.class)
112-
.withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
112+
.withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
113113
assertThat(RuntimeHintsPredicates.reflection().onType(PatternValidator.class)
114114
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.generationContext.getRuntimeHints());
115115
}
@@ -119,9 +119,9 @@ void shouldProcessTransitiveGenericTypeLevelConstraint() {
119119
process(TransitiveGenericTypeLevelConstraint.class);
120120
assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).hasSize(3);
121121
assertThat(RuntimeHintsPredicates.reflection().onType(TransitiveGenericTypeLevelConstraint.class)
122-
.withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
122+
.withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
123123
assertThat(RuntimeHintsPredicates.reflection().onType(Exclude.class)
124-
.withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
124+
.withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
125125
assertThat(RuntimeHintsPredicates.reflection().onType(PatternValidator.class)
126126
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.generationContext.getRuntimeHints());
127127
}
@@ -132,7 +132,7 @@ void shouldProcessRecursiveGenericsWithoutInfiniteRecursion(Class<?> beanClass)
132132
process(beanClass);
133133
assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).hasSize(1);
134134
assertThat(RuntimeHintsPredicates.reflection().onType(beanClass)
135-
.withMemberCategory(MemberCategory.INVOKE_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
135+
.withMemberCategory(MemberCategory.ACCESS_DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
136136
}
137137

138138
@Test // gh-33940

spring-core-test/src/main/java/org/springframework/aot/agent/InstrumentedMethod.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,13 @@ enum InstrumentedMethod {
243243
* {@link Field#get(Object)}.
244244
*/
245245
FIELD_GET(Field.class, "get", HintType.REFLECTION,
246-
invocation -> reflection().onFieldInvocation(invocation.getInstance())),
246+
invocation -> reflection().onFieldAccess(invocation.getInstance())),
247247

248248
/**
249249
* {@link Field#set(Object, Object)}.
250250
*/
251251
FIELD_SET(Field.class, "set", HintType.REFLECTION,
252-
invocation -> reflection().onFieldInvocation(invocation.getInstance())),
252+
invocation -> reflection().onFieldAccess(invocation.getInstance())),
253253

254254

255255
/*

spring-core/src/main/java/org/springframework/aot/hint/BindingReflectionHintsRegistrar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void registerReflectionHints(ReflectionHints hints, Set<Type> seen, Type
100100
typeHint.withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS,
101101
MemberCategory.INVOKE_PUBLIC_METHODS);
102102
}
103-
typeHint.withMembers(MemberCategory.INVOKE_DECLARED_FIELDS,
103+
typeHint.withMembers(MemberCategory.ACCESS_DECLARED_FIELDS,
104104
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
105105
for (Method method : clazz.getMethods()) {
106106
String methodName = method.getName();

spring-core/src/main/java/org/springframework/aot/hint/MemberCategory.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public enum MemberCategory {
3333

3434
/**
3535
* A category that represents reflective field access on public {@linkplain Field fields}.
36-
* @deprecated in favor of @link #INVOKE_PUBLIC_FIELDS} with similar semantics.
36+
* @deprecated in favor of {@link #ACCESS_PUBLIC_FIELDS} with similar semantics.
3737
* @see Field#get(Object)
3838
* @see Field#set(Object, Object)
3939
*/
@@ -44,7 +44,7 @@ public enum MemberCategory {
4444
* A category that represents reflective field access on
4545
* {@linkplain Class#getDeclaredFields() declared fields}: all fields defined by the
4646
* class but not inherited fields.
47-
* @deprecated in favor of @link #INVOKE_DECLARED_FIELDS} with similar semantics.
47+
* @deprecated in favor of {@link #ACCESS_DECLARED_FIELDS} with similar semantics.
4848
* @see Class#getDeclaredFields()
4949
* @see Field#get(Object)
5050
* @see Field#set(Object, Object)
@@ -53,20 +53,23 @@ public enum MemberCategory {
5353
DECLARED_FIELDS,
5454

5555
/**
56-
* A category that represents getting/setting values on public {@linkplain Field fields}.
56+
* A category that represents reflective field access on public {@linkplain Field fields}..
5757
* @see Field#get(Object)
5858
* @see Field#set(Object, Object)
5959
* @since 7.0
6060
*/
61-
INVOKE_PUBLIC_FIELDS,
61+
ACCESS_PUBLIC_FIELDS,
6262

6363
/**
64-
* A category that represents getting/setting values on declared {@linkplain Field fields}.
64+
* A category that represents reflective field access on
65+
* {@linkplain Class#getDeclaredFields() declared fields}: all fields defined by the
66+
* class but not inherited fields.
67+
* @see Class#getDeclaredFields()
6568
* @see Field#get(Object)
6669
* @see Field#set(Object, Object)
6770
* @since 7.0
6871
*/
69-
INVOKE_DECLARED_FIELDS,
72+
ACCESS_DECLARED_FIELDS,
7073

7174
/**
7275
* A category that defines public {@linkplain Constructor constructors} can

0 commit comments

Comments
 (0)