From 7a3e8bf648e6551045a40d7c366ae124061bc430 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Thu, 30 Jun 2022 18:20:38 +0200 Subject: [PATCH] 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. --- .../springframework/aot/hint/ReflectionHintsPredicates.java | 3 ++- .../aot/hint/ReflectionHintsPredicatesTests.java | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java b/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java index db3c9a81505..c74cb78501b 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java @@ -286,7 +286,8 @@ public class ReflectionHintsPredicates { @Override Predicate 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 parameters = Arrays.stream(this.executable.getParameterTypes()).map(TypeReference::of).toList(); ExecutableHint syntheticHint = ExecutableHint.ofConstructor(parameters) .setModes(this.executableMode).build(); diff --git a/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java b/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java index 6953a325928..2ef2c8f0177 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java @@ -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 -> {