Polish CodeBlock joining logic

Consistently use `CodeBlock.joining(", ")` when generating code.
This commit is contained in:
Phillip Webb 2022-07-20 10:25:45 +01:00
parent 4560313b9d
commit 3ae1b9ba57
2 changed files with 22 additions and 29 deletions

View File

@ -140,20 +140,21 @@ class BeanDefinitionPropertiesCodeGenerator {
private void addInitDestroyMethods(Builder builder,
AbstractBeanDefinition beanDefinition, @Nullable String[] methodNames, String format) {
if (!ObjectUtils.isEmpty(methodNames)) {
Class<?> beanType = ClassUtils
.getUserClass(beanDefinition.getResolvableType().toClass());
List<String> filteredMethodNames = Arrays.stream(methodNames)
.filter(candidate -> !AbstractBeanDefinition.INFER_METHOD.equals(candidate))
.toList();
if (!ObjectUtils.isEmpty(filteredMethodNames)) {
List<String> filteredMethodNames = (!ObjectUtils.isEmpty(methodNames))
? Arrays.stream(methodNames).filter(this::isNotInferredMethod).toList()
: Collections.emptyList();
if (!filteredMethodNames.isEmpty()) {
Class<?> beanType = ClassUtils.getUserClass(beanDefinition.getResolvableType().toClass());
filteredMethodNames.forEach(methodName -> addInitDestroyHint(beanType, methodName));
CodeBlock arguments = CodeBlock.join(filteredMethodNames.stream()
.map(name -> CodeBlock.of("$S", name)).toList(), ", ");
CodeBlock arguments = filteredMethodNames.stream()
.map(name -> CodeBlock.of("$S", name))
.collect(CodeBlock.joining(", "));
builder.addStatement(format, BEAN_DEFINITION_VARIABLE, arguments);
}
}
private boolean isNotInferredMethod(String candidate) {
return !AbstractBeanDefinition.INFER_METHOD.equals(candidate);
}
private void addInitDestroyHint(Class<?> beanUserClass, String methodName) {
@ -264,12 +265,8 @@ class BeanDefinitionPropertiesCodeGenerator {
}
private CodeBlock toStringVarArgs(String[] strings) {
CodeBlock.Builder builder = CodeBlock.builder();
for (int i = 0; i < strings.length; i++) {
builder.add((i != 0) ? ", " : "");
builder.add("$S", strings[i]);
}
return builder.build();
return Arrays.stream(strings).map(string -> CodeBlock.of("$S", string))
.collect(CodeBlock.joining(","));
}
private Object toRole(int value) {

View File

@ -16,7 +16,7 @@
package org.springframework.beans.factory.aot;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
@ -28,6 +28,7 @@ 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.GeneratedMethod;
import org.springframework.aot.generate.GeneratedMethods;
@ -43,6 +44,7 @@ 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;
/**
* Internal code generator used to generate code for a single value contained in
@ -247,17 +249,11 @@ class BeanDefinitionPropertyValueCodeGenerator {
public CodeBlock generateCode(@Nullable Object value, ResolvableType type) {
if (type.isArray()) {
ResolvableType componentType = type.getComponentType();
int length = Array.getLength(value);
Stream<CodeBlock> elements = Arrays.stream(ObjectUtils.toObjectArray(value)).map(component ->
BeanDefinitionPropertyValueCodeGenerator.this.generateCode(component, componentType));
CodeBlock.Builder builder = CodeBlock.builder();
builder.add("new $T {", type.toClass());
for (int i = 0; i < length; i++) {
Object component = Array.get(value, i);
if (i != 0) {
builder.add(", ");
}
builder.add("$L", BeanDefinitionPropertyValueCodeGenerator.this
.generateCode(component, componentType));
}
builder.add(elements.collect(CodeBlock.joining(", ")));
builder.add("}");
return builder.build();
}