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
This commit is contained in:
Stéphane Nicoll
2023-12-13 07:05:50 +01:00
parent 75da9c3c47
commit 3c2c9ca186
14 changed files with 1479 additions and 833 deletions

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.generate;
/**
* Thrown when a {@link ValueCodeGenerator} could not generate the code for a
* given value.
*
* @author Stephane Nicoll
* @since 6.1.2
*/
@SuppressWarnings("serial")
public class UnsupportedTypeValueCodeGenerationException extends ValueCodeGenerationException {
public UnsupportedTypeValueCodeGenerationException(Object value) {
super("Code generation does not support " + value.getClass().getName(), value, null);
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.generate;
import org.springframework.lang.Nullable;
/**
* Thrown when value code generation fails.
*
* @author Stephane Nicoll
* @since 6.1.2
*/
@SuppressWarnings("serial")
public class ValueCodeGenerationException extends RuntimeException {
@Nullable
private final Object value;
protected ValueCodeGenerationException(String message, @Nullable Object value, @Nullable Throwable cause) {
super(message, cause);
this.value = value;
}
public ValueCodeGenerationException(@Nullable Object value, Throwable cause) {
super(buildErrorMessage(value), cause);
this.value = value;
}
private static String buildErrorMessage(@Nullable Object value) {
StringBuilder message = new StringBuilder("Failed to generate code for '");
message.append(value).append("'");
if (value != null) {
message.append(" with type ").append(value.getClass());
}
return message.toString();
}
/**
* Return the value that failed to be generated.
* @return the value
*/
@Nullable
public Object getValue() {
return this.value;
}
}

View File

@@ -0,0 +1,152 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.generate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.javapoet.CodeBlock;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Code generator for a single value. Delegates code generation to a list of
* configurable {@link Delegate} implementations.
*
* @author Stephane Nicoll
* @since 6.1.2
*/
public final class ValueCodeGenerator {
private static final ValueCodeGenerator INSTANCE = new ValueCodeGenerator(ValueCodeGeneratorDelegates.INSTANCES, null);
private static final CodeBlock NULL_VALUE_CODE_BLOCK = CodeBlock.of("null");
private final List<Delegate> delegates;
@Nullable
private final GeneratedMethods generatedMethods;
private ValueCodeGenerator(List<Delegate> delegates, @Nullable GeneratedMethods generatedMethods) {
this.delegates = delegates;
this.generatedMethods = generatedMethods;
}
/**
* Return an instance that provides support for {@linkplain
* ValueCodeGeneratorDelegates#INSTANCES common value types}.
* @return an instance with support for common value types
*/
public static ValueCodeGenerator withDefaults() {
return INSTANCE;
}
/**
* Create an instance with the specified {@link Delegate} implementations.
* @param delegates the delegates to use
* @return an instance with the specified delegates
*/
public static ValueCodeGenerator with(Delegate... delegates) {
return with(Arrays.asList(delegates));
}
/**
* Create an instance with the specified {@link Delegate} implementations.
* @param delegates the delegates to use
* @return an instance with the specified delegates
*/
public static ValueCodeGenerator with(List<Delegate> delegates) {
Assert.notEmpty(delegates, "Delegates must not be empty");
return new ValueCodeGenerator(new ArrayList<>(delegates), null);
}
public ValueCodeGenerator add(List<Delegate> additionalDelegates) {
Assert.notEmpty(additionalDelegates, "AdditionalDelegates must not be empty");
List<Delegate> allDelegates = new ArrayList<>(this.delegates);
allDelegates.addAll(additionalDelegates);
return new ValueCodeGenerator(allDelegates, this.generatedMethods);
}
/**
* Return a {@link ValueCodeGenerator} that is scoped for the specified
* {@link GeneratedMethods}. This allows code generation to generate
* additional methods if necessary, or perform some optimization in
* case of visibility issues.
* @param generatedMethods the generated methods to use
* @return an instance scoped to the specified generated methods
*/
public ValueCodeGenerator scoped(GeneratedMethods generatedMethods) {
return new ValueCodeGenerator(this.delegates, generatedMethods);
}
/**
* Generate the code that represents the specified {@code value}.
* @param value the value to generate
* @return the code that represents the specified value
*/
public CodeBlock generateCode(@Nullable Object value) {
if (value == null) {
return NULL_VALUE_CODE_BLOCK;
}
try {
for (Delegate delegate : this.delegates) {
CodeBlock code = delegate.generateCode(this, value);
if (code != null) {
return code;
}
}
throw new UnsupportedTypeValueCodeGenerationException(value);
}
catch (Exception ex) {
throw new ValueCodeGenerationException(value, ex);
}
}
/**
* Return the {@link GeneratedMethods} that represents the scope
* in which code generated by this instance will be added, or
* {@code null} if no specific scope is set.
* @return the generated methods to use for code generation
*/
@Nullable
public GeneratedMethods getGeneratedMethods() {
return this.generatedMethods;
}
/**
* Strategy interface that can be used to implement code generation for a
* particular value type.
*/
public interface Delegate {
/**
* Generate the code for the specified non-null {@code value}. If this
* instance does not support the value, it should return {@code null} to
* indicate so.
* @param valueCodeGenerator the code generator to use for embedded values
* @param value the value to generate
* @return the code that represents the specified value or {@code null} if
* the specified value is not supported.
*/
@Nullable
CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value);
}
}

View File

@@ -0,0 +1,418 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.aot.generate;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Stream;
import org.springframework.aot.generate.ValueCodeGenerator.Delegate;
import org.springframework.core.ResolvableType;
import org.springframework.javapoet.CodeBlock;
import org.springframework.javapoet.CodeBlock.Builder;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
/**
* Code generator {@link Delegate} for well known value types.
*
* @author Stephane Nicoll
* @since 6.1.2
*/
public abstract class ValueCodeGeneratorDelegates {
/**
* Return the {@link Delegate} implementations for common value types.
* These are:
* <ul>
* <li>Primitive types,</li>
* <li>String,</li>
* <li>Charset,</li>
* <li>Enum,</li>
* <li>Class,</li>
* <li>{@link ResolvableType},</li>
* <li>Array,</li>
* <li>List via {@code List.of},</li>
* <li>Set via {@code Set.of} and support of {@link LinkedHashSet},</li>
* <li>Map via {@code Map.of} or {@code Map.ofEntries}.</li>
* </ul>
* Those implementations do not require the {@link ValueCodeGenerator} to be
* {@linkplain ValueCodeGenerator#scoped(GeneratedMethods) scoped}.
*/
public static final List<Delegate> INSTANCES = List.of(
new PrimitiveDelegate(),
new StringDelegate(),
new CharsetDelegate(),
new EnumDelegate(),
new ClassDelegate(),
new ResolvableTypeDelegate(),
new ArrayDelegate(),
new ListDelegate(),
new SetDelegate(),
new MapDelegate());
/**
* Abstract {@link Delegate} for {@code Collection} types.
* @param <T> type the collection type
*/
public abstract static class CollectionDelegate<T extends Collection<?>> implements Delegate {
private final Class<?> collectionType;
private final CodeBlock emptyResult;
protected CollectionDelegate(Class<?> collectionType, CodeBlock emptyResult) {
this.collectionType = collectionType;
this.emptyResult = emptyResult;
}
@Override
@SuppressWarnings("unchecked")
public CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
if (this.collectionType.isInstance(value)) {
T collection = (T) value;
if (collection.isEmpty()) {
return this.emptyResult;
}
return generateCollectionCode(valueCodeGenerator, collection);
}
return null;
}
protected CodeBlock generateCollectionCode(ValueCodeGenerator valueCodeGenerator, T collection) {
return generateCollectionOf(valueCodeGenerator, collection, this.collectionType);
}
protected final CodeBlock generateCollectionOf(ValueCodeGenerator valueCodeGenerator,
Collection<?> collection, Class<?> collectionType) {
Builder code = CodeBlock.builder();
code.add("$T.of(", collectionType);
Iterator<?> iterator = collection.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
code.add("$L", valueCodeGenerator.generateCode(element));
if (iterator.hasNext()) {
code.add(", ");
}
}
code.add(")");
return code.build();
}
}
/**
* {@link Delegate} for {@link Map} types.
*/
public static class MapDelegate implements Delegate {
private static final CodeBlock EMPTY_RESULT = CodeBlock.of("$T.emptyMap()", Collections.class);
@Override
public CodeBlock generateCode(ValueCodeGenerator valueCodeGenerator, Object value) {
if (value instanceof Map<?, ?> map) {
if (map.isEmpty()) {
return EMPTY_RESULT;
}
return generateMapCode(valueCodeGenerator, map);
}
return null;
}
/**
* Generate the code for a non-empty {@link Map}.
* @param valueCodeGenerator the code generator to use for embedded values
* @param map the value to generate
* @return the code that represents the specified map or {@code null} if
* the specified map is not supported.
*/
@Nullable
protected CodeBlock generateMapCode(ValueCodeGenerator valueCodeGenerator, Map<?, ?> map) {
map = orderForCodeConsistency(map);
boolean useOfEntries = map.size() > 10;
CodeBlock.Builder code = CodeBlock.builder();
code.add("$T" + ((!useOfEntries) ? ".of(" : ".ofEntries("), Map.class);
Iterator<? extends Entry<?, ?>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Entry<?, ?> entry = iterator.next();
CodeBlock keyCode = valueCodeGenerator.generateCode(entry.getKey());
CodeBlock valueCode = valueCodeGenerator.generateCode(entry.getValue());
if (!useOfEntries) {
code.add("$L, $L", keyCode, valueCode);
}
else {
code.add("$T.entry($L,$L)", Map.class, keyCode, valueCode);
}
if (iterator.hasNext()) {
code.add(", ");
}
}
code.add(")");
return code.build();
}
private <K, V> Map<K, V> orderForCodeConsistency(Map<K, V> map) {
try {
return new TreeMap<>(map);
}
catch (ClassCastException ex) {
// If elements are not comparable, just keep the original map
return map;
}
}
}
/**
* {@link Delegate} for {@code primitive} types.
*/
private static class PrimitiveDelegate implements Delegate {
private static final Map<Character, String> CHAR_ESCAPES = Map.of(
'\b', "\\b",
'\t', "\\t",
'\n', "\\n",
'\f', "\\f",
'\r', "\\r",
'\"', "\"",
'\'', "\\'",
'\\', "\\\\"
);
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator codeGenerator, Object value) {
if (value instanceof Boolean || value instanceof Integer) {
return CodeBlock.of("$L", value);
}
if (value instanceof Byte) {
return CodeBlock.of("(byte) $L", value);
}
if (value instanceof Short) {
return CodeBlock.of("(short) $L", value);
}
if (value instanceof Long) {
return CodeBlock.of("$LL", value);
}
if (value instanceof Float) {
return CodeBlock.of("$LF", value);
}
if (value instanceof Double) {
return CodeBlock.of("(double) $L", value);
}
if (value instanceof Character character) {
return CodeBlock.of("'$L'", escape(character));
}
return null;
}
private String escape(char ch) {
String escaped = CHAR_ESCAPES.get(ch);
if (escaped != null) {
return escaped;
}
return (!Character.isISOControl(ch)) ? Character.toString(ch)
: String.format("\\u%04x", (int) ch);
}
}
/**
* {@link Delegate} for {@link String} types.
*/
private static class StringDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator codeGenerator, Object value) {
if (value instanceof String) {
return CodeBlock.of("$S", value);
}
return null;
}
}
/**
* {@link Delegate} for {@link Charset} types.
*/
private static class CharsetDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator codeGenerator, Object value) {
if (value instanceof Charset charset) {
return CodeBlock.of("$T.forName($S)", Charset.class, charset.name());
}
return null;
}
}
/**
* {@link Delegate} for {@link Enum} types.
*/
private static class EnumDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator codeGenerator, Object value) {
if (value instanceof Enum<?> enumValue) {
return CodeBlock.of("$T.$L", enumValue.getDeclaringClass(),
enumValue.name());
}
return null;
}
}
/**
* {@link Delegate} for {@link Class} types.
*/
private static class ClassDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator codeGenerator, Object value) {
if (value instanceof Class<?> clazz) {
return CodeBlock.of("$T.class", ClassUtils.getUserClass(clazz));
}
return null;
}
}
/**
* {@link Delegate} for {@link ResolvableType} types.
*/
private static class ResolvableTypeDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator codeGenerator, Object value) {
if (value instanceof ResolvableType resolvableType) {
return generateCode(resolvableType, false);
}
return null;
}
private static CodeBlock generateCode(ResolvableType resolvableType, boolean allowClassResult) {
if (ResolvableType.NONE.equals(resolvableType)) {
return CodeBlock.of("$T.NONE", ResolvableType.class);
}
Class<?> type = ClassUtils.getUserClass(resolvableType.toClass());
if (resolvableType.hasGenerics() && !resolvableType.hasUnresolvableGenerics()) {
return generateCodeWithGenerics(resolvableType, type);
}
if (allowClassResult) {
return CodeBlock.of("$T.class", type);
}
return CodeBlock.of("$T.forClass($T.class)", ResolvableType.class, type);
}
private static CodeBlock generateCodeWithGenerics(ResolvableType target, Class<?> type) {
ResolvableType[] generics = target.getGenerics();
boolean hasNoNestedGenerics = Arrays.stream(generics).noneMatch(ResolvableType::hasGenerics);
CodeBlock.Builder code = CodeBlock.builder();
code.add("$T.forClassWithGenerics($T.class", ResolvableType.class, type);
for (ResolvableType generic : generics) {
code.add(", $L", generateCode(generic, hasNoNestedGenerics));
}
code.add(")");
return code.build();
}
}
/**
* {@link Delegate} for {@code array} types.
*/
private static class ArrayDelegate implements Delegate {
@Override
@Nullable
public CodeBlock generateCode(ValueCodeGenerator codeGenerator, Object value) {
if (value.getClass().isArray()) {
Stream<CodeBlock> elements = Arrays.stream(ObjectUtils.toObjectArray(value))
.map(codeGenerator::generateCode);
CodeBlock.Builder code = CodeBlock.builder();
code.add("new $T {", value.getClass());
code.add(elements.collect(CodeBlock.joining(", ")));
code.add("}");
return code.build();
}
return null;
}
}
/**
* {@link Delegate} for {@link List} types.
*/
private static class ListDelegate extends CollectionDelegate<List<?>> {
ListDelegate() {
super(List.class, CodeBlock.of("$T.emptyList()", Collections.class));
}
}
/**
* {@link Delegate} for {@link Set} types.
*/
private static class SetDelegate extends CollectionDelegate<Set<?>> {
SetDelegate() {
super(Set.class, CodeBlock.of("$T.emptySet()", Collections.class));
}
@Override
protected CodeBlock generateCollectionCode(ValueCodeGenerator valueCodeGenerator, Set<?> collection) {
if (collection instanceof LinkedHashSet) {
return CodeBlock.of("new $T($L)", LinkedHashSet.class,
generateCollectionOf(valueCodeGenerator, collection, List.class));
}
return super.generateCollectionCode(valueCodeGenerator,
orderForCodeConsistency(collection));
}
private Set<?> orderForCodeConsistency(Set<?> set) {
try {
return new TreeSet<Object>(set);
}
catch (ClassCastException ex) {
// If elements are not comparable, just keep the original set
return set;
}
}
}
}