Search in sources :

Example 1 with MethodReference

use of org.springframework.aot.generate.MethodReference in project spring-framework by spring-projects.

the class ScopedProxyBeanRegistrationCodeFragmentsCustomizerTests method testCompile.

private void testCompile(BiConsumer<DefaultListableBeanFactory, Compiled> result) {
    BeanFactoryInitializationAotContribution contribution = this.processor.processAheadOfTime(this.beanFactory);
    assertThat(contribution).isNotNull();
    contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
    this.generationContext.writeGeneratedContent();
    TestCompiler.forSystem().withFiles(this.generatedFiles).printFiles(System.out).compile(compiled -> {
        MethodReference reference = this.beanFactoryInitializationCode.getInitializers().get(0);
        Object instance = compiled.getInstance(Object.class, reference.getDeclaringClass().toString());
        Method method = ReflectionUtils.findMethod(instance.getClass(), reference.getMethodName(), DefaultListableBeanFactory.class);
        DefaultListableBeanFactory freshBeanFactory = new DefaultListableBeanFactory();
        freshBeanFactory.setBeanClassLoader(compiled.getClassLoader());
        ReflectionUtils.invokeMethod(method, instance, freshBeanFactory);
        result.accept(freshBeanFactory, compiled);
    });
}
Also used : BeanFactoryInitializationAotContribution(org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) MethodReference(org.springframework.aot.generate.MethodReference) Method(java.lang.reflect.Method)

Example 2 with MethodReference

use of org.springframework.aot.generate.MethodReference in project spring-framework by spring-projects.

the class BeanDefinitionMethodGeneratorTests method generateBeanDefinitionMethodWhenHasInstancePostProcessorGeneratesMethod.

