Fix NPE in Constructor predicate

This commit fixes a NullPointerException issue in the constructor hint
predicate. Prior to this commit, a hint for a constructor was directly
looked up and dereferenced a type hint without checking if there was one
first.
This commit is contained in:
Brian Clozel 2022-06-30 18:20:38 +02:00
parent 772d801e48
commit 7a3e8bf648
2 changed files with 7 additions and 1 deletions

View File

@ -286,7 +286,8 @@ public class ReflectionHintsPredicates {
@Override
Predicate<RuntimeHints> exactMatch() {
return hints -> hints.reflection().getTypeHint(this.executable.getDeclaringClass()).constructors().anyMatch(executableHint -> {
return hints -> (hints.reflection().getTypeHint(this.executable.getDeclaringClass()) != null) &&
hints.reflection().getTypeHint(this.executable.getDeclaringClass()).constructors().anyMatch(executableHint -> {
List<TypeReference> parameters = Arrays.stream(this.executable.getParameterTypes()).map(TypeReference::of).toList();
ExecutableHint syntheticHint = ExecutableHint.ofConstructor(parameters)
.setModes(this.executableMode).build();

View File

@ -136,6 +136,11 @@ class ReflectionHintsPredicatesTests {
@Nested
class ReflectionOnConstructor {
@Test
void constructorIntrospectionDoesNotMatchMissingHint() {
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).introspect());
}
@Test
void constructorIntrospectionMatchesConstructorHint() {
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> {