From eac616a83e8cd9fda3a0150ed3b9b3c323006c78 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 18 Aug 2022 14:27:59 +0200 Subject: [PATCH] Restore support for package private ReflectiveProcessor See gh-28975 --- .../ReflectiveRuntimeHintsRegistrar.java | 4 ++- .../ReflectiveRuntimeHintsRegistrarTests.java | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrar.java b/spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrar.java index 88febd09059..6c24c8e76e2 100644 --- a/spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrar.java +++ b/spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrar.java @@ -113,7 +113,9 @@ public class ReflectiveRuntimeHintsRegistrar { private ReflectiveProcessor instantiateClass(Class type) { try { - return type.getDeclaredConstructor().newInstance(); + Constructor constructor = type.getDeclaredConstructor(); + ReflectionUtils.makeAccessible(constructor); + return constructor.newInstance(); } catch (Exception ex) { throw new IllegalStateException("Failed to instantiate " + type, ex); diff --git a/spring-core/src/test/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrarTests.java b/spring-core/src/test/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrarTests.java index fc00dfa04e1..30ed0542fc7 100644 --- a/spring-core/src/test/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrarTests.java +++ b/spring-core/src/test/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrarTests.java @@ -21,9 +21,12 @@ import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import java.lang.reflect.Method; import org.junit.jupiter.api.Test; +import org.springframework.aot.hint.MemberCategory; +import org.springframework.aot.hint.ReflectionHints; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.TypeReference; import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; @@ -121,6 +124,16 @@ class ReflectiveRuntimeHintsRegistrarTests { .satisfies(methodHint -> assertThat(methodHint.getName()).isEqualTo("managed"))); } + @Test + void shouldInvokeCustomProcessor() { + process(SampleCustomProcessor.class); + assertThat(RuntimeHintsPredicates.reflection() + .onMethod(SampleCustomProcessor.class, "managed")).accepts(this.runtimeHints); + assertThat(RuntimeHintsPredicates.reflection().onType(String.class) + .withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.runtimeHints); + + } + private void process(Class beanClass) { this.registrar.registerRuntimeHints(this.runtimeHints, beanClass); } @@ -252,4 +265,24 @@ class ReflectiveRuntimeHintsRegistrarTests { } } + static class SampleCustomProcessor { + + @Reflective(TestReflectiveProcessor.class) + public String managed() { + return "test"; + } + + } + + private static class TestReflectiveProcessor extends SimpleReflectiveProcessor { + + @Override + protected void registerMethodHint(ReflectionHints hints, Method method) { + super.registerMethodHint(hints, method); + hints.registerType(method.getReturnType(), type -> + type.withMembers(MemberCategory.INVOKE_DECLARED_METHODS)); + } + + } + }