Skip to content

Commit e416dfd

Browse files
committed
Merge branch '6.0.x'
2 parents 592a307 + 2c15dcc commit e416dfd

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.beans.factory.aot;
1818

19+
import java.lang.reflect.GenericArrayType;
20+
import java.lang.reflect.Type;
1921
import java.util.Map;
2022

2123
import javax.lang.model.element.Modifier;
@@ -30,9 +32,11 @@
3032
import org.springframework.aot.hint.ReflectionHints;
3133
import org.springframework.aot.hint.RuntimeHints;
3234
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
35+
import org.springframework.core.ResolvableType;
3336
import org.springframework.javapoet.ClassName;
3437
import org.springframework.javapoet.CodeBlock;
3538
import org.springframework.javapoet.MethodSpec;
39+
import org.springframework.util.ReflectionUtils;
3640

3741
/**
3842
* AOT contribution from a {@link BeanRegistrationsAotProcessor} used to
@@ -118,6 +122,17 @@ private void generateRegisterHints(RuntimeHints runtimeHints, Map<BeanRegistrati
118122
if (beanClass.isRecord()) {
119123
hints.registerType(beanClass, MemberCategory.INVOKE_DECLARED_METHODS);
120124
}
125+
// Workaround for https://github.com/oracle/graal/issues/6529
126+
ReflectionUtils.doWithMethods(beanClass, method -> {
127+
for (Type type : method.getGenericParameterTypes()) {
128+
if (type instanceof GenericArrayType) {
129+
Class<?> clazz = ResolvableType.forType(type).resolve();
130+
if (clazz != null) {
131+
hints.registerType(clazz);
132+
}
133+
}
134+
}
135+
});
121136
});
122137
}
123138

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3737
import org.springframework.beans.factory.support.RegisteredBean;
3838
import org.springframework.beans.factory.support.RootBeanDefinition;
39+
import org.springframework.beans.testfixture.beans.GenericBeanWithBounds;
40+
import org.springframework.beans.testfixture.beans.Person;
3941
import org.springframework.beans.testfixture.beans.RecordBean;
4042
import org.springframework.beans.testfixture.beans.TestBean;
4143
import org.springframework.beans.testfixture.beans.factory.aot.MockBeanFactoryInitializationCode;
@@ -161,6 +163,16 @@ void applyToRegisterReflectionHintsOnRecordBean() {
161163
.accepts(this.generationContext.getRuntimeHints());
162164
}
163165

166+
@Test
167+
void applyToRegisterReflectionHintsOnGenericBeanWithBounds() {
168+
RegisteredBean registeredBean = registerBean(new RootBeanDefinition(GenericBeanWithBounds.class));
169+
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory,
170+
registeredBean, null, List.of());
171+
BeanRegistrationsAotContribution contribution = createContribution(GenericBeanWithBounds.class, generator);
172+
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
173+
assertThat(reflection().onType(Person[].class)).accepts(this.generationContext.getRuntimeHints());
174+
}
175+
164176
private RegisteredBean registerBean(RootBeanDefinition rootBeanDefinition) {
165177
String beanName = "testBean";
166178
this.beanFactory.registerBeanDefinition(beanName, rootBeanDefinition);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.testfixture.beans;
18+
19+
public class GenericBeanWithBounds<T extends Person> {
20+
21+
@SafeVarargs
22+
public final void process(T... persons) {
23+
}
24+
}

0 commit comments

Comments
 (0)