|
|
|
|
@ -1,5 +1,5 @@
@@ -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"); |
|
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
|
@ -18,6 +18,7 @@ package org.springframework.beans.factory.aot;
@@ -18,6 +18,7 @@ package org.springframework.beans.factory.aot;
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream; |
|
|
|
|
import java.io.OutputStream; |
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
import java.nio.charset.Charset; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.time.temporal.ChronoUnit; |
|
|
|
|
@ -28,7 +29,6 @@ import java.util.List;
@@ -28,7 +29,6 @@ import java.util.List;
|
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Set; |
|
|
|
|
import java.util.function.BiConsumer; |
|
|
|
|
import java.util.function.Supplier; |
|
|
|
|
|
|
|
|
|
import javax.lang.model.element.Modifier; |
|
|
|
|
|
|
|
|
|
@ -54,7 +54,7 @@ import org.springframework.core.testfixture.aot.generate.value.ExampleClass;
@@ -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.javapoet.CodeBlock; |
|
|
|
|
import org.springframework.javapoet.MethodSpec; |
|
|
|
|
import org.springframework.javapoet.ParameterizedTypeName; |
|
|
|
|
import org.springframework.util.ReflectionUtils; |
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
|
|
@ -83,14 +83,23 @@ class BeanDefinitionPropertyValueCodeGeneratorDelegatesTests {
@@ -83,14 +83,23 @@ class BeanDefinitionPropertyValueCodeGeneratorDelegatesTests {
|
|
|
|
|
CodeBlock generatedCode = createValueCodeGenerator(generatedClass).generateCode(value); |
|
|
|
|
typeBuilder.set(type -> { |
|
|
|
|
type.addModifiers(Modifier.PUBLIC); |
|
|
|
|
type.addSuperinterface( |
|
|
|
|
ParameterizedTypeName.get(Supplier.class, Object.class)); |
|
|
|
|
type.addMethod(MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC) |
|
|
|
|
type.addMethod(MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC, Modifier.STATIC) |
|
|
|
|
.returns(Object.class).addStatement("return $L", generatedCode).build()); |
|
|
|
|
}); |
|
|
|
|
generationContext.writeGeneratedContent(); |
|
|
|
|
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 |
|
|
|
|
|