Add a convenience method to create a ValueCodeGenerator

This commit makes BeanDefinitionPropertyValueCodeGeneratorDelegates
public and offer a convenience method to create a ValueCodeGenerator
that works will all core delegates.

Closes gh-34761
This commit is contained in:
Stéphane Nicoll 2025-04-15 17:17:45 +02:00
parent 5fb37e3133
commit e3e99ac8a0
3 changed files with 31 additions and 11 deletions

View File

@ -38,7 +38,6 @@ import org.jspecify.annotations.Nullable;
import org.springframework.aot.generate.GeneratedMethods;
import org.springframework.aot.generate.ValueCodeGenerator;
import org.springframework.aot.generate.ValueCodeGenerator.Delegate;
import org.springframework.aot.generate.ValueCodeGeneratorDelegates;
import org.springframework.aot.hint.ExecutableMode;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
@ -103,12 +102,12 @@ class BeanDefinitionPropertiesCodeGenerator {
this.hints = hints;
this.attributeFilter = attributeFilter;
List<Delegate> allDelegates = new ArrayList<>();
allDelegates.add((valueCodeGenerator, value) -> customValueCodeGenerator.apply(PropertyNamesStack.peek(), value));
allDelegates.addAll(additionalDelegates);
allDelegates.addAll(BeanDefinitionPropertyValueCodeGeneratorDelegates.INSTANCES);
allDelegates.addAll(ValueCodeGeneratorDelegates.INSTANCES);
this.valueCodeGenerator = ValueCodeGenerator.with(allDelegates).scoped(generatedMethods);
List<Delegate> customDelegates = new ArrayList<>();
customDelegates.add((valueCodeGenerator, value) ->
customValueCodeGenerator.apply(PropertyNamesStack.peek(), value));
customDelegates.addAll(additionalDelegates);
this.valueCodeGenerator = BeanDefinitionPropertyValueCodeGeneratorDelegates
.createValueCodeGenerator(generatedMethods, customDelegates);
}
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1128

View File

@ -16,6 +16,7 @@
package org.springframework.beans.factory.aot;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
@ -46,7 +47,7 @@ import org.springframework.javapoet.CodeBlock;
* @author Stephane Nicoll
* @since 6.1.2
*/
abstract class BeanDefinitionPropertyValueCodeGeneratorDelegates {
public abstract class BeanDefinitionPropertyValueCodeGeneratorDelegates {
/**
* A list of {@link Delegate} implementations for the following common bean
@ -73,6 +74,26 @@ abstract class BeanDefinitionPropertyValueCodeGeneratorDelegates {
);
/**
* Create a {@link ValueCodeGenerator} instance with both these
* {@link #INSTANCES delegate} and the {@link ValueCodeGeneratorDelegates#INSTANCES
* core delegates}.
* @param generatedMethods the {@link GeneratedMethods} to use
* @param customDelegates additional delegates that should be considered first
* @return a configured value code generator
* @since 7.0
* @see ValueCodeGenerator#add(List)
*/
public static ValueCodeGenerator createValueCodeGenerator(
GeneratedMethods generatedMethods, List<Delegate> customDelegates) {
List<Delegate> allDelegates = new ArrayList<>();
allDelegates.addAll(customDelegates);
allDelegates.addAll(INSTANCES);
allDelegates.addAll(ValueCodeGeneratorDelegates.INSTANCES);
return ValueCodeGenerator.with(allDelegates).scoped(generatedMethods);
}
/**
* {@link Delegate} for {@link ManagedList} types.
*/

View File

@ -22,6 +22,7 @@ import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -71,9 +72,8 @@ import static org.assertj.core.api.Assertions.assertThat;
class BeanDefinitionPropertyValueCodeGeneratorDelegatesTests {
private static ValueCodeGenerator createValueCodeGenerator(GeneratedClass generatedClass) {
return ValueCodeGenerator.with(BeanDefinitionPropertyValueCodeGeneratorDelegates.INSTANCES)
.add(ValueCodeGeneratorDelegates.INSTANCES)
.scoped(generatedClass.getMethods());
return BeanDefinitionPropertyValueCodeGeneratorDelegates.createValueCodeGenerator(
generatedClass.getMethods(), Collections.emptyList());
}
private void compile(Object value, BiConsumer<Object, Compiled> result) {