Skip to content

Commit

Permalink
Fix native support for generic array parameters
Browse files Browse the repository at this point in the history
This commit adds a workaround for oracle/graal#6529
triggered by b374824.

When the GraalVM fix will have reached a wide enough
audience, it should be removed via spring-projectsgh-30394.

Closes spring-projectsgh-30407
  • Loading branch information
sdeleuze committed May 5, 2023
1 parent 06f16f1 commit 2c15dcc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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;
Expand All @@ -30,9 +32,11 @@
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.ReflectionUtils;

/**
* AOT contribution from a {@link BeanRegistrationsAotProcessor} used to
Expand Down Expand Up @@ -118,6 +122,17 @@ private void generateRegisterHints(RuntimeHints runtimeHints, Map<BeanRegistrati
if (beanClass.isRecord()) {
hints.registerType(beanClass, 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);
}
}
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
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.GenericBeanWithBounds;
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;
Expand Down Expand Up @@ -161,6 +163,16 @@ void applyToRegisterReflectionHintsOnRecordBean() {
.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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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) {
}
}

0 comments on commit 2c15dcc

Please sign in to comment.