Skip to content

Commit c8f7a76

Browse files
committed
Merge pull request #29130 from sreenath-tm
* pr/29130: Polish "Adapt FieldHint to recent GraalVM versions" Adapt FieldHint to recent GraalVM versions Closes gh-29130
2 parents c854e35 + 042a4f3 commit c8f7a76

File tree

17 files changed

+40
-545
lines changed

17 files changed

+40
-545
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/main/java/org/springframework/aot/hint/FieldHint.java

Lines changed: 2 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
package org.springframework.aot.hint;
1818

1919
import java.lang.reflect.Field;
20-
import java.util.function.Consumer;
21-
22-
import org.springframework.lang.Nullable;
23-
import org.springframework.util.Assert;
2420

2521
/**
2622
* A hint that describes the need for reflection on a {@link Field}.
@@ -30,115 +26,8 @@
3026
*/
3127
public final class FieldHint extends MemberHint {
3228

33-
private final FieldMode mode;
34-
35-
private final boolean allowUnsafeAccess;
36-
37-
38-
private FieldHint(Builder builder) {
39-
super(builder.name);
40-
this.mode = (builder.mode != null ? builder.mode : FieldMode.WRITE);
41-
this.allowUnsafeAccess = builder.allowUnsafeAccess;
42-
}
43-
44-
/**
45-
* Return whether setting the value of the field should be allowed.
46-
* @return {@code true} to allow {@link Field#set(Object, Object)}.
47-
* @deprecated in favor of {@link #getMode()}
48-
*/
49-
@Deprecated
50-
public boolean isAllowWrite() {
51-
return this.mode == FieldMode.WRITE;
52-
}
53-
54-
/**
55-
* Return the {@linkplain FieldMode mode} that applies to this hint.
56-
* @return the mode
57-
*/
58-
public FieldMode getMode() {
59-
return this.mode;
60-
}
61-
62-
/**
63-
* Return whether using {@code Unsafe} on the field should be allowed.
64-
* @return {@code true} to allow unsafe access
65-
*/
66-
public boolean isAllowUnsafeAccess() {
67-
return this.allowUnsafeAccess;
68-
}
69-
70-
/**
71-
* Return a {@link Consumer} that applies the given {@link FieldMode}
72-
* to the accepted {@link Builder}.
73-
* @param mode the mode to apply
74-
* @return a consumer to apply the mode
75-
*/
76-
public static Consumer<Builder> builtWith(FieldMode mode) {
77-
return builder -> builder.withMode(mode);
29+
FieldHint(String name) {
30+
super(name);
7831
}
7932

80-
81-
/**
82-
* Builder for {@link FieldHint}.
83-
*/
84-
public static class Builder {
85-
86-
private final String name;
87-
88-
@Nullable
89-
private FieldMode mode;
90-
91-
private boolean allowUnsafeAccess;
92-
93-
94-
Builder(String name) {
95-
this.name = name;
96-
}
97-
98-
/**
99-
* Specify if setting the value of the field should be allowed.
100-
* @param allowWrite {@code true} to allow {@link Field#set(Object, Object)}
101-
* @return {@code this}, to facilitate method chaining
102-
* @deprecated in favor of {@link #withMode(FieldMode)}
103-
*/
104-
@Deprecated
105-
public Builder allowWrite(boolean allowWrite) {
106-
if (allowWrite) {
107-
return withMode(FieldMode.WRITE);
108-
}
109-
return this;
110-
}
111-
112-
/**
113-
* Specify that the {@linkplain FieldMode mode} is required.
114-
* @param mode the required mode
115-
* @return {@code this}, to facilitate method chaining
116-
*/
117-
public Builder withMode(FieldMode mode) {
118-
Assert.notNull(mode, "'mode' must not be null");
119-
if ((this.mode == null || !this.mode.includes(mode))) {
120-
this.mode = mode;
121-
}
122-
return this;
123-
}
124-
125-
/**
126-
* Specify whether using {@code Unsafe} on the field should be allowed.
127-
* @param allowUnsafeAccess {@code true} to allow unsafe access
128-
* @return {@code this}, to facilitate method chaining
129-
*/
130-
public Builder allowUnsafeAccess(boolean allowUnsafeAccess) {
131-
this.allowUnsafeAccess = allowUnsafeAccess;
132-
return this;
133-
}
134-
135-
/**
136-
* Create a {@link FieldHint} based on the state of this builder.
137-
* @return a field hint
138-
*/
139-
FieldHint build() {
140-
return new FieldHint(this);
141-
}
142-
143-
}
14433
}

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: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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.values().stream().map(FieldHint.Builder::build).collect(Collectors.toSet());
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
}
@@ -147,7 +147,7 @@ public static class Builder {
147147
@Nullable
148148
private TypeReference reachableType;
149149

150-
private final Map<String, FieldHint.Builder> fields = new HashMap<>();
150+
private final Set<String> fields = new HashSet<>();
151151

152152
private final Map<ExecutableKey, ExecutableHint.Builder> constructors = new HashMap<>();
153153

@@ -184,36 +184,13 @@ public Builder onReachableType(Class<?> reachableType) {
184184
return this;
185185
}
186186

187-
/**
188-
* Register the need for reflection on the field with the specified name,
189-
* enabling write access.
190-
* @param name the name of the field
191-
* @return {@code this}, to facilitate method chaining
192-
*/
193-
public Builder withField(String name) {
194-
return withField(name, FieldMode.WRITE);
195-
}
196-
197-
/**
198-
* Register the need for reflection on the field with the specified name
199-
* using the specified {@link FieldMode}.
200-
* @param name the name of the field
201-
* @param mode the requested mode
202-
* @return {@code this}, to facilitate method chaining
203-
*/
204-
public Builder withField(String name, FieldMode mode) {
205-
return withField(name, FieldHint.builtWith(mode));
206-
}
207-
208187
/**
209188
* Register the need for reflection on the field with the specified name.
210189
* @param name the name of the field
211-
* @param fieldHint a builder to further customize the hints of this field
212190
* @return {@code this}, to facilitate method chaining
213191
*/
214-
public Builder withField(String name, Consumer<FieldHint.Builder> fieldHint) {
215-
FieldHint.Builder builder = this.fields.computeIfAbsent(name, FieldHint.Builder::new);
216-
fieldHint.accept(builder);
192+
public Builder withField(String name) {
193+
this.fields.add(name);
217194
return this;
218195
}
219196

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

0 commit comments

Comments
 (0)