Add shortcut to register an executable with a mode

See gh-29011
This commit is contained in:
Stephane Nicoll
2022-08-24 15:44:54 +02:00
parent c164b918c0
commit 5aa8583159
8 changed files with 56 additions and 29 deletions

View File

@@ -158,6 +158,17 @@ public class ReflectionHints {
typeHint -> typeHint.withConstructor(mapParameters(constructor), constructorHint));
}
/**
* Register the need for reflection on the specified {@link Constructor},
* using the specified {@link ExecutableMode}.
* @param constructor the constructor that requires reflection
* @param mode the requested mode
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerConstructor(Constructor<?> constructor, ExecutableMode mode) {
return registerConstructor(constructor, constructorHint -> constructorHint.withMode(mode));
}
/**
* Register the need for reflection on the specified {@link Constructor},
* enabling {@link ExecutableMode#INVOKE}.
@@ -165,7 +176,7 @@ public class ReflectionHints {
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerConstructor(Constructor<?> constructor) {
return registerConstructor(constructor, constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE));
return registerConstructor(constructor, ExecutableMode.INVOKE);
}
/**
@@ -179,6 +190,17 @@ public class ReflectionHints {
typeHint -> typeHint.withMethod(method.getName(), mapParameters(method), methodHint));
}
/**
* Register the need for reflection on the specified {@link Method},
* using the specified {@link ExecutableMode}.
* @param method the method that requires reflection
* @param mode the requested mode
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerMethod(Method method, ExecutableMode mode) {
return registerMethod(method, methodHint -> methodHint.withMode(mode));
}
/**
* Register the need for reflection on the specified {@link Method},
* enabling {@link ExecutableMode#INVOKE}.
@@ -186,7 +208,7 @@ public class ReflectionHints {
* @return {@code this}, to facilitate method chaining
*/
public ReflectionHints registerMethod(Method method) {
return registerMethod(method, methodHint -> methodHint.withMode(ExecutableMode.INVOKE));
return registerMethod(method, ExecutableMode.INVOKE);
}
private List<TypeReference> mapParameters(Executable executable) {

View File

@@ -176,6 +176,16 @@ class ReflectionHintsTests {
});
}
@Test
void registerConstructorWithMode() {
this.reflectionHints.registerConstructor(
TestType.class.getDeclaredConstructors()[0], ExecutableMode.INTROSPECT);
assertTestTypeConstructorHint(constructorHint -> {
assertThat(constructorHint.getParameterTypes()).isEmpty();
assertThat(constructorHint.getMode()).isEqualTo(ExecutableMode.INTROSPECT);
});
}
@Test
void registerConstructorWithEmptyCustomizerAppliesConsistentDefault() {
this.reflectionHints.registerConstructor(TestType.class.getDeclaredConstructors()[0],
@@ -219,6 +229,18 @@ class ReflectionHintsTests {
});
}
@Test
void registerMethodWithMode() {
Method method = ReflectionUtils.findMethod(TestType.class, "setName", String.class);
assertThat(method).isNotNull();
this.reflectionHints.registerMethod(method, ExecutableMode.INTROSPECT);
assertTestTypeMethodHints(methodHint -> {
assertThat(methodHint.getName()).isEqualTo("setName");
assertThat(methodHint.getParameterTypes()).containsOnly(TypeReference.of(String.class));
assertThat(methodHint.getMode()).isEqualTo(ExecutableMode.INTROSPECT);
});
}
@Test
void registerMethodWithEmptyCustomizerAppliesConsistentDefault() {
Method method = ReflectionUtils.findMethod(TestType.class, "setName", String.class);