Skip to content

Commit

Permalink
Add support for Charset for property value code generation
Browse files Browse the repository at this point in the history
Closes gh-29186
  • Loading branch information
snicoll committed Sep 21, 2022
1 parent 5192d99 commit 84fd942
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -207,6 +209,23 @@ public CodeBlock generateCode(Object value, ResolvableType type) {
}


/**
* {@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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

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;
Expand Down Expand Up @@ -196,6 +198,19 @@ void generateWhenString() {

}

@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 {

Expand Down

0 comments on commit 84fd942

Please sign in to comment.