Browse Source

Remove reflection hints workarounds for GraalVM issues

This commit removes the previously introduced reflection hints that were
working around known issues in GraalVM.
Spring Framework 6.1 will require recent maintenance versions of GraalVM
and should not contribute such hints anymore.

Closes gh-30394
pull/31354/head
Brian Clozel 1 year ago
parent
commit
14c5228288
  1. 19
      spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java
  2. 26
      spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java
  3. 24
      spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBeanWithBounds.java
  4. 19
      spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/RecordBean.java

19
spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java

@ -16,8 +16,6 @@ @@ -16,8 +16,6 @@
package org.springframework.beans.factory.aot;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Type;
import java.util.Map;
import javax.lang.model.element.Modifier;
@ -32,12 +30,10 @@ import org.springframework.aot.hint.MemberCategory; @@ -32,12 +30,10 @@ import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.ResolvableType;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.CodeBlock;
import org.springframework.javapoet.MethodSpec;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* AOT contribution from a {@link BeanRegistrationsAotProcessor} used to
@ -128,21 +124,6 @@ class BeanRegistrationsAotContribution @@ -128,21 +124,6 @@ class BeanRegistrationsAotContribution
}
currentClass = currentClass.getSuperclass();
}
// Workaround for https://github.com/oracle/graal/issues/6510
if (beanClass.isRecord()) {
hints.registerType(beanClass, MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS);
}
// Workaround for https://github.com/oracle/graal/issues/6529
ReflectionUtils.doWithMethods(beanClass, method -> {
for (Type type : method.getGenericParameterTypes()) {
if (type instanceof GenericArrayType) {
Class<?> clazz = ResolvableType.forType(type).resolve();
if (clazz != null) {
hints.registerType(clazz);
}
}
}
});
});
}

26
spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java

@ -37,10 +37,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; @@ -37,10 +37,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.beans.testfixture.beans.GenericBeanWithBounds;
import org.springframework.beans.testfixture.beans.ITestBean;
import org.springframework.beans.testfixture.beans.Person;
import org.springframework.beans.testfixture.beans.RecordBean;
import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.beans.testfixture.beans.factory.aot.MockBeanFactoryInitializationCode;
import org.springframework.core.test.io.support.MockSpringFactoriesLoader;
@ -156,29 +153,6 @@ class BeanRegistrationsAotContributionTests { @@ -156,29 +153,6 @@ class BeanRegistrationsAotContributionTests {
.accepts(this.generationContext.getRuntimeHints());
}
@Test
void applyToRegisterReflectionHintsOnRecordBean() {
RegisteredBean registeredBean = registerBean(new RootBeanDefinition(RecordBean.class));
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory,
registeredBean, null, List.of());
BeanRegistrationsAotContribution contribution = createContribution(RecordBean.class, generator);
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
assertThat(reflection().onType(RecordBean.class)
.withMemberCategories(MemberCategory.INTROSPECT_PUBLIC_METHODS, MemberCategory.INTROSPECT_DECLARED_METHODS,
MemberCategory.INVOKE_PUBLIC_METHODS, MemberCategory.INVOKE_DECLARED_METHODS))
.accepts(this.generationContext.getRuntimeHints());
}
@Test
void applyToRegisterReflectionHintsOnGenericBeanWithBounds() {
RegisteredBean registeredBean = registerBean(new RootBeanDefinition(GenericBeanWithBounds.class));
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory,
registeredBean, null, List.of());
BeanRegistrationsAotContribution contribution = createContribution(GenericBeanWithBounds.class, generator);
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
assertThat(reflection().onType(Person[].class)).accepts(this.generationContext.getRuntimeHints());
}
private RegisteredBean registerBean(RootBeanDefinition rootBeanDefinition) {
String beanName = "testBean";
this.beanFactory.registerBeanDefinition(beanName, rootBeanDefinition);

24
spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/GenericBeanWithBounds.java

@ -1,24 +0,0 @@ @@ -1,24 +0,0 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.testfixture.beans;
public class GenericBeanWithBounds<T extends Person> {
@SafeVarargs
public final void process(T... persons) {
}
}

19
spring-beans/src/testFixtures/java/org/springframework/beans/testfixture/beans/RecordBean.java

@ -1,19 +0,0 @@ @@ -1,19 +0,0 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.testfixture.beans;
public record RecordBean(String name) { }
Loading…
Cancel
Save