Make ObjectUtils.addObjectToArray() generic

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3951 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Chris Beams 2011-02-08 13:01:29 +00:00
parent 91ecefdaa1
commit 6a68b44ca9
4 changed files with 16 additions and 15 deletions

View File

@ -305,7 +305,7 @@ public class BeanDefinitionBuilder {
this.beanDefinition.setDependsOn(new String[] {beanName}); this.beanDefinition.setDependsOn(new String[] {beanName});
} }
else { else {
String[] added = (String[]) ObjectUtils.addObjectToArray(this.beanDefinition.getDependsOn(), beanName); String[] added = ObjectUtils.addObjectToArray(this.beanDefinition.getDependsOn(), beanName);
this.beanDefinition.setDependsOn(added); this.beanDefinition.setDependsOn(added);
} }
return this; return this;

View File

@ -334,12 +334,12 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces
ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class); ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
ScriptSource scriptSource = ScriptSource scriptSource =
getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator()); getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
Class[] interfaces = scriptFactory.getScriptInterfaces(); Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
Class[] scriptedInterfaces = interfaces; Class<?>[] scriptedInterfaces = interfaces;
if (scriptFactory.requiresConfigInterface() && !bd.getPropertyValues().isEmpty()) { if (scriptFactory.requiresConfigInterface() && !bd.getPropertyValues().isEmpty()) {
Class configInterface = createConfigInterface(bd, interfaces); Class<?> configInterface = createConfigInterface(bd, interfaces);
scriptedInterfaces = (Class[]) ObjectUtils.addObjectToArray(interfaces, configInterface); scriptedInterfaces = ObjectUtils.addObjectToArray(interfaces, configInterface);
} }
BeanDefinition objectBd = createScriptedObjectBeanDefinition( BeanDefinition objectBd = createScriptedObjectBeanDefinition(

View File

@ -122,14 +122,14 @@ public abstract class ObjectUtils {
} }
/** /**
* Append the given Object to the given array, returning a new array * Append the given object to the given array, returning a new array
* consisting of the input array contents plus the given Object. * consisting of the input array contents plus the given object.
* @param array the array to append to (can be <code>null</code>) * @param array the array to append to (can be <code>null</code>)
* @param obj the Object to append * @param obj the object to append
* @return the new array (of the same component type; never <code>null</code>) * @return the new array (of the same component type; never <code>null</code>)
*/ */
public static Object[] addObjectToArray(Object[] array, Object obj) { public static <A,O extends A> A[] addObjectToArray(A[] array, O obj) {
Class compType = Object.class; Class<?> compType = Object.class;
if (array != null) { if (array != null) {
compType = array.getClass().getComponentType(); compType = array.getClass().getComponentType();
} }
@ -137,7 +137,8 @@ public abstract class ObjectUtils {
compType = obj.getClass(); compType = obj.getClass();
} }
int newArrLength = (array != null ? array.length + 1 : 1); int newArrLength = (array != null ? array.length + 1 : 1);
Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength); @SuppressWarnings("unchecked")
A[] newArr = (A[]) Array.newInstance(compType, newArrLength);
if (array != null) { if (array != null) {
System.arraycopy(array, 0, newArr, 0, array.length); System.arraycopy(array, 0, newArr, 0, array.length);
} }

View File

@ -119,7 +119,7 @@ public final class ObjectUtilsTests extends TestCase {
public void testAddObjectToArrayWhenEmpty() { public void testAddObjectToArrayWhenEmpty() {
String[] array = new String[0]; String[] array = new String[0];
String newElement = "foo"; String newElement = "foo";
Object[] newArray = ObjectUtils.addObjectToArray(array, newElement); String[] newArray = ObjectUtils.addObjectToArray(array, newElement);
assertEquals(1, newArray.length); assertEquals(1, newArray.length);
assertEquals(newElement, newArray[0]); assertEquals(newElement, newArray[0]);
} }
@ -128,7 +128,7 @@ public final class ObjectUtilsTests extends TestCase {
String existingElement = "foo"; String existingElement = "foo";
String[] array = new String[] {existingElement}; String[] array = new String[] {existingElement};
String newElement = "bar"; String newElement = "bar";
Object[] newArray = ObjectUtils.addObjectToArray(array, newElement); String[] newArray = ObjectUtils.addObjectToArray(array, newElement);
assertEquals(2, newArray.length); assertEquals(2, newArray.length);
assertEquals(existingElement, newArray[0]); assertEquals(existingElement, newArray[0]);
assertEquals(newElement, newArray[1]); assertEquals(newElement, newArray[1]);
@ -137,7 +137,7 @@ public final class ObjectUtilsTests extends TestCase {
public void testAddObjectToSingleNullElementArray() { public void testAddObjectToSingleNullElementArray() {
String[] array = new String[] {null}; String[] array = new String[] {null};
String newElement = "bar"; String newElement = "bar";
Object[] newArray = ObjectUtils.addObjectToArray(array, newElement); String[] newArray = ObjectUtils.addObjectToArray(array, newElement);
assertEquals(2, newArray.length); assertEquals(2, newArray.length);
assertEquals(null, newArray[0]); assertEquals(null, newArray[0]);
assertEquals(newElement, newArray[1]); assertEquals(newElement, newArray[1]);
@ -145,7 +145,7 @@ public final class ObjectUtilsTests extends TestCase {
public void testAddObjectToNullArray() throws Exception { public void testAddObjectToNullArray() throws Exception {
String newElement = "foo"; String newElement = "foo";
Object[] newArray = ObjectUtils.addObjectToArray(null, newElement); String[] newArray = ObjectUtils.addObjectToArray(null, newElement);
assertEquals(1, newArray.length); assertEquals(1, newArray.length);
assertEquals(newElement, newArray[0]); assertEquals(newElement, newArray[0]);
} }