Skip to content

Commit b7654dd

Browse files
committed
Make sure the generated values are available from a static context
This commit updates the tests of property values code generated to invoke the generated code from a `static` context. This ensures that the test fails if that's not the case. This commit also updated LinkedHashMap handling that did suffer from that problem. Closes gh-34661
1 parent 4645ce6 commit b7654dd

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorDelegates.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -156,6 +156,8 @@ private CodeBlock generateLinkedHashMapCode(ValueCodeGenerator valueCodeGenerato
156156
.builder(SuppressWarnings.class)
157157
.addMember("value", "{\"rawtypes\", \"unchecked\"}")
158158
.build());
159+
method.addModifiers(javax.lang.model.element.Modifier.PRIVATE,
160+
javax.lang.model.element.Modifier.STATIC);
159161
method.returns(Map.class);
160162
method.addStatement("$T map = new $T($L)", Map.class,
161163
LinkedHashMap.class, map.size());

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorDelegatesTests.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.InputStream;
2020
import java.io.OutputStream;
21+
import java.lang.reflect.Method;
2122
import java.nio.charset.Charset;
2223
import java.nio.charset.StandardCharsets;
2324
import java.time.temporal.ChronoUnit;
@@ -28,7 +29,6 @@
2829
import java.util.Map;
2930
import java.util.Set;
3031
import java.util.function.BiConsumer;
31-
import java.util.function.Supplier;
3232

3333
import javax.lang.model.element.Modifier;
3434

@@ -54,7 +54,7 @@
5454
import org.springframework.core.testfixture.aot.generate.value.ExampleClass$$GeneratedBy;
5555
import org.springframework.javapoet.CodeBlock;
5656
import org.springframework.javapoet.MethodSpec;
57-
import org.springframework.javapoet.ParameterizedTypeName;
57+
import org.springframework.util.ReflectionUtils;
5858

5959
import static org.assertj.core.api.Assertions.assertThat;
6060

@@ -83,14 +83,23 @@ private void compile(Object value, BiConsumer<Object, Compiled> result) {
8383
CodeBlock generatedCode = createValueCodeGenerator(generatedClass).generateCode(value);
8484
typeBuilder.set(type -> {
8585
type.addModifiers(Modifier.PUBLIC);
86-
type.addSuperinterface(
87-
ParameterizedTypeName.get(Supplier.class, Object.class));
88-
type.addMethod(MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC)
86+
type.addMethod(MethodSpec.methodBuilder("get").addModifiers(Modifier.PUBLIC, Modifier.STATIC)
8987
.returns(Object.class).addStatement("return $L", generatedCode).build());
9088
});
9189
generationContext.writeGeneratedContent();
9290
TestCompiler.forSystem().with(generationContext).compile(compiled ->
93-
result.accept(compiled.getInstance(Supplier.class).get(), compiled));
91+
result.accept(getGeneratedCodeReturnValue(compiled, generatedClass), compiled));
92+
}
93+
94+
private static Object getGeneratedCodeReturnValue(Compiled compiled, GeneratedClass generatedClass) {
95+
try {
96+
Object instance = compiled.getInstance(Object.class, generatedClass.getName().reflectionName());
97+
Method get = ReflectionUtils.findMethod(instance.getClass(), "get");
98+
return get.invoke(null);
99+
}
100+
catch (Exception ex) {
101+
throw new RuntimeException("Failed to invoke generated code '%s':".formatted(generatedClass.getName()), ex);
102+
}
94103
}
95104

96105
@Nested

0 commit comments

Comments
 (0)