Skip to content

Commit d6e4bd7

Browse files
committed
Resolve AOT factory method target by bean name and reduce reflective method exposure
Includes consistent withShortcut naming and consistent bean definition flag exposure. Removes support for inner classes in alignment with standard core container behavior. Closes gh-32834
1 parent aef5143 commit d6e4bd7

File tree

12 files changed

+447
-537
lines changed

12 files changed

+447
-537
lines changed

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

+26-21
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,34 @@ class BeanDefinitionPropertiesCodeGenerator {
113113

114114
CodeBlock generateCode(RootBeanDefinition beanDefinition) {
115115
CodeBlock.Builder code = CodeBlock.builder();
116-
addStatementForValue(code, beanDefinition, BeanDefinition::isPrimary,
117-
"$L.setPrimary($L)");
118-
addStatementForValue(code, beanDefinition, BeanDefinition::isFallback,
119-
"$L.setFallback($L)");
120116
addStatementForValue(code, beanDefinition, BeanDefinition::getScope,
121117
this::hasScope, "$L.setScope($S)");
118+
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::isBackgroundInit,
119+
"$L.setBackgroundInit($L)");
120+
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::getLazyInit,
121+
"$L.setLazyInit($L)");
122122
addStatementForValue(code, beanDefinition, BeanDefinition::getDependsOn,
123123
this::hasDependsOn, "$L.setDependsOn($L)", this::toStringVarArgs);
124124
addStatementForValue(code, beanDefinition, BeanDefinition::isAutowireCandidate,
125125
"$L.setAutowireCandidate($L)");
126-
addStatementForValue(code, beanDefinition, BeanDefinition::getRole,
127-
this::hasRole, "$L.setRole($L)", this::toRole);
128-
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::getLazyInit,
129-
"$L.setLazyInit($L)");
126+
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::isDefaultCandidate,
127+
"$L.setDefaultCandidate($L)");
128+
addStatementForValue(code, beanDefinition, BeanDefinition::isPrimary,
129+
"$L.setPrimary($L)");
130+
addStatementForValue(code, beanDefinition, BeanDefinition::isFallback,
131+
"$L.setFallback($L)");
130132
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::isSynthetic,
131133
"$L.setSynthetic($L)");
134+
addStatementForValue(code, beanDefinition, BeanDefinition::getRole,
135+
this::hasRole, "$L.setRole($L)", this::toRole);
132136
addInitDestroyMethods(code, beanDefinition, beanDefinition.getInitMethodNames(),
133137
"$L.setInitMethodNames($L)");
134138
addInitDestroyMethods(code, beanDefinition, beanDefinition.getDestroyMethodNames(),
135139
"$L.setDestroyMethodNames($L)");
140+
if (beanDefinition.getFactoryBeanName() != null) {
141+
addStatementForValue(code, beanDefinition, BeanDefinition::getFactoryBeanName,
142+
"$L.setFactoryBeanName(\"$L\")");
143+
}
136144
addConstructorArgumentValues(code, beanDefinition);
137145
addPropertyValues(code, beanDefinition);
138146
addAttributes(code, beanDefinition);
@@ -142,6 +150,7 @@ CodeBlock generateCode(RootBeanDefinition beanDefinition) {
142150

143151
private void addInitDestroyMethods(Builder code, AbstractBeanDefinition beanDefinition,
144152
@Nullable String[] methodNames, String format) {
153+
145154
// For Publisher-based destroy methods
146155
this.hints.reflection().registerType(TypeReference.of("org.reactivestreams.Publisher"));
147156
if (!ObjectUtils.isEmpty(methodNames)) {
@@ -210,7 +219,6 @@ private void addConstructorArgumentValues(CodeBlock.Builder code, BeanDefinition
210219
else if (valueHolder.getType() != null) {
211220
code.addStatement("$L.getConstructorArgumentValues().addGenericArgumentValue($L, $S)",
212221
BEAN_DEFINITION_VARIABLE, valueCode, valueHolder.getType());
213-
214222
}
215223
else {
216224
code.addStatement("$L.getConstructorArgumentValues().addGenericArgumentValue($L)",
@@ -224,7 +232,8 @@ private void addPropertyValues(CodeBlock.Builder code, RootBeanDefinition beanDe
224232
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
225233
if (!propertyValues.isEmpty()) {
226234
Class<?> infrastructureType = getInfrastructureType(beanDefinition);
227-
Map<String, Method> writeMethods = (infrastructureType != Object.class) ? getWriteMethods(infrastructureType) : Collections.emptyMap();
235+
Map<String, Method> writeMethods = (infrastructureType != Object.class ?
236+
getWriteMethods(infrastructureType) : Collections.emptyMap());
228237
for (PropertyValue propertyValue : propertyValues) {
229238
String name = propertyValue.getName();
230239
CodeBlock valueCode = generateValue(name, propertyValue.getValue());
@@ -266,8 +275,8 @@ private void addQualifiers(CodeBlock.Builder code, RootBeanDefinition beanDefini
266275
}
267276

268277
private CodeBlock generateValue(@Nullable String name, @Nullable Object value) {
278+
PropertyNamesStack.push(name);
269279
try {
270-
PropertyNamesStack.push(name);
271280
return this.valueCodeGenerator.generateCode(value);
272281
}
273282
finally {
@@ -308,8 +317,7 @@ private void addAttributes(CodeBlock.Builder code, BeanDefinition beanDefinition
308317
}
309318

310319
private boolean hasScope(String defaultValue, String actualValue) {
311-
return StringUtils.hasText(actualValue) &&
312-
!ConfigurableBeanFactory.SCOPE_SINGLETON.equals(actualValue);
320+
return (StringUtils.hasText(actualValue) && !ConfigurableBeanFactory.SCOPE_SINGLETON.equals(actualValue));
313321
}
314322

315323
private boolean hasDependsOn(String[] defaultValue, String[] actualValue) {
@@ -335,8 +343,7 @@ private Object toRole(int value) {
335343
}
336344

337345
private <B extends BeanDefinition, T> void addStatementForValue(
338-
CodeBlock.Builder code, BeanDefinition beanDefinition,
339-
Function<B, T> getter, String format) {
346+
CodeBlock.Builder code, BeanDefinition beanDefinition, Function<B, T> getter, String format) {
340347

341348
addStatementForValue(code, beanDefinition, getter,
342349
(defaultValue, actualValue) -> !Objects.equals(defaultValue, actualValue), format);
@@ -351,9 +358,8 @@ private <B extends BeanDefinition, T> void addStatementForValue(
351358

352359
@SuppressWarnings("unchecked")
353360
private <B extends BeanDefinition, T> void addStatementForValue(
354-
CodeBlock.Builder code, BeanDefinition beanDefinition,
355-
Function<B, T> getter, BiPredicate<T, T> filter, String format,
356-
Function<T, Object> formatter) {
361+
CodeBlock.Builder code, BeanDefinition beanDefinition, Function<B, T> getter,
362+
BiPredicate<T, T> filter, String format, Function<T, Object> formatter) {
357363

358364
T defaultValue = getter.apply((B) DEFAULT_BEAN_DEFINITION);
359365
T actualValue = getter.apply((B) beanDefinition);
@@ -363,9 +369,8 @@ private <B extends BeanDefinition, T> void addStatementForValue(
363369
}
364370

365371
/**
366-
* Cast the specified {@code valueCode} to the specified {@code castType} if
367-
* the {@code castNecessary} is {@code true}. Otherwise return the valueCode
368-
* as is.
372+
* Cast the specified {@code valueCode} to the specified {@code castType} if the
373+
* {@code castNecessary} is {@code true}. Otherwise, return the valueCode as-is.
369374
* @param castNecessary whether a cast is necessary
370375
* @param castType the type to cast to
371376
* @param valueCode the code for the value

0 commit comments

Comments
 (0)