diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 6407e3bd8d..1a179045c2 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -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"; diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java index c87bfce918..58f67d0b89 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGenerator.java @@ -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 { private final MethodGenerator methodGenerator; - private final List delegates; + private final List 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 { */ private static class PrimitiveDelegate implements Delegate { - private static final Map CHAR_ESCAPES; - - static { - Map 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 CHAR_ESCAPES = Map.of( + '\b', "\\b", + '\t', "\\t", + '\n', "\\n", + '\f', "\\f", + '\r', "\\r", + '\"', "\"", + '\'', "\\'", + '\\', "\\\\" + ); + @Override @Nullable @@ -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 { /** * Abstract {@link Delegate} for {@code Collection} types. */ - private abstract class CollectionDelegate> - implements Delegate { + private abstract class CollectionDelegate> implements Delegate { private final Class collectionType; @@ -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 { 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 { .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 { } @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 { 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 { 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()); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorTests.java index 6acd67d10e..bbdeb883e2 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionPropertyValueCodeGeneratorTests.java @@ -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())); } }