Update runtime hints predicates after GraalVM changes

As of gh-33847, method and field introspection is included by default
when a type is registered for reflection.
Many methods in ReflectionHintsPredicates are now mostly useless as their
default behavior checks for introspection.

This commit deprecates those methods and promotes instead invocation
variants. During the upgrade, developers should replace it for an
`onType` check if only reflection is required. If they were checking for
invocation, they should use the new 'onXInvocation` method.

Closes gh-34239
This commit is contained in:
Brian Clozel
2025-01-13 15:34:34 +01:00
parent 6be811119e
commit d28c0396c9
24 changed files with 224 additions and 177 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
@@ -79,7 +79,7 @@ class CommonAnnotationBeanRegistrationAotContributionTests {
RegisteredBean registeredBean = getAndApplyContribution(
PrivateFieldResourceSample.class);
assertThat(RuntimeHintsPredicates.reflection()
.onField(PrivateFieldResourceSample.class, "one"))
.onType(PrivateFieldResourceSample.class))
.accepts(this.generationContext.getRuntimeHints());
compile(registeredBean, (postProcessor, compiled) -> {
PrivateFieldResourceSample instance = new PrivateFieldResourceSample();
@@ -98,7 +98,7 @@ class CommonAnnotationBeanRegistrationAotContributionTests {
RegisteredBean registeredBean = getAndApplyContribution(
PackagePrivateFieldResourceSample.class);
assertThat(RuntimeHintsPredicates.reflection()
.onField(PackagePrivateFieldResourceSample.class, "one"))
.onType(PackagePrivateFieldResourceSample.class))
.accepts(this.generationContext.getRuntimeHints());
compile(registeredBean, (postProcessor, compiled) -> {
PackagePrivateFieldResourceSample instance = new PackagePrivateFieldResourceSample();
@@ -117,7 +117,7 @@ class CommonAnnotationBeanRegistrationAotContributionTests {
RegisteredBean registeredBean = getAndApplyContribution(
PackagePrivateFieldResourceFromParentSample.class);
assertThat(RuntimeHintsPredicates.reflection()
.onField(PackagePrivateFieldResourceSample.class, "one"))
.onType(PackagePrivateFieldResourceSample.class))
.accepts(this.generationContext.getRuntimeHints());
compile(registeredBean, (postProcessor, compiled) -> {
PackagePrivateFieldResourceFromParentSample instance = new PackagePrivateFieldResourceFromParentSample();
@@ -135,7 +135,7 @@ class CommonAnnotationBeanRegistrationAotContributionTests {
RegisteredBean registeredBean = getAndApplyContribution(
PrivateMethodResourceSample.class);
assertThat(RuntimeHintsPredicates.reflection()
.onMethod(PrivateMethodResourceSample.class, "setOne").invoke())
.onMethodInvocation(PrivateMethodResourceSample.class, "setOne"))
.accepts(this.generationContext.getRuntimeHints());
compile(registeredBean, (postProcessor, compiled) -> {
PrivateMethodResourceSample instance = new PrivateMethodResourceSample();
@@ -153,7 +153,7 @@ class CommonAnnotationBeanRegistrationAotContributionTests {
RegisteredBean registeredBean = getAndApplyContribution(
PrivateMethodResourceWithCustomNameSample.class);
assertThat(RuntimeHintsPredicates.reflection()
.onMethod(PrivateMethodResourceWithCustomNameSample.class, "setText").invoke())
.onMethodInvocation(PrivateMethodResourceWithCustomNameSample.class, "setText"))
.accepts(this.generationContext.getRuntimeHints());
compile(registeredBean, (postProcessor, compiled) -> {
PrivateMethodResourceWithCustomNameSample instance = new PrivateMethodResourceWithCustomNameSample();
@@ -172,7 +172,7 @@ class CommonAnnotationBeanRegistrationAotContributionTests {
RegisteredBean registeredBean = getAndApplyContribution(
PackagePrivateMethodResourceSample.class);
assertThat(RuntimeHintsPredicates.reflection()
.onMethod(PackagePrivateMethodResourceSample.class, "setOne").introspect())
.onType(PackagePrivateMethodResourceSample.class))
.accepts(this.generationContext.getRuntimeHints());
compile(registeredBean, (postProcessor, compiled) -> {
PackagePrivateMethodResourceSample instance = new PackagePrivateMethodResourceSample();
@@ -191,7 +191,7 @@ class CommonAnnotationBeanRegistrationAotContributionTests {
RegisteredBean registeredBean = getAndApplyContribution(
PackagePrivateMethodResourceFromParentSample.class);
assertThat(RuntimeHintsPredicates.reflection()
.onMethod(PackagePrivateMethodResourceSample.class, "setOne"))
.onMethodInvocation(PackagePrivateMethodResourceSample.class, "setOne"))
.accepts(this.generationContext.getRuntimeHints());
compile(registeredBean, (postProcessor, compiled) -> {
PackagePrivateMethodResourceFromParentSample instance = new PackagePrivateMethodResourceFromParentSample();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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.
@@ -17,7 +17,6 @@
package org.springframework.context.aot;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.function.BiConsumer;
@@ -565,8 +564,7 @@ class ApplicationContextAotGeneratorTests {
GenericApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.registerBean(ConfigurableCglibConfiguration.class);
TestGenerationContext generationContext = processAheadOfTime(applicationContext);
Constructor<?> userConstructor = ConfigurableCglibConfiguration.class.getDeclaredConstructors()[0];
assertThat(RuntimeHintsPredicates.reflection().onConstructor(userConstructor).introspect())
assertThat(RuntimeHintsPredicates.reflection().onType(ConfigurableCglibConfiguration.class))
.accepts(generationContext.getRuntimeHints());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 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.
@@ -16,8 +16,6 @@
package org.springframework.context.aot;
import java.lang.reflect.Constructor;
import org.junit.jupiter.api.Test;
import org.springframework.aot.generate.GenerationContext;
@@ -69,8 +67,7 @@ class ReflectiveProcessorBeanFactoryInitializationAotProcessorTests {
void shouldProcessAllBeans() throws NoSuchMethodException {
ReflectionHintsPredicates reflection = RuntimeHintsPredicates.reflection();
process(SampleTypeAnnotatedBean.class, SampleConstructorAnnotatedBean.class);
Constructor<?> constructor = SampleConstructorAnnotatedBean.class.getDeclaredConstructor(String.class);
assertThat(reflection.onType(SampleTypeAnnotatedBean.class).and(reflection.onConstructor(constructor)))
assertThat(reflection.onType(SampleTypeAnnotatedBean.class))
.accepts(this.generationContext.getRuntimeHints());
}