Add support for Charset for property value code generation

Closes gh-29186
This commit is contained in:
Stephane Nicoll 2022-09-21 19:27:05 +02:00
parent 5192d99fa4
commit 84fd942712
2 changed files with 34 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package org.springframework.beans.factory.aot;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -63,6 +64,7 @@ class BeanDefinitionPropertyValueCodeGenerator {
private final List<Delegate> delegates = List.of(
new PrimitiveDelegate(),
new StringDelegate(),
new CharsetDelegate(),
new EnumDelegate(),
new ClassDelegate(),
new ResolvableTypeDelegate(),
@ -207,6 +209,23 @@ class BeanDefinitionPropertyValueCodeGenerator {
}
/**
* {@link Delegate} for {@link Charset} types.
*/
private static class CharsetDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(Object value, ResolvableType type) {
if (value instanceof Charset charset) {
return CodeBlock.of("$T.forName($S)", Charset.class, charset.name());
}
return null;
}
}
/**
* {@link Delegate} for {@link Enum} types.
*/

View File

@ -18,6 +18,8 @@ package org.springframework.beans.factory.aot;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -196,6 +198,19 @@ class BeanDefinitionPropertyValueCodeGeneratorTests {
}
@Nested
class CharsetTests {
@Test
void generateWhenCharset() {
compile(StandardCharsets.UTF_8, (instance, compiled) -> {
assertThat(instance).isEqualTo(Charset.forName("UTF-8"));
assertThat(compiled.getSourceFile()).contains("\"UTF-8\"");
});
}
}
@Nested
class EnumTests {