Skip to content

Commit 042a4f3

Browse files
committed
Polish "Adapt FieldHint to recent GraalVM versions"
See gh-29130
1 parent 1cb5f00 commit 042a4f3

File tree

16 files changed

+49
-388
lines changed

16 files changed

+49
-388
lines changed

spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanRegistrationAotContributionTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void contributeWhenPrivateFieldInjectionInjectsUsingReflection() {
7373
RegisteredBean registeredBean = getAndApplyContribution(
7474
PrivateFieldInjectionSample.class);
7575
assertThat(RuntimeHintsPredicates.reflection()
76-
.onField(PrivateFieldInjectionSample.class, "environment").withWriteMode())
76+
.onField(PrivateFieldInjectionSample.class, "environment"))
7777
.accepts(this.generationContext.getRuntimeHints());
7878
compile(registeredBean, (postProcessor, compiled) -> {
7979
PrivateFieldInjectionSample instance = new PrivateFieldInjectionSample();
@@ -92,7 +92,7 @@ void contributeWhenPackagePrivateFieldInjectionInjectsUsingConsumer() {
9292
RegisteredBean registeredBean = getAndApplyContribution(
9393
PackagePrivateFieldInjectionSample.class);
9494
assertThat(RuntimeHintsPredicates.reflection()
95-
.onField(PackagePrivateFieldInjectionSample.class, "environment").withWriteMode())
95+
.onField(PackagePrivateFieldInjectionSample.class, "environment"))
9696
.accepts(this.generationContext.getRuntimeHints());
9797
compile(registeredBean, (postProcessor, compiled) -> {
9898
PackagePrivateFieldInjectionSample instance = new PackagePrivateFieldInjectionSample();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ enum InstrumentedMethod {
290290
* {@link Field#set(Object, Object)}.
291291
*/
292292
FIELD_SET(Field.class, "set", HintType.REFLECTION,
293-
invocation -> reflection().onField(invocation.getInstance()).withWriteMode()),
293+
invocation -> reflection().onField(invocation.getInstance())),
294294

295295

296296
/*

spring-core/src/test/java/org/springframework/aot/hint/FieldHintTests.java renamed to spring-core/src/main/java/org/springframework/aot/hint/FieldHint.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,18 @@
1616

1717
package org.springframework.aot.hint;
1818

19-
import org.junit.jupiter.api.Test;
20-
21-
import static org.assertj.core.api.Assertions.assertThat;
19+
import java.lang.reflect.Field;
2220

2321
/**
24-
* Tests for {@link FieldHint}.
22+
* A hint that describes the need for reflection on a {@link Field}.
2523
*
26-
* @author Phillip Webb
24+
* @author Stephane Nicoll
25+
* @since 6.0
2726
*/
28-
class FieldHintTests {
27+
public final class FieldHint extends MemberHint {
2928

30-
@Test
31-
void builtWithAppliesMode() {
32-
FieldHint.Builder builder = new FieldHint.Builder("test");
33-
assertThat(builder.build().getMode()).isEqualTo(FieldMode.WRITE);
34-
FieldHint.builtWith(FieldMode.READ).accept(builder);
35-
assertThat(builder.build().getMode()).isEqualTo(FieldMode.READ);
29+
FieldHint(String name) {
30+
super(name);
3631
}
3732

3833
}

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

Lines changed: 0 additions & 51 deletions
This file was deleted.

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -147,35 +147,14 @@ public ReflectionHints registerTypes(Iterable<TypeReference> types, Consumer<Typ
147147
return this;
148148
}
149149

150-
/**
151-
* Register the need for reflection on the specified {@link Field},
152-
* enabling {@link FieldMode#WRITE}.
153-
* @param field the field that requires reflection
154-
* @return {@code this}, to facilitate method chaining
155-
*/
156-
public ReflectionHints registerField(Field field) {
157-
return registerField(field, FieldMode.WRITE);
158-
}
159-
160-
/**
161-
* Register the need for reflection on the specified {@link Field}
162-
* using the specified {@link FieldMode}.
163-
* @param field the field that requires reflection
164-
* @return {@code this}, to facilitate method chaining
165-
*/
166-
public ReflectionHints registerField(Field field, FieldMode mode) {
167-
return registerField(field, FieldHint.builtWith(mode));
168-
}
169-
170150
/**
171151
* Register the need for reflection on the specified {@link Field}.
172152
* @param field the field that requires reflection
173-
* @param fieldHint a builder to further customize the hints of this field
174153
* @return {@code this}, to facilitate method chaining
175154
*/
176-
public ReflectionHints registerField(Field field, Consumer<FieldHint.Builder> fieldHint) {
155+
public ReflectionHints registerField(Field field) {
177156
return registerType(TypeReference.of(field.getDeclaringClass()),
178-
typeHint -> typeHint.withField(field.getName(), fieldHint));
157+
typeHint -> typeHint.withField(field.getName()));
179158
}
180159

181160
/**

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public final class TypeHint implements ConditionalHint {
4444
@Nullable
4545
private final TypeReference reachableType;
4646

47-
private final Set<String> fields;
47+
private final Set<FieldHint> fields;
4848

4949
private final Set<ExecutableHint> constructors;
5050

@@ -57,7 +57,7 @@ private TypeHint(Builder builder) {
5757
this.type = builder.type;
5858
this.reachableType = builder.reachableType;
5959
this.memberCategories = Set.copyOf(builder.memberCategories);
60-
this.fields = builder.fields;
60+
this.fields = builder.fields.stream().map(FieldHint::new).collect(Collectors.toSet());
6161
this.constructors = builder.constructors.values().stream().map(ExecutableHint.Builder::build).collect(Collectors.toSet());
6262
this.methods = builder.methods.values().stream().map(ExecutableHint.Builder::build).collect(Collectors.toSet());
6363
}
@@ -89,10 +89,10 @@ public TypeReference getReachableType() {
8989

9090
/**
9191
* Return the fields that require reflection.
92-
* @return a stream of Strings
92+
* @return a stream of {@link FieldHint}
9393
*/
94-
public Set<String> fields() {
95-
return this.fields;
94+
public Stream<FieldHint> fields() {
95+
return this.fields.stream();
9696
}
9797

9898
/**
@@ -185,16 +185,15 @@ public Builder onReachableType(Class<?> reachableType) {
185185
}
186186

187187
/**
188-
* Register the need for reflection on the field with the specified name,
189-
* enabling write access.
188+
* Register the need for reflection on the field with the specified name.
190189
* @param name the name of the field
191190
* @return {@code this}, to facilitate method chaining
192191
*/
193192
public Builder withField(String name) {
194-
return withField(name);
193+
this.fields.add(name);
194+
return this;
195195
}
196196

197-
198197
/**
199198
* Register the need for reflection on the constructor with the specified
200199
* parameter types, enabling {@link ExecutableMode#INVOKE}.

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

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
import org.springframework.aot.hint.ExecutableHint;
3030
import org.springframework.aot.hint.ExecutableMode;
31-
import org.springframework.aot.hint.FieldHint;
32-
import org.springframework.aot.hint.FieldMode;
3331
import org.springframework.aot.hint.MemberCategory;
3432
import org.springframework.aot.hint.ReflectionHints;
3533
import org.springframework.aot.hint.RuntimeHints;
@@ -375,57 +373,10 @@ public static class FieldHintPredicate implements Predicate<RuntimeHints> {
375373

376374
private final Field field;
377375

378-
private FieldMode mode = FieldMode.READ;
379-
380-
private boolean allowUnsafeAccess;
381-
382376
FieldHintPredicate(Field field) {
383377
this.field = field;
384378
}
385379

386-
/**
387-
* Refine the current predicate to match if write access is allowed on the field.
388-
* @return the refined {@link RuntimeHints} predicate
389-
* @see FieldHint#isAllowWrite()
390-
* @deprecated in favor of {@link #withReadMode()} or {@link #withWriteMode()}
391-
*/
392-
@Deprecated
393-
public FieldHintPredicate allowWrite() {
394-
this.mode = FieldMode.WRITE;
395-
return this;
396-
}
397-
398-
/**
399-
* Refine the current predicate to match if read access is allowed on the field.
400-
* @return the refined {@link RuntimeHints} predicate
401-
* @see FieldHint#getMode()
402-
*/
403-
public FieldHintPredicate withReadMode() {
404-
// FieldMode.READ is already the default and should not override a writeMode() call.
405-
return this;
406-
}
407-
408-
/**
409-
* Refine the current predicate to match if write access is allowed on the field.
410-
* @return the refined {@link RuntimeHints} predicate
411-
* @see FieldHint#getMode()
412-
*/
413-
public FieldHintPredicate withWriteMode() {
414-
this.mode = FieldMode.WRITE;
415-
return this;
416-
}
417-
418-
419-
/**
420-
* Refine the current predicate to match if unsafe access is allowed on the field.
421-
* @return the refined {@link RuntimeHints} predicate
422-
* @see FieldHint#isAllowUnsafeAccess() ()
423-
*/
424-
public FieldHintPredicate allowUnsafeAccess() {
425-
this.allowUnsafeAccess = true;
426-
return this;
427-
}
428-
429380
@Override
430381
public boolean test(RuntimeHints runtimeHints) {
431382
TypeHint typeHint = runtimeHints.reflection().getTypeHint(this.field.getDeclaringClass());
@@ -447,9 +398,7 @@ private boolean memberCategoryMatch(TypeHint typeHint) {
447398

448399
private boolean exactMatch(TypeHint typeHint) {
449400
return typeHint.fields().anyMatch(fieldHint ->
450-
this.field.getName().equals(fieldHint.getName())
451-
&& (fieldHint.getMode().includes(this.mode))
452-
&& (!this.allowUnsafeAccess || this.allowUnsafeAccess == fieldHint.isAllowUnsafeAccess()));
401+
this.field.getName().equals(fieldHint.getName()));
453402
}
454403
}
455404

spring-core/src/main/java/org/springframework/aot/nativex/ReflectionHintsWriter.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.aot.hint.ExecutableHint;
2727
import org.springframework.aot.hint.ExecutableMode;
2828
import org.springframework.aot.hint.FieldHint;
29-
import org.springframework.aot.hint.FieldMode;
3029
import org.springframework.aot.hint.MemberCategory;
3130
import org.springframework.aot.hint.ReflectionHints;
3231
import org.springframework.aot.hint.TypeHint;
@@ -78,12 +77,6 @@ private void handleFields(Map<String, Object> attributes, Stream<FieldHint> fiel
7877
private Map<String, Object> toAttributes(FieldHint hint) {
7978
Map<String, Object> attributes = new LinkedHashMap<>();
8079
attributes.put("name", hint.getName());
81-
if (hint.getMode() == FieldMode.WRITE) {
82-
attributes.put("allowWrite", true);
83-
}
84-
if (hint.isAllowUnsafeAccess()) {
85-
attributes.put("allowUnsafeAccess", hint.isAllowUnsafeAccess());
86-
}
8780
return attributes;
8881
}
8982

spring-core/src/test/java/org/springframework/aot/hint/FieldModeTests.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)