Search in sources :

Example 1 with MultiStatement

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("() -> "));
        }
    };
}
Also used : DefaultBeanRegistrationWriter(org.springframework.aot.context.bootstrap.generator.bean.DefaultBeanRegistrationWriter) MultiStatement(org.springframework.aot.context.bootstrap.generator.bean.support.MultiStatement) Builder(com.squareup.javapoet.CodeBlock.Builder) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanInstanceDescriptor(org.springframework.aot.context.bootstrap.generator.bean.descriptor.BeanInstanceDescriptor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Nullable(org.springframework.lang.Nullable)

Example 2 with MultiStatement

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(")");
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) MultiStatement(org.springframework.aot.context.bootstrap.generator.bean.support.MultiStatement) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)

Aggregations

MultiStatement (org.springframework.aot.context.bootstrap.generator.bean.support.MultiStatement)2 Builder (com.squareup.javapoet.CodeBlock.Builder)1 DefaultBeanRegistrationWriter (org.springframework.aot.context.bootstrap.generator.bean.DefaultBeanRegistrationWriter)1 BeanInstanceDescriptor (org.springframework.aot.context.bootstrap.generator.bean.descriptor.BeanInstanceDescriptor)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 ValueHolder (org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)1 AbstractBeanDefinition (org.springframework.beans.factory.support.AbstractBeanDefinition)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 Nullable (org.springframework.lang.Nullable)1