Browse Source

Polishing

pull/28448/head
Sam Brannen 3 years ago
parent
commit
d8f650e3ca
  1. 2
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java
  2. 93
      spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java
  3. 7
      spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorTests.java

2
spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

@ -878,7 +878,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA @@ -878,7 +878,7 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA
/**
* {@link BeanRegistrationAotContribution} to autowire fields and methods.
*/
private class AotContribution implements BeanRegistrationAotContribution {
private static class AotContribution implements BeanRegistrationAotContribution {
private static final String APPLY_METHOD = "apply";

93
spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java

@ -17,10 +17,8 @@ @@ -17,10 +17,8 @@
package org.springframework.beans.factory.aot;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -61,25 +59,25 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -61,25 +59,25 @@ class BeanDefinitionPropertyValueCodeGenerator {
private final MethodGenerator methodGenerator;
private final List<Delegate> delegates;
private final List<Delegate> delegates = List.of(
new PrimitiveDelegate(),
new StringDelegate(),
new EnumDelegate(),
new ClassDelegate(),
new ResolvableTypeDelegate(),
new ArrayDelegate(),
new ManagedListDelegate(),
new ManagedSetDelegate(),
new ManagedMapDelegate(),
new ListDelegate(),
new SetDelegate(),
new MapDelegate(),
new BeanReferenceDelegate()
);
BeanDefinitionPropertyValueCodeGenerator(MethodGenerator methodGenerator) {
this.methodGenerator = methodGenerator;
this.delegates = new ArrayList<>();
this.delegates.add(new PrimitiveDelegate());
this.delegates.add(new StringDelegate());
this.delegates.add(new EnumDelegate());
this.delegates.add(new ClassDelegate());
this.delegates.add(new ResolvableTypeDelegate());
this.delegates.add(new ArrayDelegate());
this.delegates.add(new ManagedListDelegate());
this.delegates.add(new ManagedSetDelegate());
this.delegates.add(new ManagedMapDelegate());
this.delegates.add(new ListDelegate());
this.delegates.add(new SetDelegate());
this.delegates.add(new MapDelegate());
this.delegates.add(new BeanReferenceDelegate());
}
@ -121,20 +119,17 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -121,20 +119,17 @@ class BeanDefinitionPropertyValueCodeGenerator {
*/
private static class PrimitiveDelegate implements Delegate {
private static final Map<Character, String> CHAR_ESCAPES;
static {
Map<Character, String> escapes = new HashMap<>();
escapes.put('\b', "\\b");
escapes.put('\t', "\\t");
escapes.put('\n', "\\n");
escapes.put('\f', "\\f");
escapes.put('\r', "\\r");
escapes.put('\"', "\"");
escapes.put('\'', "\\'");
escapes.put('\\', "\\\\");
CHAR_ESCAPES = Collections.unmodifiableMap(escapes);
}
private static final Map<Character, String> CHAR_ESCAPES = Map.of(
'\b', "\\b",
'\t', "\\t",
'\n', "\\n",
'\f', "\\f",
'\r', "\\r",
'\"', "\"",
'\'', "\\'",
'\\', "\\\\"
);
@Override
@Nullable
@ -258,7 +253,9 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -258,7 +253,9 @@ class BeanDefinitionPropertyValueCodeGenerator {
builder.add("new $T {", type.toClass());
for (int i = 0; i < length; i++) {
Object component = Array.get(value, i);
builder.add((i != 0) ? ", " : "");
if (i != 0) {
builder.add(", ");
}
builder.add("$L", BeanDefinitionPropertyValueCodeGenerator.this
.generateCode(component, componentType));
}
@ -274,8 +271,7 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -274,8 +271,7 @@ class BeanDefinitionPropertyValueCodeGenerator {
/**
* Abstract {@link Delegate} for {@code Collection} types.
*/
private abstract class CollectionDelegate<T extends Collection<?>>
implements Delegate {
private abstract class CollectionDelegate<T extends Collection<?>> implements Delegate {
private final Class<?> collectionType;
@ -301,8 +297,7 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -301,8 +297,7 @@ class BeanDefinitionPropertyValueCodeGenerator {
return null;
}
protected CodeBlock generateCollectionCode(ResolvableType elementType,
T collection) {
protected CodeBlock generateCollectionCode(ResolvableType elementType, T collection) {
return generateCollectionOf(collection, this.collectionType, elementType);
}
@ -315,7 +310,9 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -315,7 +310,9 @@ class BeanDefinitionPropertyValueCodeGenerator {
Object element = iterator.next();
builder.add("$L", BeanDefinitionPropertyValueCodeGenerator.this
.generateCode(element, elementType));
builder.add((!iterator.hasNext()) ? "" : ", ");
if (iterator.hasNext()) {
builder.add(", ");
}
}
builder.add(")");
return builder.build();
@ -382,7 +379,9 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -382,7 +379,9 @@ class BeanDefinitionPropertyValueCodeGenerator {
.generateCode(entry.getKey(), keyType),
BeanDefinitionPropertyValueCodeGenerator.this
.generateCode(entry.getValue(), valueType));
builder.add((!iterator.hasNext()) ? "" : ", ");
if (iterator.hasNext()) {
builder.add(", ");
}
}
builder.add(")");
return builder.build();
@ -413,8 +412,7 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -413,8 +412,7 @@ class BeanDefinitionPropertyValueCodeGenerator {
}
@Override
protected CodeBlock generateCollectionCode(ResolvableType elementType,
Set<?> set) {
protected CodeBlock generateCollectionCode(ResolvableType elementType, Set<?> set) {
if (set instanceof LinkedHashSet) {
return CodeBlock.of("new $T($L)", LinkedHashSet.class,
generateCollectionOf(set, List.class, elementType));
@ -473,7 +471,9 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -473,7 +471,9 @@ class BeanDefinitionPropertyValueCodeGenerator {
else {
builder.add("$T.entry($L,$L)", Map.class, keyCode, valueCode);
}
builder.add((!iterator.hasNext()) ? "" : ", ");
if (iterator.hasNext()) {
builder.add(", ");
}
}
builder.add(")");
return builder.build();
@ -495,12 +495,11 @@ class BeanDefinitionPropertyValueCodeGenerator { @@ -495,12 +495,11 @@ class BeanDefinitionPropertyValueCodeGenerator {
builder.returns(Map.class);
builder.addStatement("$T map = new $T($L)", Map.class,
LinkedHashMap.class, map.size());
map.forEach(
(key, value) -> builder.addStatement("map.put($L, $L)",
BeanDefinitionPropertyValueCodeGenerator.this
.generateCode(key, keyType),
BeanDefinitionPropertyValueCodeGenerator.this
.generateCode(value, valueType)));
map.forEach((key, value) -> builder.addStatement("map.put($L, $L)",
BeanDefinitionPropertyValueCodeGenerator.this
.generateCode(key, keyType),
BeanDefinitionPropertyValueCodeGenerator.this
.generateCode(value, valueType)));
builder.addStatement("return map");
});
return CodeBlock.of("$L()", method.getName());

7
spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorTests.java

@ -471,11 +471,8 @@ class BeanDefinitionPropertyValueCodeGeneratorTests { @@ -471,11 +471,8 @@ class BeanDefinitionPropertyValueCodeGeneratorTests {
@Test
void generatedWhenBeanReference() {
BeanReference beanReference = new RuntimeBeanNameReference("test");
compile(beanReference,
(instance,
compiler) -> assertThat(
((BeanReference) instance).getBeanName())
.isEqualTo(beanReference.getBeanName()));
compile(beanReference, (instance, compiler) ->
assertThat(((BeanReference) instance).getBeanName()).isEqualTo(beanReference.getBeanName()));
}
}

Loading…
Cancel
Save