Skip to content

Commit 7ca57b7

Browse files
committed
Use consistent registration API in TypeHint.Builder
This commit adapts the registration of fields, constructors, and methods to provide the same convenience than the reflection-based one available in ReflectionHints. See gh-29011
1 parent 5aa8583 commit 7ca57b7

File tree

6 files changed

+249
-97
lines changed

6 files changed

+249
-97
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ void classGetConstructorShouldMatchInvokeDeclaredConstructorsHint() {
151151

152152
@Test
153153
void classGetConstructorShouldMatchInstrospectConstructorHint() {
154-
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(Collections.emptyList(),
155-
constructorHint -> constructorHint.withMode(ExecutableMode.INTROSPECT)));
154+
hints.reflection().registerType(String.class,typeHint ->
155+
typeHint.withConstructor(Collections.emptyList(), ExecutableMode.INTROSPECT));
156156
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
157157
}
158158

159159
@Test
160160
void classGetConstructorShouldMatchInvokeConstructorHint() {
161-
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(Collections.emptyList(),
162-
constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
161+
hints.reflection().registerType(String.class, typeHint ->
162+
typeHint.withConstructor(Collections.emptyList(),ExecutableMode.INVOKE));
163163
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
164164
}
165165

@@ -201,15 +201,15 @@ void classGetDeclaredConstructorShouldNotMatchIntrospectPublicConstructorsHint()
201201

202202
@Test
203203
void classGetDeclaredConstructorShouldMatchInstrospectConstructorHint() {
204-
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class),
205-
constructorHint -> constructorHint.withMode(ExecutableMode.INTROSPECT)));
204+
hints.reflection().registerType(String.class, typeHint ->
205+
typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class), ExecutableMode.INTROSPECT));
206206
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor);
207207
}
208208

209209
@Test
210210
void classGetDeclaredConstructorShouldMatchInvokeConstructorHint() {
211-
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class),
212-
constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
211+
hints.reflection().registerType(String.class, typeHint ->
212+
typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class), ExecutableMode.INVOKE));
213213
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor);
214214
}
215215

@@ -237,7 +237,7 @@ void constructorNewInstanceShouldMatchInvokeHintOnConstructor() throws NoSuchMet
237237
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE)
238238
.onInstance(String.class.getConstructor()).returnValue("").build();
239239
hints.reflection().registerType(String.class, typeHint ->
240-
typeHint.withConstructor(Collections.emptyList(), constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
240+
typeHint.withConstructor(Collections.emptyList(), ExecutableMode.INVOKE));
241241
assertThatInvocationMatches(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE, invocation);
242242
}
243243

@@ -246,7 +246,7 @@ void constructorNewInstanceShouldNotMatchIntrospectHintOnConstructor() throws No
246246
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE)
247247
.onInstance(String.class.getConstructor()).returnValue("").build();
248248
hints.reflection().registerType(String.class, typeHint ->
249-
typeHint.withConstructor(Collections.emptyList(), constructorHint -> constructorHint.withMode(ExecutableMode.INTROSPECT)));
249+
typeHint.withConstructor(Collections.emptyList(), ExecutableMode.INTROSPECT));
250250
assertThatInvocationDoesNotMatch(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE, invocation);
251251
}
252252

@@ -285,15 +285,15 @@ void classGetDeclaredMethodShouldNotMatchIntrospectPublicMethodsHint() {
285285
void classGetDeclaredMethodShouldMatchIntrospectMethodHint() {
286286
List<TypeReference> parameterTypes = TypeReference.listOf(int.class, float.class);
287287
hints.reflection().registerType(String.class, typeHint ->
288-
typeHint.withMethod("scale", parameterTypes, methodHint -> methodHint.withMode(ExecutableMode.INTROSPECT)));
288+
typeHint.withMethod("scale", parameterTypes, ExecutableMode.INTROSPECT));
289289
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
290290
}
291291

292292
@Test
293293
void classGetDeclaredMethodShouldMatchInvokeMethodHint() {
294294
List<TypeReference> parameterTypes = TypeReference.listOf(int.class, float.class);
295295
hints.reflection().registerType(String.class, typeHint ->
296-
typeHint.withMethod("scale", parameterTypes, methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
296+
typeHint.withMethod("scale", parameterTypes, ExecutableMode.INVOKE));
297297
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
298298
}
299299

@@ -378,14 +378,14 @@ void classGetMethodShouldMatchInvokeDeclaredMethodsHint() {
378378
@Test
379379
void classGetMethodShouldMatchIntrospectMethodHint() {
380380
hints.reflection().registerType(String.class, typeHint ->
381-
typeHint.withMethod("toString", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INTROSPECT)));
381+
typeHint.withMethod("toString", Collections.emptyList(), ExecutableMode.INTROSPECT));
382382
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
383383
}
384384

