Fix RuntimeHintsPredicates matching rules

Prior to this commit, the `RuntimeHintsPredicates` would assume that
registering introspection or invocation hints for "all declared methods"
on a type would also include "all public methods". This is not true, as
the Java reflection API itself behaves differently.
`getDeclaredMethods()` does not return a superset of `getMethods()`, as
the latter can return inherited methods, but not the former.
Same reasoning applies to fields.

This commit fixes the hints predicates to only match if the correct hint
has been registered.

Fixes gh-31224
This commit is contained in:
Brian Clozel
2023-09-15 17:50:53 +02:00
parent 8f130316d2
commit 227049824c
3 changed files with 35 additions and 33 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -329,15 +329,15 @@ class ReflectionHintsPredicatesTests {
}
@Test
void methodIntrospectionMatchesIntrospectDeclaredMethods() {
void methodIntrospectionDoesNotMatchIntrospectDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INTROSPECT_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
}
@Test
void methodIntrospectionMatchesInvokeDeclaredMethods() {
void methodIntrospectionDoesNotMatchInvokeDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").introspect());
}
@Test
@@ -373,9 +373,9 @@ class ReflectionHintsPredicatesTests {
}
@Test
void methodInvocationMatchesInvokeDeclaredMethods() {
void methodInvocationDoesNotMatchInvokeDeclaredMethods() {
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.INVOKE_DECLARED_METHODS);
assertPredicateMatches(reflection.onMethod(SampleClass.class, "publicMethod").invoke());
assertPredicateDoesNotMatch(reflection.onMethod(SampleClass.class, "publicMethod").invoke());
}
@Test
@@ -482,9 +482,9 @@ class ReflectionHintsPredicatesTests {
}
@Test
void fieldReflectionMatchesDeclaredFieldsHint() {
void fieldReflectionDoesNotMatchDeclaredFieldsHint() {
runtimeHints.reflection().registerType(SampleClass.class, MemberCategory.DECLARED_FIELDS);
assertPredicateMatches(reflection.onField(SampleClass.class, "publicField"));
assertPredicateDoesNotMatch(reflection.onField(SampleClass.class, "publicField"));
}
@Test