Browse Source

Polishing

(cherry picked from commit ec1f5ca600)
pull/33048/head
Juergen Hoeller 2 years ago
parent
commit
5c9f364352
  1. 4
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java
  2. 25
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java
  3. 20
      spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorAotContributionTests.java
  4. 2
      spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java

4
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 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.
@ -225,7 +225,6 @@ class ConfigurationClassEnhancer { @@ -225,7 +225,6 @@ class ConfigurationClassEnhancer {
};
return new TransformingClassGenerator(cg, transformer);
}
}
@ -334,6 +333,7 @@ class ConfigurationClassEnhancer { @@ -334,6 +333,7 @@ class ConfigurationClassEnhancer {
return resolveBeanReference(beanMethod, beanMethodArgs, beanFactory, beanName);
}
@Nullable
private Object resolveBeanReference(Method beanMethod, Object[] beanMethodArgs,
ConfigurableBeanFactory beanFactory, String beanName) {

25
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@ -386,11 +386,11 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -386,11 +386,11 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
});
// Detect any custom bean name generation strategy supplied through the enclosing application context
SingletonBeanRegistry sbr = null;
if (registry instanceof SingletonBeanRegistry _sbr) {
sbr = _sbr;
SingletonBeanRegistry singletonRegistry = null;
if (registry instanceof SingletonBeanRegistry sbr) {
singletonRegistry = sbr;
if (!this.localBeanNameGeneratorSet) {
BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(
BeanNameGenerator generator = (BeanNameGenerator) singletonRegistry.getSingleton(
AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR);
if (generator != null) {
this.componentScanBeanNameGenerator = generator;
@ -451,8 +451,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -451,8 +451,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
while (!candidates.isEmpty());
// Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
if (singletonRegistry != null && !singletonRegistry.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
singletonRegistry.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
}
// Store the PropertySourceDescriptors to contribute them Ahead-of-time if necessary
@ -550,6 +550,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -550,6 +550,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
}
@Override
@Nullable
public PropertyValues postProcessProperties(@Nullable PropertyValues pvs, Object bean, String beanName) {
// Inject the BeanFactory before AutowiredAnnotationBeanPostProcessor's
// postProcessProperties method attempts to autowire other configuration beans.
@ -645,9 +646,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -645,9 +646,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
}
return mappings;
}
}
private static class PropertySourcesAotContribution implements BeanFactoryInitializationAotContribution {
private static final String ENVIRONMENT_VARIABLE = "environment";
@ -743,15 +744,14 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -743,15 +744,14 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
return nonNull.get();
}
}
}
private static class ConfigurationClassProxyBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
private final Class<?> proxyClass;
public ConfigurationClassProxyBeanRegistrationCodeFragments(BeanRegistrationCodeFragments codeFragments,
Class<?> proxyClass) {
public ConfigurationClassProxyBeanRegistrationCodeFragments(BeanRegistrationCodeFragments codeFragments, Class<?> proxyClass) {
super(codeFragments);
this.proxyClass = proxyClass;
}
@ -759,6 +759,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -759,6 +759,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
@Override
public CodeBlock generateSetBeanDefinitionPropertiesCode(GenerationContext generationContext,
BeanRegistrationCode beanRegistrationCode, RootBeanDefinition beanDefinition, Predicate<String> attributeFilter) {
CodeBlock.Builder code = CodeBlock.builder();
code.add(super.generateSetBeanDefinitionPropertiesCode(generationContext,
beanRegistrationCode, beanDefinition, attributeFilter));
@ -771,6 +772,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -771,6 +772,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext,
BeanRegistrationCode beanRegistrationCode, Executable constructorOrFactoryMethod,
boolean allowDirectSupplierShortcut) {
Executable executableToUse = proxyExecutable(generationContext.getRuntimeHints(), constructorOrFactoryMethod);
return super.generateInstanceSupplierCode(generationContext, beanRegistrationCode,
executableToUse, allowDirectSupplierShortcut);
@ -788,7 +790,6 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo @@ -788,7 +790,6 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
}
return userExecutable;
}
}
}

