Browse Source

Restore support for package private ReflectiveProcessor

See gh-28975
pull/28987/head
Stephane Nicoll 2 years ago
parent
commit
eac616a83e
  1. 4
      spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrar.java
  2. 33
      spring-core/src/test/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrarTests.java

4
spring-core/src/main/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrar.java

@ -113,7 +113,9 @@ public class ReflectiveRuntimeHintsRegistrar { @@ -113,7 +113,9 @@ public class ReflectiveRuntimeHintsRegistrar {
private ReflectiveProcessor instantiateClass(Class<? extends ReflectiveProcessor> type) {
try {
return type.getDeclaredConstructor().newInstance();
Constructor<? extends ReflectiveProcessor> constructor = type.getDeclaredConstructor();
ReflectionUtils.makeAccessible(constructor);
return constructor.newInstance();
}
catch (Exception ex) {
throw new IllegalStateException("Failed to instantiate " + type, ex);

33
spring-core/src/test/java/org/springframework/aot/hint/annotation/ReflectiveRuntimeHintsRegistrarTests.java

@ -21,9 +21,12 @@ import java.lang.annotation.ElementType; @@ -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 { @@ -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 { @@ -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));
}
}
}

Loading…
Cancel
Save