Skip to content

Commit 3c2c9ca

Browse files
committed
Extract value code generation to make it reusable
This commit introduces ValueCodeGenerator and its Delegate interface as a way to generate the code for a particular value. Implementations in spring-core provides support for common value types such a String, primitives, Collections, etc. Additional implementations are provided for code generation of bean definition property values. Closes gh-28999
1 parent 75da9c3 commit 3c2c9ca

File tree

14 files changed

+1479
-833
lines changed

14 files changed

+1479
-833
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
import java.util.function.Predicate;
3535

3636
import org.springframework.aot.generate.GeneratedMethods;
37+
import org.springframework.aot.generate.ValueCodeGenerator;
38+
import org.springframework.aot.generate.ValueCodeGenerator.Delegate;
39+
import org.springframework.aot.generate.ValueCodeGeneratorDelegates;
3740
import org.springframework.aot.hint.ExecutableMode;
3841
import org.springframework.aot.hint.MemberCategory;
3942
import org.springframework.aot.hint.RuntimeHints;
@@ -89,7 +92,7 @@ class BeanDefinitionPropertiesCodeGenerator {
8992

9093
private final Predicate<String> attributeFilter;
9194

92-
private final BeanDefinitionPropertyValueCodeGenerator valueCodeGenerator;
95+
private final ValueCodeGenerator valueCodeGenerator;
9396

9497

9598
BeanDefinitionPropertiesCodeGenerator(RuntimeHints hints,
@@ -98,8 +101,11 @@ class BeanDefinitionPropertiesCodeGenerator {
98101

99102
this.hints = hints;
100103
this.attributeFilter = attributeFilter;
101-
this.valueCodeGenerator = new BeanDefinitionPropertyValueCodeGenerator(generatedMethods,
102-
(object, type) -> customValueCodeGenerator.apply(PropertyNamesStack.peek(), object));
104+
this.valueCodeGenerator = ValueCodeGenerator
105+
.with(new ValueCodeGeneratorDelegateAdapter(customValueCodeGenerator))
106+
.add(BeanDefinitionPropertyValueCodeGeneratorDelegates.INSTANCES)
107+
.add(ValueCodeGeneratorDelegates.INSTANCES)
108+
.scoped(generatedMethods);
103109
}
104110

105111

@@ -366,6 +372,22 @@ private CodeBlock castIfNecessary(boolean castNecessary, Class<?> castType, Code
366372
return (castNecessary ? CodeBlock.of("($T) $L", castType, valueCode) : valueCode);
367373
}
368374

375+
376+
static class ValueCodeGeneratorDelegateAdapter implements Delegate {
377+
378+
private final BiFunction<String, Object, CodeBlock> customValueCodeGenerator;
379+
380+
ValueCodeGeneratorDelegateAdapter(BiFunction<String, Object, CodeBlock> customValueCodeGenerator) {
381+
this.customValueCodeGenerator = customValueCodeGenerator;
382+
}
383+
384+
@Override
385+
public CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
386+
return this.customValueCodeGenerator.apply(PropertyNamesStack.peek(), value);
387+
}
388+
}
389+
390+
369391
static class PropertyNamesStack {
370392

371393
private static final ThreadLocal<ArrayDeque<String>> threadLocal = ThreadLocal.withInitial(ArrayDeque::new);
@@ -384,7 +406,6 @@ static String peek() {
384406
String value = threadLocal.get().peek();
385407
return ("".equals(value) ? null : value);
386408
}
387-
388409
}
389410

390411
}

0 commit comments

Comments
 (0)