|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2023 the original author or authors. |
| 2 | + * Copyright 2002-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 |
|
19 | 19 | import java.io.InputStream;
|
20 | 20 | import java.io.OutputStream;
|
| 21 | +import java.lang.reflect.Method; |
21 | 22 | import java.nio.charset.Charset;
|
22 | 23 | import java.nio.charset.StandardCharsets;
|
23 | 24 | import java.time.temporal.ChronoUnit;
|
|
28 | 29 | import java.util.Map;
|
29 | 30 | import java.util.Set;
|
30 | 31 | import java.util.function.BiConsumer;
|
31 |
| -import java.util.function.Supplier; |
32 | 32 |
|
33 | 33 | import javax.lang.model.element.Modifier;
|
34 | 34 |
|
|
54 | 54 | import org.springframework.core.testfixture.aot.generate.value.ExampleClass$$GeneratedBy;
|
55 | 55 | import org.springframework.javapoet.CodeBlock;
|
56 | 56 | import org.springframework.javapoet.MethodSpec;
|
57 |
| -import org.springframework.javapoet.ParameterizedTypeName; |
| 57 | +import org.springframework.util.ReflectionUtils; |
58 | 58 |
|
59 | 59 | import static org.assertj.core.api.Assertions.assertThat;
|
60 | 60 |
|
@@ -83,14 +83,23 @@ private void compile(Object value, BiConsumer<Object, Compiled> result) {
|
83 | 83 | CodeBlock generatedCode = createValueCodeGenerator(generatedClass).generateCode(value);
|
84 | 84 | typeBuilder.set(type -> {
|
85 | 85 | 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) |
89 | 87 | .returns(Object.class).addStatement("return $L", generatedCode).build());
|
90 | 88 | });
|
91 | 89 | generationContext.writeGeneratedContent();
|
92 | 90 | 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 | + } |
94 | 103 | }
|
95 | 104 |
|
96 | 105 | @Nested
|
|
0 commit comments