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
This commit is contained in:
Stephane Nicoll
2022-08-24 16:51:42 +02:00
parent 5aa8583159
commit 7ca57b7e80
6 changed files with 249 additions and 97 deletions

View File

@@ -151,15 +151,15 @@ class InstrumentedMethodTests {
@Test
void classGetConstructorShouldMatchInstrospectConstructorHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(Collections.emptyList(),
constructorHint -> constructorHint.withMode(ExecutableMode.INTROSPECT)));
hints.reflection().registerType(String.class,typeHint ->
typeHint.withConstructor(Collections.emptyList(), ExecutableMode.INTROSPECT));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
}
@Test
void classGetConstructorShouldMatchInvokeConstructorHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(Collections.emptyList(),
constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
hints.reflection().registerType(String.class, typeHint ->
typeHint.withConstructor(Collections.emptyList(),ExecutableMode.INVOKE));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETCONSTRUCTOR, this.stringGetConstructor);
}
@@ -201,15 +201,15 @@ class InstrumentedMethodTests {
@Test
void classGetDeclaredConstructorShouldMatchInstrospectConstructorHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class),
constructorHint -> constructorHint.withMode(ExecutableMode.INTROSPECT)));
hints.reflection().registerType(String.class, typeHint ->
typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class), ExecutableMode.INTROSPECT));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor);
}
@Test
void classGetDeclaredConstructorShouldMatchInvokeConstructorHint() {
hints.reflection().registerType(String.class, typeHint -> typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class),
constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
hints.reflection().registerType(String.class, typeHint ->
typeHint.withConstructor(TypeReference.listOf(byte[].class, byte.class), ExecutableMode.INVOKE));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDCONSTRUCTOR, this.stringGetDeclaredConstructor);
}
@@ -237,7 +237,7 @@ class InstrumentedMethodTests {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE)
.onInstance(String.class.getConstructor()).returnValue("").build();
hints.reflection().registerType(String.class, typeHint ->
typeHint.withConstructor(Collections.emptyList(), constructorHint -> constructorHint.withMode(ExecutableMode.INVOKE)));
typeHint.withConstructor(Collections.emptyList(), ExecutableMode.INVOKE));
assertThatInvocationMatches(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE, invocation);
}
@@ -246,7 +246,7 @@ class InstrumentedMethodTests {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE)
.onInstance(String.class.getConstructor()).returnValue("").build();
hints.reflection().registerType(String.class, typeHint ->
typeHint.withConstructor(Collections.emptyList(), constructorHint -> constructorHint.withMode(ExecutableMode.INTROSPECT)));
typeHint.withConstructor(Collections.emptyList(), ExecutableMode.INTROSPECT));
assertThatInvocationDoesNotMatch(InstrumentedMethod.CONSTRUCTOR_NEWINSTANCE, invocation);
}
@@ -285,7 +285,7 @@ class InstrumentedMethodTests {
void classGetDeclaredMethodShouldMatchIntrospectMethodHint() {
List<TypeReference> parameterTypes = TypeReference.listOf(int.class, float.class);
hints.reflection().registerType(String.class, typeHint ->
typeHint.withMethod("scale", parameterTypes, methodHint -> methodHint.withMode(ExecutableMode.INTROSPECT)));
typeHint.withMethod("scale", parameterTypes, ExecutableMode.INTROSPECT));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
}
@@ -293,7 +293,7 @@ class InstrumentedMethodTests {
void classGetDeclaredMethodShouldMatchInvokeMethodHint() {
List<TypeReference> parameterTypes = TypeReference.listOf(int.class, float.class);
hints.reflection().registerType(String.class, typeHint ->
typeHint.withMethod("scale", parameterTypes, methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
typeHint.withMethod("scale", parameterTypes, ExecutableMode.INVOKE));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETDECLAREDMETHOD, this.stringGetScaleMethod);
}
@@ -378,14 +378,14 @@ class InstrumentedMethodTests {
@Test
void classGetMethodShouldMatchIntrospectMethodHint() {
hints.reflection().registerType(String.class, typeHint ->
typeHint.withMethod("toString", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INTROSPECT)));
typeHint.withMethod("toString", Collections.emptyList(), ExecutableMode.INTROSPECT));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
}
@Test
void classGetMethodShouldMatchInvokeMethodHint() {
hints.reflection().registerType(String.class, typeHint ->
typeHint.withMethod("toString", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
typeHint.withMethod("toString", Collections.emptyList(), ExecutableMode.INVOKE));
assertThatInvocationMatches(InstrumentedMethod.CLASS_GETMETHOD, this.stringGetToStringMethod);
}
@@ -412,7 +412,7 @@ class InstrumentedMethodTests {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.METHOD_INVOKE)
.onInstance(String.class.getMethod("startsWith", String.class)).withArguments("testString", new Object[] { "test" }).build();
hints.reflection().registerType(String.class, typeHint -> typeHint.withMethod("startsWith",
TypeReference.listOf(String.class), methodHint -> methodHint.withMode(ExecutableMode.INVOKE)));
TypeReference.listOf(String.class), ExecutableMode.INVOKE));
assertThatInvocationMatches(InstrumentedMethod.METHOD_INVOKE, invocation);
}
@@ -421,7 +421,7 @@ class InstrumentedMethodTests {
RecordedInvocation invocation = RecordedInvocation.of(InstrumentedMethod.METHOD_INVOKE)
.onInstance(String.class.getMethod("toString")).withArguments("", new Object[0]).build();
hints.reflection().registerType(String.class, typeHint ->
typeHint.withMethod("toString", Collections.emptyList(), methodHint -> methodHint.withMode(ExecutableMode.INTROSPECT)));
typeHint.withMethod("toString", Collections.emptyList(), ExecutableMode.INTROSPECT));
assertThatInvocationDoesNotMatch(InstrumentedMethod.METHOD_INVOKE, invocation);
}