[ 
https://issues.apache.org/jira/browse/LANG-1050?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14174785#comment-14174785
 ] 

James Sawle commented on LANG-1050:
-----------------------------------

Because it will fail at runtime if you don't.

If I modify the method to 
{code}
public static <T> T[] nullToEmpty(final T[] array, final Class<T[]> type) {
    if(array == null) {
        //return type.cast(Array.newInstance(type.getComponentType(), 0));
        return ArrayUtils.<T>toArray();
    }
    return array;
}
{code}

the following exception is thrown in _testNullToEmptyGenericNull_.

{noformat}
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to 
[Lorg.apache.commons.lang3.ArrayUtilsTest$TestClass;
        at 
org.apache.commons.lang3.ArrayUtilsTest.testNullToEmptyGenericNull(ArrayUtilsTest.java:370)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
        at 
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
        at 
com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
        at 
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
{noformat}

There is now a second reason as well. Due to use not changing the original 
methods, having a _static <T> T[] nullToEmpty(T[])_ method would require us to 
remove the Object version, and build in logic to still support the return of 
the static constant at the top. This would mean that the method had one 
contract when handling Object arrays and a different one for all other classes 
(which are not handled specifically below).

> Change nullToEmpty methods to generics
> --------------------------------------
>
>                 Key: LANG-1050
>                 URL: https://issues.apache.org/jira/browse/LANG-1050
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.*
>            Reporter: James Sawle
>             Fix For: Review Patch
>
>
> Currently there are multiple Object based methods which could be replaced by 
> a single generic method.
> - public static Long[] nullToEmpty(final Long[] array)
> - public static Integer[] nullToEmpty(final Integer[] array)
> - public static Short[] nullToEmpty(final Short[] array)
> - public static Character[] nullToEmpty(final Character[] array)
> - public static Byte[] nullToEmpty(final Byte[] array)
> - public static Double[] nullToEmpty(final Double[] array)
> - public static Float[] nullToEmpty(final Float[] array)
> - public static Boolean[] nullToEmpty(final Boolean[] array)
> Recommendation, replace all of these with a single method that would also 
> allow a defensive programming style when not using wrapped primitives.
> - public static <T> T[] nullToEmpty(final T[] array)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to