diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedList.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedList.java index 7a282ed7fc..2814670194 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedList.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedList.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -17,6 +17,7 @@ package org.springframework.beans.factory.support; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.springframework.beans.BeanMetadataElement; @@ -53,6 +54,20 @@ public class ManagedList extends ArrayList implements Mergeable, BeanMetad } + /** + * Return a new instance containing an arbitrary number of elements. + * @param elements the elements to be contained in the list + * @param the {@code List}'s element type + * @return a {@code List} containing the specified elements + * @since 5.3.16 + */ + @SuppressWarnings("unchecked") + public static ManagedList of(E... elements) { + ManagedList list = new ManagedList<>(); + list.addAll(Arrays.asList(elements)); + return list; + } + /** * Set the configuration source {@code Object} for this metadata element. *

The exact type of the object will depend on the configuration mechanism used. diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedMap.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedMap.java index 55b76f0317..acd80074bf 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedMap.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedMap.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -18,6 +18,7 @@ package org.springframework.beans.factory.support; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; import org.springframework.beans.BeanMetadataElement; import org.springframework.beans.Mergeable; @@ -56,6 +57,25 @@ public class ManagedMap extends LinkedHashMap implements Mergeable, } + /** + * Return a new instance containing keys and values extracted from the + * given entries. The entries themselves are not stored in the map. + * @param entries {@code Map.Entry}s containing the keys and values + * from which the map is populated + * @param the {@code Map}'s key type + * @param the {@code Map}'s value type + * @return a {@code Map} containing the specified mappings + * @since 5.3.16 + */ + @SuppressWarnings("unchecked") + public static ManagedMap ofEntries(Entry... entries) { + ManagedMap map = new ManagedMap<>(); + for (Entry entry : entries) { + map.put(entry.getKey(), entry.getValue()); + } + return map; + } + /** * Set the configuration source {@code Object} for this metadata element. *

The exact type of the object will depend on the configuration mechanism used. diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedSet.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedSet.java index 7f84ec7f44..f5c7246237 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedSet.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ManagedSet.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -16,6 +16,7 @@ package org.springframework.beans.factory.support; +import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; @@ -52,6 +53,20 @@ public class ManagedSet extends LinkedHashSet implements Mergeable, BeanMe } + /** + * Return a new instance containing an arbitrary number of elements. + * @param elements the elements to be contained in the set + * @param the {@code Set}'s element type + * @return a {@code Set} containing the specified elements + * @since 5.3.16 + */ + @SuppressWarnings("unchecked") + public static ManagedSet of(E... elements) { + ManagedSet set = new ManagedSet<>(); + set.addAll(Arrays.asList(elements)); + return set; + } + /** * Set the configuration source {@code Object} for this metadata element. *

The exact type of the object will depend on the configuration mechanism used. diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index d76579eaf2..cbf9983dc0 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -2298,9 +2298,7 @@ class DefaultListableBeanFactoryTests { @Test void prototypeWithArrayConversionForConstructor() { - List list = new ManagedList<>(); - list.add("myName"); - list.add("myBeanName"); + List list = ManagedList.of("myName", "myBeanName"); RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class); bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.getConstructorArgumentValues().addGenericArgumentValue(list); @@ -2316,9 +2314,7 @@ class DefaultListableBeanFactoryTests { @Test void prototypeWithArrayConversionForFactoryMethod() { - List list = new ManagedList<>(); - list.add("myName"); - list.add("myBeanName"); + List list = ManagedList.of("myName", "myBeanName"); RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class); bd.setScope(BeanDefinition.SCOPE_PROTOTYPE); bd.setFactoryMethodName("create"); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java index 29ecb68699..e51017df5d 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyResourceConfigurerTests.java @@ -16,6 +16,7 @@ package org.springframework.beans.factory.config; +import java.util.AbstractMap.SimpleEntry; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -357,22 +358,18 @@ public class PropertyResourceConfigurerTests { MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("stringArray", new String[] {"${os.name}", "${age}"}); - List friends = new ManagedList<>(); - friends.add("na${age}me"); - friends.add(new RuntimeBeanReference("${ref}")); + List friends = ManagedList.of("na${age}me", new RuntimeBeanReference("${ref}")); pvs.add("friends", friends); - Set someSet = new ManagedSet<>(); - someSet.add("na${age}me"); - someSet.add(new RuntimeBeanReference("${ref}")); - someSet.add(new TypedStringValue("${age}", Integer.class)); + Set someSet = ManagedSet.of("na${age}me", + new RuntimeBeanReference("${ref}"), new TypedStringValue("${age}", Integer.class)); pvs.add("someSet", someSet); - Map someMap = new ManagedMap<>(); - someMap.put(new TypedStringValue("key${age}"), new TypedStringValue("${age}")); - someMap.put(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}")); - someMap.put("key1", new RuntimeBeanReference("${ref}")); - someMap.put("key2", "${age}name"); + Map someMap = ManagedMap.ofEntries( + new SimpleEntry<>(new TypedStringValue("key${age}"), new TypedStringValue("${age}")), + new SimpleEntry<>(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}")), + new SimpleEntry<>("key1", new RuntimeBeanReference("${ref}")), + new SimpleEntry<>("key2", "${age}name")); MutablePropertyValues innerPvs = new MutablePropertyValues(); innerPvs.add("country", "${os.name}"); RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java index 9bf6576d51..420226d173 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedListTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -34,11 +34,8 @@ public class ManagedListTests { @Test public void mergeSunnyDay() { - ManagedList parent = new ManagedList(); - parent.add("one"); - parent.add("two"); - ManagedList child = new ManagedList(); - child.add("three"); + ManagedList parent = ManagedList.of("one", "two"); + ManagedList child = ManagedList.of("three"); child.setMergeEnabled(true); List mergedList = child.merge(parent); assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3); @@ -46,8 +43,7 @@ public class ManagedListTests { @Test public void mergeWithNullParent() { - ManagedList child = new ManagedList(); - child.add("one"); + ManagedList child = ManagedList.of("one"); child.setMergeEnabled(true); assertThat(child.merge(null)).isSameAs(child); } @@ -61,8 +57,7 @@ public class ManagedListTests { @Test public void mergeWithNonCompatibleParentType() { - ManagedList child = new ManagedList(); - child.add("one"); + ManagedList child = ManagedList.of("one"); child.setMergeEnabled(true); assertThatIllegalArgumentException().isThrownBy(() -> child.merge("hello")); @@ -70,9 +65,7 @@ public class ManagedListTests { @Test public void mergeEmptyChild() { - ManagedList parent = new ManagedList(); - parent.add("one"); - parent.add("two"); + ManagedList parent = ManagedList.of("one", "two"); ManagedList child = new ManagedList(); child.setMergeEnabled(true); List mergedList = child.merge(parent); @@ -82,11 +75,8 @@ public class ManagedListTests { @Test public void mergeChildValuesOverrideTheParents() { // doesn't make much sense in the context of a list... - ManagedList parent = new ManagedList(); - parent.add("one"); - parent.add("two"); - ManagedList child = new ManagedList(); - child.add("one"); + ManagedList parent = ManagedList.of("one", "two"); + ManagedList child = ManagedList.of("one"); child.setMergeEnabled(true); List mergedList = child.merge(parent); assertThat(mergedList.size()).as("merge() obviously did not work.").isEqualTo(3); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java index 1f6dc5e416..b83fa176d3 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedMapTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -16,6 +16,7 @@ package org.springframework.beans.factory.support; +import java.util.AbstractMap.SimpleEntry; import java.util.Map; import org.junit.jupiter.api.Test; @@ -34,11 +35,9 @@ public class ManagedMapTests { @Test public void mergeSunnyDay() { - ManagedMap parent = new ManagedMap(); - parent.put("one", "one"); - parent.put("two", "two"); - ManagedMap child = new ManagedMap(); - child.put("three", "three"); + ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"), + new SimpleEntry<>("two", "two")); + ManagedMap child = ManagedMap.ofEntries(new SimpleEntry<>("tree", "three")); child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); assertThat(mergedMap.size()).as("merge() obviously did not work.").isEqualTo(3); @@ -67,9 +66,8 @@ public class ManagedMapTests { @Test public void mergeEmptyChild() { - ManagedMap parent = new ManagedMap(); - parent.put("one", "one"); - parent.put("two", "two"); + ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"), + new SimpleEntry<>("two", "two")); ManagedMap child = new ManagedMap(); child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); @@ -78,11 +76,9 @@ public class ManagedMapTests { @Test public void mergeChildValuesOverrideTheParents() { - ManagedMap parent = new ManagedMap(); - parent.put("one", "one"); - parent.put("two", "two"); - ManagedMap child = new ManagedMap(); - child.put("one", "fork"); + ManagedMap parent = ManagedMap.ofEntries(new SimpleEntry<>("one", "one"), + new SimpleEntry<>("two", "two")); + ManagedMap child = ManagedMap.ofEntries(new SimpleEntry<>("one", "fork")); child.setMergeEnabled(true); Map mergedMap = (Map) child.merge(parent); // child value for 'one' must override parent value... diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java index 39c0899781..c11a63a6da 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/support/ManagedSetTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 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. @@ -34,10 +34,8 @@ public class ManagedSetTests { @Test public void mergeSunnyDay() { - ManagedSet parent = new ManagedSet(); - parent.add("one"); - parent.add("two"); - ManagedSet child = new ManagedSet(); + ManagedSet parent = ManagedSet.of("one", "two"); + ManagedSet child = ManagedSet.of("three"); child.add("three"); child.setMergeEnabled(true); Set mergedSet = child.merge(parent); @@ -46,8 +44,7 @@ public class ManagedSetTests { @Test public void mergeWithNullParent() { - ManagedSet child = new ManagedSet(); - child.add("one"); + ManagedSet child = ManagedSet.of("one"); child.setMergeEnabled(true); assertThat(child.merge(null)).isSameAs(child); } @@ -60,8 +57,7 @@ public class ManagedSetTests { @Test public void mergeWithNonCompatibleParentType() { - ManagedSet child = new ManagedSet(); - child.add("one"); + ManagedSet child = ManagedSet.of("one"); child.setMergeEnabled(true); assertThatIllegalArgumentException().isThrownBy(() -> child.merge("hello")); @@ -69,9 +65,7 @@ public class ManagedSetTests { @Test public void mergeEmptyChild() { - ManagedSet parent = new ManagedSet(); - parent.add("one"); - parent.add("two"); + ManagedSet parent = ManagedSet.of("one", "two"); ManagedSet child = new ManagedSet(); child.setMergeEnabled(true); Set mergedSet = child.merge(parent); @@ -81,11 +75,8 @@ public class ManagedSetTests { @Test public void mergeChildValuesOverrideTheParents() { // asserts that the set contract is not violated during a merge() operation... - ManagedSet parent = new ManagedSet(); - parent.add("one"); - parent.add("two"); - ManagedSet child = new ManagedSet(); - child.add("one"); + ManagedSet parent = ManagedSet.of("one", "two"); + ManagedSet child = ManagedSet.of("one"); child.setMergeEnabled(true); Set mergedSet = child.merge(parent); assertThat(mergedSet.size()).as("merge() obviously did not work.").isEqualTo(2); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java index be08292460..3b1d2c9446 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/ComplexWebApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2022 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. @@ -158,9 +158,7 @@ public class ComplexWebApplicationContext extends StaticWebApplicationContext { pvs = new MutablePropertyValues(); pvs.add("order", "0"); pvs.add("exceptionMappings", "java.lang.Exception=failed1"); - List mappedHandlers = new ManagedList<>(); - mappedHandlers.add(new RuntimeBeanReference("anotherLocaleHandler")); - pvs.add("mappedHandlers", mappedHandlers); + pvs.add("mappedHandlers", ManagedList.of(new RuntimeBeanReference("anotherLocaleHandler"))); pvs.add("defaultStatusCode", "500"); pvs.add("defaultErrorView", "failed2"); registerSingleton("handlerExceptionResolver", SimpleMappingExceptionResolver.class, pvs);