Add RuntimeHints predicates generator

The `RuntimeHints` API allows to describe hints for the reflection,
proxies and resources behavior at runtime. The need for a particular
behavior can be covered by several types of hints, at different levels.
This knowledge can be important in several cases:

* before contributing additional hints, infrastructure can check if an
  existing hint already covers the behavior
* this can be used in test suites and test infrastructure

This commit adds a new RuntimeHintsPredicates that generates `Predicate`
instances for testing `RuntimeHints` against a desired runtime behavior
for reflection, resources or proxies.

Closes gh-28555
This commit is contained in:
Brian Clozel
2022-06-10 17:04:54 +02:00
parent 100ce9642a
commit 9c9b2356ce
13 changed files with 1318 additions and 55 deletions

View File

@@ -25,8 +25,8 @@ import javax.lang.model.element.Modifier;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsPredicates;
import org.springframework.aot.test.generator.compile.Compiled;
import org.springframework.aot.test.generator.compile.TestCompiler;
import org.springframework.beans.testfixture.beans.TestBean;
@@ -87,11 +87,8 @@ class InjectionCodeGeneratorTests {
TestBean bean = new TestBean();
Field field = ReflectionUtils.findField(bean.getClass(), "age");
this.generator.generateInjectionCode(field, INSTANCE_VARIABLE, CodeBlock.of("$L", 123));
assertThat(this.hints.reflection().getTypeHint(TestBean.class))
.satisfies(hint -> assertThat(hint.fields()).anySatisfy(fieldHint -> {
assertThat(fieldHint.getName()).isEqualTo("age");
assertThat(fieldHint.isAllowWrite()).isTrue();
}));
assertThat(RuntimeHintsPredicates.reflection().onField(TestBean.class, "age").allowWrite())
.accepts(this.hints);
}
@Test
@@ -127,11 +124,8 @@ class InjectionCodeGeneratorTests {
TestBeanWithPrivateMethod bean = new TestBeanWithPrivateMethod();
Method method = ReflectionUtils.findMethod(bean.getClass(), "setAge", int.class);
this.generator.generateInjectionCode(method, INSTANCE_VARIABLE, CodeBlock.of("$L", 123));
assertThat(this.hints.reflection().getTypeHint(TestBeanWithPrivateMethod.class))
.satisfies(hint -> assertThat(hint.methods()).anySatisfy(methodHint -> {
assertThat(methodHint.getName()).isEqualTo("setAge");
assertThat(methodHint.getModes()).contains(ExecutableMode.INVOKE);
}));
assertThat(RuntimeHintsPredicates.reflection()
.onMethod(TestBeanWithPrivateMethod.class, "setAge").invoke()).accepts(this.hints);
}
@SuppressWarnings("unchecked")