@ -1,5 +1,5 @@
/ *
/ *
* Copyright 2002 - 2023 the original author or authors .
* Copyright 2002 - 2025 the original author or authors .
*
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* you may not use this file except in compliance with the License .
@ -18,6 +18,7 @@ package org.springframework.beans.factory.aot;
import java.io.InputStream ;
import java.io.InputStream ;
import java.io.OutputStream ;
import java.io.OutputStream ;
import java.lang.reflect.Method ;
import java.nio.charset.Charset ;
import java.nio.charset.Charset ;
import java.nio.charset.StandardCharsets ;
import java.nio.charset.StandardCharsets ;
import java.time.temporal.ChronoUnit ;
import java.time.temporal.ChronoUnit ;
@ -28,7 +29,6 @@ import java.util.List;
import java.util.Map ;
import java.util.Map ;
import java.util.Set ;
import java.util.Set ;
import java.util.function.BiConsumer ;
import java.util.function.BiConsumer ;
import java.util.function.Supplier ;
import javax.lang.model.element.Modifier ;
import javax.lang.model.element.Modifier ;
@ -54,7 +54,7 @@ import org.springframework.core.testfixture.aot.generate.value.ExampleClass;
import org.springframework.core.testfixture.aot.generate.value.ExampleClass $$GeneratedBy ;
import org.springframework.core.testfixture.aot.generate.value.ExampleClass $$GeneratedBy ;
import org.springframework.javapoet.CodeBlock ;
import org.springframework.javapoet.CodeBlock ;
import org.springframework.javapoet.MethodSpec ;
import org.springframework.javapoet.MethodSpec ;
import org.springframework.javapoet.ParameterizedTypeName ;
import org.springframework.util.ReflectionUtils ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThat ;
@ -83,14 +83,23 @@ class BeanDefinitionPropertyValueCodeGeneratorDelegatesTests {
CodeBlock generatedCode = createValueCodeGenerator ( generatedClass ) . generateCode ( value ) ;
CodeBlock generatedCode = createValueCodeGenerator ( generatedClass ) . generateCode ( value ) ;
typeBuilder . set ( type - > {
typeBuilder . set ( type - > {
type . addModifiers ( Modifier . PUBLIC ) ;
type . addModifiers ( Modifier . PUBLIC ) ;
type . addSuperinterface (
type . addMethod ( MethodSpec . methodBuilder ( "get" ) . addModifiers ( Modifier . PUBLIC , Modifier . STATIC )
ParameterizedTypeName . get ( Supplier . class , Object . class ) ) ;
type . addMethod ( MethodSpec . methodBuilder ( "get" ) . addModifiers ( Modifier . PUBLIC )
. returns ( Object . class ) . addStatement ( "return $L" , generatedCode ) . build ( ) ) ;
. returns ( Object . class ) . addStatement ( "return $L" , generatedCode ) . build ( ) ) ;
} ) ;
} ) ;
generationContext . writeGeneratedContent ( ) ;
generationContext . writeGeneratedContent ( ) ;
TestCompiler . forSystem ( ) . with ( generationContext ) . compile ( compiled - >
TestCompiler . forSystem ( ) . with ( generationContext ) . compile ( compiled - >
result . accept ( compiled . getInstance ( Supplier . class ) . get ( ) , compiled ) ) ;
result . accept ( getGeneratedCodeReturnValue ( compiled , generatedClass ) , compiled ) ) ;
}
private static Object getGeneratedCodeReturnValue ( Compiled compiled , GeneratedClass generatedClass ) {
try {
Object instance = compiled . getInstance ( Object . class , generatedClass . getName ( ) . reflectionName ( ) ) ;
Method get = ReflectionUtils . findMethod ( instance . getClass ( ) , "get" ) ;
return get . invoke ( null ) ;
}
catch ( Exception ex ) {
throw new RuntimeException ( "Failed to invoke generated code '%s':" . formatted ( generatedClass . getName ( ) ) , ex ) ;
}
}
}
@Nested
@Nested