Browse Source

Temporarily revert gh-29246

This new hint contribution triggers a GraalVM bug and breaks native
compilation for several samples, we need to temporarily revert this
change until a fix is available in GraalVM.

See gh-29246
pull/29247/head
Brian Clozel 2 years ago
parent
commit
054579a892
  1. 27
      spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationKey.java
  2. 18
      spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java
  3. 5
      spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessor.java
  4. 30
      spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java
  5. 2
      spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessorTests.java

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

@ -1,27 +0,0 @@ @@ -1,27 +0,0 @@
/*
* Copyright 2002-2022 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.factory.aot;
/**
* Record class holding key information for beans registered in a bean factory.
* @param beanName the name of the registered bean
* @param beanClass the type of the registered bean
* @author Brian Clozel
* @since 6.0
*/
record BeanRegistrationKey(String beanName, Class<?> beanClass) {
}

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

@ -26,8 +26,6 @@ import org.springframework.aot.generate.GeneratedMethods; @@ -26,8 +26,6 @@ import org.springframework.aot.generate.GeneratedMethods;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.javapoet.ClassName;
import org.springframework.javapoet.CodeBlock;
@ -38,7 +36,6 @@ import org.springframework.javapoet.MethodSpec; @@ -38,7 +36,6 @@ import org.springframework.javapoet.MethodSpec;
* register bean definitions.
*
* @author Phillip Webb
* @author Brian Clozel
* @since 6.0
* @see BeanRegistrationsAotProcessor
*/
@ -47,11 +44,11 @@ class BeanRegistrationsAotContribution @@ -47,11 +44,11 @@ class BeanRegistrationsAotContribution
private static final String BEAN_FACTORY_PARAMETER_NAME = "beanFactory";
private final Map<BeanRegistrationKey, BeanDefinitionMethodGenerator> registrations;
private final Map<String, BeanDefinitionMethodGenerator> registrations;
BeanRegistrationsAotContribution(
Map<BeanRegistrationKey, BeanDefinitionMethodGenerator> registrations) {
Map<String, BeanDefinitionMethodGenerator> registrations) {
this.registrations = registrations;
}
@ -70,15 +67,8 @@ class BeanRegistrationsAotContribution @@ -70,15 +67,8 @@ class BeanRegistrationsAotContribution
GeneratedMethod generatedMethod = codeGenerator.getMethods().add("registerBeanDefinitions", method ->
generateRegisterMethod(method, generationContext, codeGenerator));
beanFactoryInitializationCode.addInitializer(generatedMethod.toMethodReference());
generateRegisterHints(generationContext.getRuntimeHints(), this.registrations);
}
private void generateRegisterHints(RuntimeHints runtimeHints, Map<BeanRegistrationKey, BeanDefinitionMethodGenerator> registrations) {
registrations.keySet().forEach(beanRegistrationKey -> runtimeHints.reflection()
.registerType(beanRegistrationKey.beanClass(), MemberCategory.INTROSPECT_DECLARED_METHODS));
}
private void generateRegisterMethod(MethodSpec.Builder method,
GenerationContext generationContext,
BeanRegistrationsCode beanRegistrationsCode) {
@ -88,14 +78,14 @@ class BeanRegistrationsAotContribution @@ -88,14 +78,14 @@ class BeanRegistrationsAotContribution
method.addParameter(DefaultListableBeanFactory.class,
BEAN_FACTORY_PARAMETER_NAME);
CodeBlock.Builder code = CodeBlock.builder();
this.registrations.forEach((beanRegistrationKey, beanDefinitionMethodGenerator) -> {
this.registrations.forEach((beanName, beanDefinitionMethodGenerator) -> {
MethodReference beanDefinitionMethod = beanDefinitionMethodGenerator
.generateBeanDefinitionMethod(generationContext,
beanRegistrationsCode);
CodeBlock methodInvocation = beanDefinitionMethod.toInvokeCodeBlock(
ArgumentCodeGenerator.none(), beanRegistrationsCode.getClassName());
code.addStatement("$L.registerBeanDefinition($S, $L)",
BEAN_FACTORY_PARAMETER_NAME, beanRegistrationKey.beanName(),
BEAN_FACTORY_PARAMETER_NAME, beanName,
methodInvocation);
});
method.addCode(code.build());

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

@ -27,7 +27,6 @@ import org.springframework.beans.factory.support.RegisteredBean; @@ -27,7 +27,6 @@ import org.springframework.beans.factory.support.RegisteredBean;
* register beans.
*
* @author Phillip Webb
* @author Brian Clozel
* @since 6.0
*/
class BeanRegistrationsAotProcessor implements BeanFactoryInitializationAotProcessor {
@ -36,13 +35,13 @@ class BeanRegistrationsAotProcessor implements BeanFactoryInitializationAotProce @@ -36,13 +35,13 @@ class BeanRegistrationsAotProcessor implements BeanFactoryInitializationAotProce
public BeanRegistrationsAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) {
BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory =
new BeanDefinitionMethodGeneratorFactory(beanFactory);
Map<BeanRegistrationKey, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
Map<String, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
for (String beanName : beanFactory.getBeanDefinitionNames()) {
RegisteredBean registeredBean = RegisteredBean.of(beanFactory, beanName);
BeanDefinitionMethodGenerator beanDefinitionMethodGenerator = beanDefinitionMethodGeneratorFactory
.getBeanDefinitionMethodGenerator(registeredBean, null);
if (beanDefinitionMethodGenerator != null) {
registrations.put(new BeanRegistrationKey(beanName, registeredBean.getBeanClass()), beanDefinitionMethodGenerator);
registrations.put(beanName, beanDefinitionMethodGenerator);
}
}
if (registrations.isEmpty()) {

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

@ -32,8 +32,6 @@ import org.springframework.aot.generate.ClassNameGenerator; @@ -32,8 +32,6 @@ import org.springframework.aot.generate.ClassNameGenerator;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.generate.MethodReference.ArgumentCodeGenerator;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RegisteredBean;
@ -78,13 +76,13 @@ class BeanRegistrationsAotContributionTests { @@ -78,13 +76,13 @@ class BeanRegistrationsAotContributionTests {
@Test
void applyToAppliesContribution() {
Map<BeanRegistrationKey, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
Map<String, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
RegisteredBean registeredBean = registerBean(
new RootBeanDefinition(TestBean.class));
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
registrations.put(new BeanRegistrationKey(registeredBean.getBeanName(), registeredBean.getBeanClass()), generator);
registrations.put("testBean", generator);
BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution(
registrations);
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
@ -100,13 +98,13 @@ class BeanRegistrationsAotContributionTests { @@ -100,13 +98,13 @@ class BeanRegistrationsAotContributionTests {
this.generationContext = new TestGenerationContext(
new ClassNameGenerator(TestGenerationContext.TEST_TARGET, "Management"));
this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode(this.generationContext);
Map<BeanRegistrationKey, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
Map<String, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
RegisteredBean registeredBean = registerBean(
new RootBeanDefinition(TestBean.class));
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
registrations.put(new BeanRegistrationKey(registeredBean.getBeanName(), registeredBean.getBeanClass()), generator);
registrations.put("testBean", generator);
BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution(
registrations);
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
@ -119,7 +117,7 @@ class BeanRegistrationsAotContributionTests { @@ -119,7 +117,7 @@ class BeanRegistrationsAotContributionTests {
@Test
void applyToCallsRegistrationsWithBeanRegistrationsCode() {
List<BeanRegistrationsCode> beanRegistrationsCodes = new ArrayList<>();
Map<BeanRegistrationKey, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
Map<String, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
RegisteredBean registeredBean = registerBean(
new RootBeanDefinition(TestBean.class));
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
@ -136,7 +134,7 @@ class BeanRegistrationsAotContributionTests { @@ -136,7 +134,7 @@ class BeanRegistrationsAotContributionTests {
}
};
registrations.put(new BeanRegistrationKey(registeredBean.getBeanName(), registeredBean.getBeanClass()), generator);
registrations.put("testBean", generator);
BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution(
registrations);
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
@ -145,22 +143,6 @@ class BeanRegistrationsAotContributionTests { @@ -145,22 +143,6 @@ class BeanRegistrationsAotContributionTests {
assertThat(actual.getMethods()).isNotNull();
}
@Test
void applyToRegisterReflectionHints() {
Map<BeanRegistrationKey, BeanDefinitionMethodGenerator> registrations = new LinkedHashMap<>();
RegisteredBean registeredBean = registerBean(
new RootBeanDefinition(TestBean.class));
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(
this.methodGeneratorFactory, registeredBean, null,
Collections.emptyList());
registrations.put(new BeanRegistrationKey(registeredBean.getBeanName(), registeredBean.getBeanClass()), generator);
BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution(
registrations);
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
assertThat(RuntimeHintsPredicates.reflection().onType(TestBean.class).withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS))
.accepts(this.generationContext.getRuntimeHints());
}
private RegisteredBean registerBean(RootBeanDefinition rootBeanDefinition) {
String beanName = "testBean";
this.beanFactory.registerBeanDefinition(beanName, rootBeanDefinition);

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

@ -50,7 +50,7 @@ class BeanRegistrationsAotProcessorTests { @@ -50,7 +50,7 @@ class BeanRegistrationsAotProcessorTests {
BeanRegistrationsAotContribution contribution = processor
.processAheadOfTime(beanFactory);
assertThat(contribution).extracting("registrations")
.asInstanceOf(InstanceOfAssertFactories.MAP).hasSize(2);
.asInstanceOf(InstanceOfAssertFactories.MAP).containsKeys("b1", "b2");
}
}

Loading…
Cancel
Save