Skip to content

Commit 7a3e8bf

Browse files
committed
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.
1 parent 772d801 commit 7a3e8bf

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

spring-core/src/main/java/org/springframework/aot/hint/ReflectionHintsPredicates.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ MemberCategory[] getDeclaredMemberCategories() {
286286

287287
@Override
288288
Predicate<RuntimeHints> exactMatch() {
289-
return hints -> hints.reflection().getTypeHint(this.executable.getDeclaringClass()).constructors().anyMatch(executableHint -> {
289+
return hints -> (hints.reflection().getTypeHint(this.executable.getDeclaringClass()) != null) &&
290+
hints.reflection().getTypeHint(this.executable.getDeclaringClass()).constructors().anyMatch(executableHint -> {
290291
List<TypeReference> parameters = Arrays.stream(this.executable.getParameterTypes()).map(TypeReference::of).toList();
291292
ExecutableHint syntheticHint = ExecutableHint.ofConstructor(parameters)
292293
.setModes(this.executableMode).build();

spring-core/src/test/java/org/springframework/aot/hint/ReflectionHintsPredicatesTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ void typeWithAnyMemberCategoryDoesNotMatchOtherCategory() {
136136
@Nested
137137
class ReflectionOnConstructor {
138138

139+
@Test
140+
void constructorIntrospectionDoesNotMatchMissingHint() {
141+
assertPredicateDoesNotMatch(reflection.onConstructor(publicConstructor).introspect());
142+
}
143+
139144
@Test
140145
void constructorIntrospectionMatchesConstructorHint() {
141146
runtimeHints.reflection().registerType(SampleClass.class, typeHint -> typeHint.withConstructor(Collections.emptyList(), constructorHint -> {

0 commit comments

Comments
 (0)