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);
});
}
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(");
});
}
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);
}
});
}
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);
});
}
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);
});
}
Aggregations