use of org.springframework.aot.context.bootstrap.generator.bean.support.MultiStatement in project spring-native by spring-projects-experimental.
the class ScopedProxyBeanRegistrationWriterSupplier method createScopedProxyBeanRegistrationWriter.
@Nullable
private BeanRegistrationWriter createScopedProxyBeanRegistrationWriter(String beanName, BeanDefinition beanDefinition) {
Object targetBeanName = beanDefinition.getPropertyValues().get("targetBeanName");
BeanDefinition targetBeanDefinition = getTargetBeanDefinition(targetBeanName);
if (targetBeanDefinition == null) {
logger.warn("Could not handle " + ScopedProxyFactoryBean.class.getSimpleName() + ": no target bean definition found with name " + targetBeanName);
return null;
}
RootBeanDefinition processedBeanDefinition = new RootBeanDefinition((RootBeanDefinition) beanDefinition);
processedBeanDefinition.setTargetType(targetBeanDefinition.getResolvableType());
processedBeanDefinition.getPropertyValues().removePropertyValue("targetBeanName");
BeanInstanceDescriptor descriptor = BeanInstanceDescriptor.of(ScopedProxyFactoryBean.class).build();
return new DefaultBeanRegistrationWriter(beanName, processedBeanDefinition, descriptor) {
@Override
protected void writeInstanceSupplier(Builder code) {
MultiStatement statements = new MultiStatement();
statements.add("$T factory = new $T()", ScopedProxyFactoryBean.class, ScopedProxyFactoryBean.class);
statements.add("factory.setTargetBeanName($S)", targetBeanName);
statements.add("factory.setBeanFactory(beanFactory)");
statements.add("return factory.getObject()");
code.add(statements.toCodeBlock("() -> "));
}
};
}
use of org.springframework.aot.context.bootstrap.generator.bean.support.MultiStatement in project spring-native by spring-projects-experimental.
the class DefaultBeanRegistrationWriter method handleBeanDefinitionMetadata.
private void handleBeanDefinitionMetadata(Builder code) {
String bdVariable = determineVariableName("bd");
MultiStatement statements = new MultiStatement();
if (this.beanDefinition.isPrimary()) {
statements.add("$L.setPrimary(true)", bdVariable);
}
String scope = this.beanDefinition.getScope();
if (StringUtils.hasText(scope) && !ConfigurableBeanFactory.SCOPE_SINGLETON.equals(scope)) {
statements.add("$L.setScope($S)", bdVariable, scope);
}
String[] dependsOn = this.beanDefinition.getDependsOn();
if (!ObjectUtils.isEmpty(dependsOn)) {
statements.add("$L.setDependsOn($L)", bdVariable, this.parameterWriter.writeParameterValue(dependsOn));
}
if (this.beanDefinition.isLazyInit()) {
statements.add("$L.setLazyInit(true)", bdVariable);
}
if (!this.beanDefinition.isAutowireCandidate()) {
statements.add("$L.setAutowireCandidate(false)", bdVariable);
}
if (this.beanDefinition instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) this.beanDefinition).isSynthetic()) {
statements.add("$L.setSynthetic(true)", bdVariable);
}
if (this.beanDefinition.getRole() != BeanDefinition.ROLE_APPLICATION) {
statements.add("$L.setRole($L)", bdVariable, this.beanDefinition.getRole());
}
Map<Integer, ValueHolder> indexedArgumentValues = this.beanDefinition.getConstructorArgumentValues().getIndexedArgumentValues();
if (!indexedArgumentValues.isEmpty()) {
handleArgumentValues(statements, bdVariable, indexedArgumentValues);
}
if (this.beanDefinition.hasPropertyValues()) {
handlePropertyValues(statements, bdVariable, this.beanDefinition.getPropertyValues());
}
if (this.beanDefinition.attributeNames().length > 0) {
handleAttributes(statements, bdVariable);
}
if (statements.isEmpty()) {
return;
}
code.add(statements.toCodeBlock(".customize((" + bdVariable + ") ->"));
code.add(")");
}
Aggregations