@Test
void generateBeanDefinitionMethodWhenHasInstancePostProcessorGeneratesMethod() {
    RegisteredBean registeredBean = registerBean(new RootBeanDefinition(TestBean.class));
    BeanRegistrationAotContribution aotContribution = (generationContext, beanRegistrationCode) -> {
        GeneratedMethod method = beanRegistrationCode.getMethodGenerator().generateMethod("postProcess").using(builder -> builder.addModifiers(Modifier.STATIC).addParameter(RegisteredBean.class, "registeredBean").addParameter(TestBean.class, "testBean").returns(TestBean.class).addCode("return new $T($S);", TestBean.class, "postprocessed"));
        beanRegistrationCode.addInstancePostProcessor(MethodReference.ofStatic(beanRegistrationCode.getClassName(), method.getName().toString()));
    };
    List<BeanRegistrationAotContribution> aotContributions = Collections.singletonList(aotContribution);
    BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory, registeredBean, null, aotContributions, Collections.emptyList());
    MethodReference method = generator.generateBeanDefinitionMethod(this.generationContext, this.beanRegistrationsCode);
    testCompiledResult(method, (actual, compiled) -> {
        assertThat(actual.getBeanClass()).isEqualTo(TestBean.class);
        InstanceSupplier<?> supplier = (InstanceSupplier<?>) actual.getInstanceSupplier();
        try {
            TestBean instance = (TestBean) supplier.get(registeredBean);
            assertThat(instance.getName()).isEqualTo("postprocessed");
        } catch (Exception ex) {
        }
        SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
        assertThat(sourceFile).contains("instanceSupplier.andThen(");
    });
}
Also used : RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ParameterizedTypeName(org.springframework.javapoet.ParameterizedTypeName) BeforeEach(org.junit.jupiter.api.BeforeEach) InMemoryGeneratedFiles(org.springframework.aot.generate.InMemoryGeneratedFiles) GenericBean(org.springframework.beans.testfixture.beans.GenericBean) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) Modifier(javax.lang.model.element.Modifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultGenerationContext(org.springframework.aot.generate.DefaultGenerationContext) SourceFile(org.springframework.aot.test.generator.file.SourceFile) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MethodReference(org.springframework.aot.generate.MethodReference) CompileWithTargetClassAccess(org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess) TestCompiler(org.springframework.aot.test.generator.compile.TestCompiler) InstanceSupplier(org.springframework.beans.factory.support.InstanceSupplier) GenerationContext(org.springframework.aot.generate.GenerationContext) ClassName(org.springframework.javapoet.ClassName) BiConsumer(java.util.function.BiConsumer) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSpec(org.springframework.javapoet.MethodSpec) ResolvableType(org.springframework.core.ResolvableType) GeneratedMethod(org.springframework.aot.generate.GeneratedMethod) MockSpringFactoriesLoader(org.springframework.core.mock.MockSpringFactoriesLoader) AnnotatedBean(org.springframework.beans.testfixture.beans.AnnotatedBean) Predicate(java.util.function.Predicate) JavaFile(org.springframework.javapoet.JavaFile) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) RegisteredBean(org.springframework.beans.factory.support.RegisteredBean) CodeBlock(org.springframework.javapoet.CodeBlock) Test(org.junit.jupiter.api.Test) Compiled(org.springframework.aot.test.generator.compile.Compiled) List(java.util.List) TypeSpec(org.springframework.javapoet.TypeSpec) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Collections(java.util.Collections) RegisteredBean(org.springframework.beans.factory.support.RegisteredBean) GeneratedMethod(org.springframework.aot.generate.GeneratedMethod) InstanceSupplier(org.springframework.beans.factory.support.InstanceSupplier) TestBean(org.springframework.beans.testfixture.beans.TestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MethodReference(org.springframework.aot.generate.MethodReference) SourceFile(org.springframework.aot.test.generator.file.SourceFile) Test(org.junit.jupiter.api.Test)

Example 3 with MethodReference

use of org.springframework.aot.generate.MethodReference in project spring-framework by spring-projects.

the class BeanDefinitionMethodGeneratorTests method generateBeanDefinitionMethodWhenHasInnerBeanPropertyValueGeneratesMethod.

@Test
void generateBeanDefinitionMethodWhenHasInnerBeanPropertyValueGeneratesMethod() {
    RootBeanDefinition innerBeanDefinition = (RootBeanDefinition) BeanDefinitionBuilder.rootBeanDefinition(AnnotatedBean.class).setRole(BeanDefinition.ROLE_INFRASTRUCTURE).setPrimary(true).getBeanDefinition();
    RootBeanDefinition beanDefinition = new RootBeanDefinition(TestBean.class);
    beanDefinition.getPropertyValues().add("name", innerBeanDefinition);
    RegisteredBean registeredBean = registerBean(beanDefinition);
    BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory, registeredBean, null, Collections.emptyList(), Collections.emptyList());
    MethodReference method = generator.generateBeanDefinitionMethod(this.generationContext, this.beanRegistrationsCode);
    testCompiledResult(method, (actual, compiled) -> {
        RootBeanDefinition actualInnerBeanDefinition = (RootBeanDefinition) actual.getPropertyValues().get("name");
        assertThat(actualInnerBeanDefinition.isPrimary()).isTrue();
        assertThat(actualInnerBeanDefinition.getRole()).isEqualTo(BeanDefinition.ROLE_INFRASTRUCTURE);
        Supplier<?> innerInstanceSupplier = actualInnerBeanDefinition.getInstanceSupplier();
        try {
            assertThat(innerInstanceSupplier.get()).isInstanceOf(AnnotatedBean.class);
        } catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    });
}
Also used : RegisteredBean(org.springframework.beans.factory.support.RegisteredBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MethodReference(org.springframework.aot.generate.MethodReference) Test(org.junit.jupiter.api.Test)

Example 4 with MethodReference

use of org.springframework.aot.generate.MethodReference in project spring-framework by spring-projects.

the class BeanDefinitionMethodGeneratorTests method generateBeanDefinitionMethodWhenInnerBeanGeneratesMethod.

@Test
void generateBeanDefinitionMethodWhenInnerBeanGeneratesMethod() {
    RegisteredBean parent = registerBean(new RootBeanDefinition(TestBean.class));
    RegisteredBean innerBean = RegisteredBean.ofInnerBean(parent, new RootBeanDefinition(AnnotatedBean.class));
    BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory, innerBean, "testInnerBean", Collections.emptyList(), Collections.emptyList());
    MethodReference method = generator.generateBeanDefinitionMethod(this.generationContext, this.beanRegistrationsCode);
    testCompiledResult(method, (actual, compiled) -> {
        assertThat(compiled.getSourceFile(".*BeanDefinitions")).contains("Get the inner-bean definition for 'testInnerBean'");
        assertThat(actual).isInstanceOf(RootBeanDefinition.class);
    });
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) RegisteredBean(org.springframework.beans.factory.support.RegisteredBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MethodReference(org.springframework.aot.generate.MethodReference) AnnotatedBean(org.springframework.beans.testfixture.beans.AnnotatedBean) Test(org.junit.jupiter.api.Test)

Example 5 with MethodReference

use of org.springframework.aot.generate.MethodReference in project spring-framework by spring-projects.

the class BeanDefinitionMethodGeneratorTests method generateBeanDefinitionMethodGeneratesMethod.

@Test
void generateBeanDefinitionMethodGeneratesMethod() {
    RegisteredBean registeredBean = registerBean(new RootBeanDefinition(TestBean.class));
    BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator(this.methodGeneratorFactory, registeredBean, null, Collections.emptyList(), Collections.emptyList());
    MethodReference method = generator.generateBeanDefinitionMethod(this.generationContext, this.beanRegistrationsCode);
    testCompiledResult(method, (actual, compiled) -> {
        SourceFile sourceFile = compiled.getSourceFile(".*BeanDefinitions");
        assertThat(sourceFile).contains("Get the bean definition for 'testBean'");
        assertThat(sourceFile).contains("beanType = TestBean.class");
        assertThat(sourceFile).contains("setInstanceSupplier(TestBean::new)");
        assertThat(actual).isInstanceOf(RootBeanDefinition.class);
    });
}
Also used : TestBean(org.springframework.beans.testfixture.beans.TestBean) RegisteredBean(org.springframework.beans.factory.support.RegisteredBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MethodReference(org.springframework.aot.generate.MethodReference) SourceFile(org.springframework.aot.test.generator.file.SourceFile) Test(org.junit.jupiter.api.Test)

Aggregations

MethodReference (org.springframework.aot.generate.MethodReference)16 RegisteredBean (org.springframework.beans.factory.support.RegisteredBean)11 Test (org.junit.jupiter.api.Test)10 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)10 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)6 TestBean (org.springframework.beans.testfixture.beans.TestBean)5 TypeSpec (org.springframework.javapoet.TypeSpec)5 BiConsumer (java.util.function.BiConsumer)4 SourceFile (org.springframework.aot.test.generator.file.SourceFile)4 CodeBlock (org.springframework.javapoet.CodeBlock)4 ArrayList (java.util.ArrayList)3 CompileWithTargetClassAccess (org.springframework.aot.test.generator.compile.CompileWithTargetClassAccess)3 ValueHolder (org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder)3 AnnotatedBean (org.springframework.beans.testfixture.beans.AnnotatedBean)3 Collections (java.util.Collections)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 Predicate (java.util.function.Predicate)2 Supplier (java.util.function.Supplier)2 Modifier (javax.lang.model.element.Modifier)2