Skip to content

Commit b04987c

Browse files
committed
Make ObjectUtils.addObjectToArray() generic
1 parent fb57316 commit b04987c

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public BeanDefinitionBuilder addDependsOn(String beanName) {
305305
this.beanDefinition.setDependsOn(new String[] {beanName});
306306
}
307307
else {
308-
String[] added = (String[]) ObjectUtils.addObjectToArray(this.beanDefinition.getDependsOn(), beanName);
308+
String[] added = ObjectUtils.addObjectToArray(this.beanDefinition.getDependsOn(), beanName);
309309
this.beanDefinition.setDependsOn(added);
310310
}
311311
return this;

org.springframework.context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,12 @@ protected void prepareScriptBeans(
334334
ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
335335
ScriptSource scriptSource =
336336
getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
337-
Class[] interfaces = scriptFactory.getScriptInterfaces();
337+
Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
338338

339-
Class[] scriptedInterfaces = interfaces;
339+
Class<?>[] scriptedInterfaces = interfaces;
340340
if (scriptFactory.requiresConfigInterface() && !bd.getPropertyValues().isEmpty()) {
341-
Class configInterface = createConfigInterface(bd, interfaces);
342-
scriptedInterfaces = (Class[]) ObjectUtils.addObjectToArray(interfaces, configInterface);
341+
Class<?> configInterface = createConfigInterface(bd, interfaces);
342+
scriptedInterfaces = ObjectUtils.addObjectToArray(interfaces, configInterface);
343343
}
344344

345345
BeanDefinition objectBd = createScriptedObjectBeanDefinition(

org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,23 @@ public static boolean containsElement(Object[] array, Object element) {
122122
}
123123

124124
/**
125-
* Append the given Object to the given array, returning a new array
126-
* consisting of the input array contents plus the given Object.
125+
* Append the given object to the given array, returning a new array
126+
* consisting of the input array contents plus the given object.
127127
* @param array the array to append to (can be <code>null</code>)
128-
* @param obj the Object to append
128+
* @param obj the object to append
129129
* @return the new array (of the same component type; never <code>null</code>)
130130
*/
131-
public static Object[] addObjectToArray(Object[] array, Object obj) {
132-
Class compType = Object.class;
131+
public static <A,O extends A> A[] addObjectToArray(A[] array, O obj) {
132+
Class<?> compType = Object.class;
133133
if (array != null) {
134134
compType = array.getClass().getComponentType();
135135
}
136136
else if (obj != null) {
137137
compType = obj.getClass();
138138
}
139139
int newArrLength = (array != null ? array.length + 1 : 1);
140-
Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength);
140+
@SuppressWarnings("unchecked")
141+
A[] newArr = (A[]) Array.newInstance(compType, newArrLength);
141142
if (array != null) {
142143
System.arraycopy(array, 0, newArr, 0, array.length);
143144
}

org.springframework.core/src/test/java/org/springframework/util/ObjectUtilsTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public void testAddObjectToArraySunnyDay() {
119119
public void testAddObjectToArrayWhenEmpty() {
120120
String[] array = new String[0];
121121
String newElement = "foo";
122-
Object[] newArray = ObjectUtils.addObjectToArray(array, newElement);
122+
String[] newArray = ObjectUtils.addObjectToArray(array, newElement);
123123
assertEquals(1, newArray.length);
124124
assertEquals(newElement, newArray[0]);
125125
}
@@ -128,7 +128,7 @@ public void testAddObjectToSingleNonNullElementArray() {
128128
String existingElement = "foo";
129129
String[] array = new String[] {existingElement};
130130
String newElement = "bar";
131-
Object[] newArray = ObjectUtils.addObjectToArray(array, newElement);
131+
String[] newArray = ObjectUtils.addObjectToArray(array, newElement);
132132
assertEquals(2, newArray.length);
133133
assertEquals(existingElement, newArray[0]);
134134
assertEquals(newElement, newArray[1]);
@@ -137,15 +137,15 @@ public void testAddObjectToSingleNonNullElementArray() {
137137
public void testAddObjectToSingleNullElementArray() {
138138
String[] array = new String[] {null};
139139
String newElement = "bar";
140-
Object[] newArray = ObjectUtils.addObjectToArray(array, newElement);
140+
String[] newArray = ObjectUtils.addObjectToArray(array, newElement);
141141
assertEquals(2, newArray.length);
142142
assertEquals(null, newArray[0]);
143143
assertEquals(newElement, newArray[1]);
144144
}
145145

146146
public void testAddObjectToNullArray() throws Exception {
147147
String newElement = "foo";
148-
Object[] newArray = ObjectUtils.addObjectToArray(null, newElement);
148+
String[] newArray = ObjectUtils.addObjectToArray(null, newElement);
149149
assertEquals(1, newArray.length);
150150
assertEquals(newElement, newArray[0]);
151151
}

0 commit comments

Comments
 (0)