Add AOT support for TypedStringValue

This commit adds support for TypeStringValue when generating AOT code.
If the value does not specify an explicit type, it's specified as is.
Otherwise, the TypeStringValue instance is restored via the appropriate
code generation.

Closes gh-29074
This commit is contained in:
Stephane Nicoll
2023-09-12 15:43:30 +02:00
committed by Stéphane Nicoll
parent 15c19411f6
commit 37c2619fc4
8 changed files with 197 additions and 4 deletions

View File

@@ -38,6 +38,7 @@ import org.springframework.aot.generate.GeneratedMethods;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanReference;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.ManagedSet;
@@ -88,7 +89,8 @@ class BeanDefinitionPropertyValueCodeGenerator {
new ListDelegate(),
new SetDelegate(),
new MapDelegate(),
new BeanReferenceDelegate()
new BeanReferenceDelegate(),
new TypedStringValueDelegate()
));
}
@@ -569,4 +571,31 @@ class BeanDefinitionPropertyValueCodeGenerator {
}
}
/**
* {@link Delegate} for {@link TypedStringValue} types.
*/
private class TypedStringValueDelegate implements Delegate {
@Override
public CodeBlock generateCode(Object value, ResolvableType type) {
if (value instanceof TypedStringValue typedStringValue) {
return generateTypeStringValueCode(typedStringValue);
}
return null;
}
private CodeBlock generateTypeStringValueCode(TypedStringValue typedStringValue) {
String value = typedStringValue.getValue();
if (typedStringValue.hasTargetType()) {
return CodeBlock.of("new $T($S, $L)", TypedStringValue.class, value,
generateCode(typedStringValue.getTargetType()));
}
return generateCode(value);
}
private CodeBlock generateCode(@Nullable Object value) {
return BeanDefinitionPropertyValueCodeGenerator.this.generateCode(value);
}
}
}

View File

@@ -16,6 +16,8 @@
package org.springframework.beans.factory.config;
import java.util.Comparator;
import org.springframework.beans.BeanMetadataElement;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -35,7 +37,7 @@ import org.springframework.util.ObjectUtils;
* @see BeanDefinition#getPropertyValues
* @see org.springframework.beans.MutablePropertyValues#addPropertyValue
*/
public class TypedStringValue implements BeanMetadataElement {
public class TypedStringValue implements BeanMetadataElement, Comparable<TypedStringValue> {
@Nullable
private String value;
@@ -213,6 +215,10 @@ public class TypedStringValue implements BeanMetadataElement {
return this.dynamic;
}
@Override
public int compareTo(@Nullable TypedStringValue o) {
return Comparator.comparing(TypedStringValue::getValue).compare(this, o);
}
@Override
public boolean equals(@Nullable Object other) {