Skip to content

Commit

Permalink
Resolve AOT factory method target by bean name and reduce reflective …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jhoeller committed Sep 24, 2024
1 parent aef5143 commit d6e4bd7
Show file tree
Hide file tree
Showing 12 changed files with 447 additions and 537 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,34 @@ class BeanDefinitionPropertiesCodeGenerator {

CodeBlock generateCode(RootBeanDefinition beanDefinition) {
CodeBlock.Builder code = CodeBlock.builder();
addStatementForValue(code, beanDefinition, BeanDefinition::isPrimary,
"$L.setPrimary($L)");
addStatementForValue(code, beanDefinition, BeanDefinition::isFallback,
"$L.setFallback($L)");
addStatementForValue(code, beanDefinition, BeanDefinition::getScope,
this::hasScope, "$L.setScope($S)");
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::isBackgroundInit,
"$L.setBackgroundInit($L)");
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::getLazyInit,
"$L.setLazyInit($L)");
addStatementForValue(code, beanDefinition, BeanDefinition::getDependsOn,
this::hasDependsOn, "$L.setDependsOn($L)", this::toStringVarArgs);
addStatementForValue(code, beanDefinition, BeanDefinition::isAutowireCandidate,
"$L.setAutowireCandidate($L)");
addStatementForValue(code, beanDefinition, BeanDefinition::getRole,
this::hasRole, "$L.setRole($L)", this::toRole);
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::getLazyInit,
"$L.setLazyInit($L)");
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::isDefaultCandidate,
"$L.setDefaultCandidate($L)");
addStatementForValue(code, beanDefinition, BeanDefinition::isPrimary,
"$L.setPrimary($L)");
addStatementForValue(code, beanDefinition, BeanDefinition::isFallback,
"$L.setFallback($L)");
addStatementForValue(code, beanDefinition, AbstractBeanDefinition::isSynthetic,
"$L.setSynthetic($L)");
addStatementForValue(code, beanDefinition, BeanDefinition::getRole,
this::hasRole, "$L.setRole($L)", this::toRole);
addInitDestroyMethods(code, beanDefinition, beanDefinition.getInitMethodNames(),
"$L.setInitMethodNames($L)");
addInitDestroyMethods(code, beanDefinition, beanDefinition.getDestroyMethodNames(),
"$L.setDestroyMethodNames($L)");
if (beanDefinition.getFactoryBeanName() != null) {
addStatementForValue(code, beanDefinition, BeanDefinition::getFactoryBeanName,
"$L.setFactoryBeanName(\"$L\")");
}
addConstructorArgumentValues(code, beanDefinition);
addPropertyValues(code, beanDefinition);
addAttributes(code, beanDefinition);
Expand All @@ -142,6 +150,7 @@ CodeBlock generateCode(RootBeanDefinition beanDefinition) {

private void addInitDestroyMethods(Builder code, AbstractBeanDefinition beanDefinition,
@Nullable String[] methodNames, String format) {

// For Publisher-based destroy methods
this.hints.reflection().registerType(TypeReference.of("org.reactivestreams.Publisher"));
if (!ObjectUtils.isEmpty(methodNames)) {
Expand Down Expand Up @@ -210,7 +219,6 @@ private void addConstructorArgumentValues(CodeBlock.Builder code, BeanDefinition
else if (valueHolder.getType() != null) {
code.addStatement("$L.getConstructorArgumentValues().addGenericArgumentValue($L, $S)",
BEAN_DEFINITION_VARIABLE, valueCode, valueHolder.getType());

}
else {
code.addStatement("$L.getConstructorArgumentValues().addGenericArgumentValue($L)",
Expand All @@ -224,7 +232,8 @@ private void addPropertyValues(CodeBlock.Builder code, RootBeanDefinition beanDe
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
if (!propertyValues.isEmpty()) {
Class<?> infrastructureType = getInfrastructureType(beanDefinition);
Map<String, Method> writeMethods = (infrastructureType != Object.class) ? getWriteMethods(infrastructureType) : Collections.emptyMap();
Map<String, Method> writeMethods = (infrastructureType != Object.class ?
getWriteMethods(infrastructureType) : Collections.emptyMap());
for (PropertyValue propertyValue : propertyValues) {
String name = propertyValue.getName();
CodeBlock valueCode = generateValue(name, propertyValue.getValue());
Expand Down Expand Up @@ -266,8 +275,8 @@ private void addQualifiers(CodeBlock.Builder code, RootBeanDefinition beanDefini
}

private CodeBlock generateValue(@Nullable String name, @Nullable Object value) {
PropertyNamesStack.push(name);
try {
PropertyNamesStack.push(name);
return this.valueCodeGenerator.generateCode(value);
}
finally {
Expand Down Expand Up @@ -308,8 +317,7 @@ private void addAttributes(CodeBlock.Builder code, BeanDefinition beanDefinition
}

private boolean hasScope(String defaultValue, String actualValue) {
return StringUtils.hasText(actualValue) &&
!ConfigurableBeanFactory.SCOPE_SINGLETON.equals(actualValue);
return (StringUtils.hasText(actualValue) && !ConfigurableBeanFactory.SCOPE_SINGLETON.equals(actualValue));
}

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

private <B extends BeanDefinition, T> void addStatementForValue(
CodeBlock.Builder code, BeanDefinition beanDefinition,
Function<B, T> getter, String format) {
CodeBlock.Builder code, BeanDefinition beanDefinition, Function<B, T> getter, String format) {

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

@SuppressWarnings("unchecked")
private <B extends BeanDefinition, T> void addStatementForValue(
CodeBlock.Builder code, BeanDefinition beanDefinition,
Function<B, T> getter, BiPredicate<T, T> filter, String format,
Function<T, Object> formatter) {
CodeBlock.Builder code, BeanDefinition beanDefinition, Function<B, T> getter,
BiPredicate<T, T> filter, String format, Function<T, Object> formatter) {

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

/**
* Cast the specified {@code valueCode} to the specified {@code castType} if
* the {@code castNecessary} is {@code true}. Otherwise return the valueCode
* as is.
* Cast the specified {@code valueCode} to the specified {@code castType} if the
* {@code castNecessary} is {@code true}. Otherwise, return the valueCode as-is.
* @param castNecessary whether a cast is necessary
* @param castType the type to cast to
* @param valueCode the code for the value
Expand Down
Loading

0 comments on commit d6e4bd7

Please sign in to comment.