From b04987ccc37985eae59a2b5cc19a1c6ea5d6c844 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 8 Feb 2011 13:01:29 +0000 Subject: [PATCH] Make ObjectUtils.addObjectToArray() generic --- .../factory/support/BeanDefinitionBuilder.java | 2 +- .../support/ScriptFactoryPostProcessor.java | 8 ++++---- .../java/org/springframework/util/ObjectUtils.java | 13 +++++++------ .../org/springframework/util/ObjectUtilsTests.java | 8 ++++---- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionBuilder.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionBuilder.java index a734e49360..478697fffc 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionBuilder.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/BeanDefinitionBuilder.java @@ -305,7 +305,7 @@ public class BeanDefinitionBuilder { this.beanDefinition.setDependsOn(new String[] {beanName}); } else { - String[] added = (String[]) ObjectUtils.addObjectToArray(this.beanDefinition.getDependsOn(), beanName); + String[] added = ObjectUtils.addObjectToArray(this.beanDefinition.getDependsOn(), beanName); this.beanDefinition.setDependsOn(added); } return this; diff --git a/org.springframework.context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java b/org.springframework.context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java index 76e10b6132..2458de6f84 100644 --- a/org.springframework.context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java +++ b/org.springframework.context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java @@ -334,12 +334,12 @@ public class ScriptFactoryPostProcessor extends InstantiationAwareBeanPostProces ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class); ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator()); - Class[] interfaces = scriptFactory.getScriptInterfaces(); + Class[] interfaces = scriptFactory.getScriptInterfaces(); - Class[] scriptedInterfaces = interfaces; + Class[] scriptedInterfaces = interfaces; if (scriptFactory.requiresConfigInterface() && !bd.getPropertyValues().isEmpty()) { - Class configInterface = createConfigInterface(bd, interfaces); - scriptedInterfaces = (Class[]) ObjectUtils.addObjectToArray(interfaces, configInterface); + Class configInterface = createConfigInterface(bd, interfaces); + scriptedInterfaces = ObjectUtils.addObjectToArray(interfaces, configInterface); } BeanDefinition objectBd = createScriptedObjectBeanDefinition( diff --git a/org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java b/org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java index 17f80132cd..c590dfa806 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java +++ b/org.springframework.core/src/main/java/org/springframework/util/ObjectUtils.java @@ -122,14 +122,14 @@ public abstract class ObjectUtils { } /** - * Append the given Object to the given array, returning a new array - * consisting of the input array contents plus the given Object. + * Append the given object to the given array, returning a new array + * consisting of the input array contents plus the given object. * @param array the array to append to (can be null) - * @param obj the Object to append + * @param obj the object to append * @return the new array (of the same component type; never null) */ - public static Object[] addObjectToArray(Object[] array, Object obj) { - Class compType = Object.class; + public static A[] addObjectToArray(A[] array, O obj) { + Class compType = Object.class; if (array != null) { compType = array.getClass().getComponentType(); } @@ -137,7 +137,8 @@ public abstract class ObjectUtils { compType = obj.getClass(); } 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) { System.arraycopy(array, 0, newArr, 0, array.length); } diff --git a/org.springframework.core/src/test/java/org/springframework/util/ObjectUtilsTests.java b/org.springframework.core/src/test/java/org/springframework/util/ObjectUtilsTests.java index 847c046466..d929faa692 100644 --- a/org.springframework.core/src/test/java/org/springframework/util/ObjectUtilsTests.java +++ b/org.springframework.core/src/test/java/org/springframework/util/ObjectUtilsTests.java @@ -119,7 +119,7 @@ public final class ObjectUtilsTests extends TestCase { public void testAddObjectToArrayWhenEmpty() { String[] array = new String[0]; String newElement = "foo"; - Object[] newArray = ObjectUtils.addObjectToArray(array, newElement); + String[] newArray = ObjectUtils.addObjectToArray(array, newElement); assertEquals(1, newArray.length); assertEquals(newElement, newArray[0]); } @@ -128,7 +128,7 @@ public final class ObjectUtilsTests extends TestCase { String existingElement = "foo"; String[] array = new String[] {existingElement}; String newElement = "bar"; - Object[] newArray = ObjectUtils.addObjectToArray(array, newElement); + String[] newArray = ObjectUtils.addObjectToArray(array, newElement); assertEquals(2, newArray.length); assertEquals(existingElement, newArray[0]); assertEquals(newElement, newArray[1]); @@ -137,7 +137,7 @@ public final class ObjectUtilsTests extends TestCase { public void testAddObjectToSingleNullElementArray() { String[] array = new String[] {null}; String newElement = "bar"; - Object[] newArray = ObjectUtils.addObjectToArray(array, newElement); + String[] newArray = ObjectUtils.addObjectToArray(array, newElement); assertEquals(2, newArray.length); assertEquals(null, newArray[0]); assertEquals(newElement, newArray[1]); @@ -145,7 +145,7 @@ public final class ObjectUtilsTests extends TestCase { public void testAddObjectToNullArray() throws Exception { String newElement = "foo"; - Object[] newArray = ObjectUtils.addObjectToArray(null, newElement); + String[] newArray = ObjectUtils.addObjectToArray(null, newElement); assertEquals(1, newArray.length); assertEquals(newElement, newArray[0]); }