From 054579a89234d990e209e76783ff7a758a572126 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 3 Oct 2022 14:14:30 +0200 Subject: [PATCH] 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 --- .../factory/aot/BeanRegistrationKey.java | 27 ----------------- .../aot/BeanRegistrationsAotContribution.java | 18 +++-------- .../aot/BeanRegistrationsAotProcessor.java | 5 ++-- ...BeanRegistrationsAotContributionTests.java | 30 ++++--------------- .../BeanRegistrationsAotProcessorTests.java | 2 +- 5 files changed, 13 insertions(+), 69 deletions(-) delete mode 100644 spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationKey.java diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationKey.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationKey.java deleted file mode 100644 index bd99bebca9..0000000000 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationKey.java +++ /dev/null @@ -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) { -} diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java index 1ad7d6a873..a80db112d3 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContribution.java @@ -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; * register bean definitions. * * @author Phillip Webb - * @author Brian Clozel * @since 6.0 * @see BeanRegistrationsAotProcessor */ @@ -47,11 +44,11 @@ class BeanRegistrationsAotContribution private static final String BEAN_FACTORY_PARAMETER_NAME = "beanFactory"; - private final Map registrations; + private final Map registrations; BeanRegistrationsAotContribution( - Map registrations) { + Map registrations) { this.registrations = registrations; } @@ -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 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 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()); diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessor.java index 2ef54446ff..9388d79919 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessor.java @@ -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 public BeanRegistrationsAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(beanFactory); - Map registrations = new LinkedHashMap<>(); + Map 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()) { diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java index acf0fdae6d..577631ff54 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotContributionTests.java @@ -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 { @Test void applyToAppliesContribution() { - Map registrations = new LinkedHashMap<>(); + Map 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 { this.generationContext = new TestGenerationContext( new ClassNameGenerator(TestGenerationContext.TEST_TARGET, "Management")); this.beanFactoryInitializationCode = new MockBeanFactoryInitializationCode(this.generationContext); - Map registrations = new LinkedHashMap<>(); + Map 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 { @Test void applyToCallsRegistrationsWithBeanRegistrationsCode() { List beanRegistrationsCodes = new ArrayList<>(); - Map registrations = new LinkedHashMap<>(); + Map registrations = new LinkedHashMap<>(); RegisteredBean registeredBean = registerBean( new RootBeanDefinition(TestBean.class)); BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator( @@ -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 { assertThat(actual.getMethods()).isNotNull(); } - @Test - void applyToRegisterReflectionHints() { - Map 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); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessorTests.java index 9586c536b9..efd8fcdbda 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanRegistrationsAotProcessorTests.java @@ -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"); } }