385385
@Test
386386
void classGetMethodShouldMatchInvokeMethodHint() {
387387
hints.reflection().registerType(String.class, typeHint ->
388-
typeHint.withMethod("toString", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
388+
typeHint.withMethod("toString", Collections.emptyList(), ExecutableMode.INVOKE));
389389
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
390390
}
391391

@@ -412,7 +412,7 @@ void methodInvokeShouldMatchInvokeHintOnMethod() throws NoSuchMethodException {
412412
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.METHOD_INVOKE)
413413
.onInstance(String.class.getMethod("startsWith", String.class)).withArguments("testString", new Object[] { "test" }).build();
414414
hints.reflection().registerType(String.class, typeHint -> typeHint.withMethod("startsWith",
415-
TypeReference.listOf(String.class), methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
415+
TypeReference.listOf(String.class), ExecutableMode.INVOKE));
416416
assertThatInvocationMatches(InstrumentedMethod.METHOD_INVOKE, invocation);
417417
}
418418

@@ -421,7 +421,7 @@ void methodInvokeShouldNotMatchIntrospectHintOnMethod() throws NoSuchMethodExcep
421421
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.METHOD_INVOKE)
422422
.onInstance(String.class.getMethod("toString")).withArguments("", new Object[0]).build();
423423
hints.reflection().registerType(String.class, typeHint ->
424-
typeHint.withMethod("toString", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INTROSPECT)));
424+
typeHint.withMethod("toString", Collections.emptyList(), ExecutableMode.INTROSPECT));
425425
assertThatInvocationDoesNotMatch(InstrumentedMethod.METHOD_INVOKE, invocation);
426426
}
427427

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,16 @@ public Builder withField(String name, Consumer<FieldHint.Builder> fieldHint) {
173173
return this;
174174
}
175175

176+
/**
177+
* Register the need for reflection on the field with the specified name,
178+
* enabling write access.
179+
* @param name the name of the field
180+
* @return {@code this}, to facilitate method chaining
181+
*/
182+
public Builder withField(String name) {
183+
return withField(name, fieldHint -> {});
184+
}
185+
176186
/**
177187
* Register the need for reflection on the constructor with the specified
178188
* parameter types.
@@ -189,6 +199,27 @@ public Builder withConstructor(List<TypeReference> parameterTypes, Consumer<Exec
189199
return this;
190200
}
191201

202+
/**
203+
* Register the need for reflection on the constructor with the specified
204+
* parameter types, using the specified {@link ExecutableMode}.
205+
* @param parameterTypes the parameter types of the constructor
206+
* @param mode the requested mode
207+
* @return {@code this}, to facilitate method chaining
208+
*/
209+
public Builder withConstructor(List<TypeReference> parameterTypes, ExecutableMode mode) {
210+
return withConstructor(parameterTypes, constructorHint -> constructorHint.withMode(mode));
211+
}
212+
213+
/**
214+
* Register the need for reflection on the constructor with the specified
215+
* parameter types, enabling {@link ExecutableMode#INVOKE}.
216+
* @param parameterTypes the parameter types of the constructor
217+
* @return {@code this}, to facilitate method chaining
218+
*/
219+
public Builder withConstructor(List<TypeReference> parameterTypes) {
220+
return withConstructor(parameterTypes, ExecutableMode.INVOKE);
221+
}
222+
192223
/**
193224
* Register the need for reflection on the method with the specified name
194225
* and parameter types.
@@ -205,6 +236,29 @@ public Builder withMethod(String name, List<TypeReference> parameterTypes, Consu
205236
return this;
206237
}
207238

239+
/**
240+
* Register the need for reflection on the method with the specified name
241+
* and parameter types, using the specified {@link ExecutableMode}.
242+
* @param name the name of the method
243+
* @param parameterTypes the parameter types of the constructor
244+
* @param mode the requested mode
245+
* @return {@code this}, to facilitate method chaining
246+
*/
247+
public Builder withMethod(String name, List<TypeReference> parameterTypes, ExecutableMode mode) {
248+
return withMethod(name, parameterTypes, methodHint -> methodHint.withMode(mode));
249+
}
250+
251+
/**
252+
* Register the need for reflection on the method with the specified name
253+
* and parameter types, enabling {@link ExecutableMode#INVOKE}.
254+
* @param name the name of the method
255+
* @param parameterTypes the parameter types of the constructor
256+
* @return {@code this}, to facilitate method chaining
257+
*/
258+
public Builder withMethod(String name, List<TypeReference> parameterTypes) {
259+
return withMethod(name, parameterTypes, ExecutableMode.INVOKE);
260+
}
261+
208262
/**
209263
* Adds the specified {@linkplain MemberCategory member categories}.
210264
* @param memberCategories the categories to apply

0 commit comments

Comments
 (0)