20
spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorAotContributionTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@ -99,8 +99,8 @@ class ConfigurationClassPostProcessorAotContributionTests { @@ -99,8 +99,8 @@ class ConfigurationClassPostProcessorAotContributionTests {
initializer.accept(freshBeanFactory);
freshContext.refresh();
assertThat(freshBeanFactory.getBeanPostProcessors()).filteredOn(ImportAwareAotBeanPostProcessor.class::isInstance)
.singleElement().satisfies(postProcessor -> assertPostProcessorEntry(postProcessor, ImportAwareConfiguration.class,
ImportConfiguration.class));
.singleElement().satisfies(postProcessor ->
assertPostProcessorEntry(postProcessor, ImportAwareConfiguration.class, ImportConfiguration.class));
freshContext.close();
});
}
@ -117,8 +117,8 @@ class ConfigurationClassPostProcessorAotContributionTests { @@ -117,8 +117,8 @@ class ConfigurationClassPostProcessorAotContributionTests {
freshContext.refresh();
TestAwareCallbackBean bean = freshContext.getBean(TestAwareCallbackBean.class);
assertThat(bean.instances).hasSize(2);
assertThat(bean.instances.get(0)).isEqualTo(freshContext);
assertThat(bean.instances.get(1)).isInstanceOfSatisfying(AnnotationMetadata.class, metadata ->
assertThat(bean.instances).element(0).isEqualTo(freshContext);
assertThat(bean.instances).element(1).isInstanceOfSatisfying(AnnotationMetadata.class, metadata ->
assertThat(metadata.getClassName()).isEqualTo(TestAwareCallbackConfiguration.class.getName()));
freshContext.close();
});
@ -236,13 +236,14 @@ class ConfigurationClassPostProcessorAotContributionTests { @@ -236,13 +236,14 @@ class ConfigurationClassPostProcessorAotContributionTests {
}
@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
Assert.notNull(this.metadata, "Metadata was not injected");
}
}
}
@Nested
class PropertySourceTests {
@ -362,9 +363,9 @@ class ConfigurationClassPostProcessorAotContributionTests { @@ -362,9 +363,9 @@ class ConfigurationClassPostProcessorAotContributionTests {
static class PropertySourceWithCustomFactoryConfiguration {
}
}
@Nested
class ConfigurationClassProxyTests {
@ -384,15 +385,14 @@ class ConfigurationClassPostProcessorAotContributionTests { @@ -384,15 +385,14 @@ class ConfigurationClassPostProcessorAotContributionTests {
getRegisteredBean(CglibConfiguration.class))).isNotNull();
}
private RegisteredBean getRegisteredBean(Class<?> bean) {
this.beanFactory.registerBeanDefinition("test", new RootBeanDefinition(bean));
this.processor.postProcessBeanFactory(this.beanFactory);
return RegisteredBean.of(this.beanFactory, "test");
}
}
@Nullable
private BeanFactoryInitializationAotContribution getContribution(Class<?>... types) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
@ -410,8 +410,8 @@ class ConfigurationClassPostProcessorAotContributionTests { @@ -410,8 +410,8 @@ class ConfigurationClassPostProcessorAotContributionTests {
.containsExactly(entry(key.getName(), value.getName()));
}
static class CustomPropertySourcesFactory extends DefaultPropertySourceFactory {
static class CustomPropertySourcesFactory extends DefaultPropertySourceFactory {
}
}

2
spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java

@ -75,7 +75,7 @@ public class ReflectUtils { @@ -75,7 +75,7 @@ public class ReflectUtils {
Throwable throwable = null;
try {
classLoaderDefineClass = ClassLoader.class.getDeclaredMethod("defineClass",
String.class, byte[].class, Integer.TYPE, Integer.TYPE, ProtectionDomain.class);
String.class, byte[].class, Integer.TYPE, Integer.TYPE, ProtectionDomain.class);
}
catch (Throwable t) {
classLoaderDefineClass = null;

Loading…
Cancel
Save