commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Levon Karayan (JIRA)" <>
Subject [jira] Commented: (LANG-534) ArrayUtils should have method to convert null arrays to empty ones to help with Defensive coding
Date Fri, 25 Sep 2009 19:25:16 GMT


Levon Karayan commented on LANG-534:

Sorry I should have been more clear.

It's too bad that it won't work for this case, so we're still left with the per-type implementations.
 BTW, should I add the @since/@author/"@param T" tags to the patch?

His toArray method works when you know the types of objects during compile time, and is a
creative way to get a list of objects into an type safe array using generics.  I think it
would be a nice addition to ArrayUtils

public class TypedArray {
        public static void main(String[] args) {
                String[] sa = null;
                for (String s : TypedArray.<String>toArray()) {
                        System.out.println("This should never print");
                System.out.println( TypedArray.<String>toArray().getClass().getName()

        public static <T> T[] toArray(T... items) {
                return items;

On my system:

As a side point:  Even if we could somehow use generics to break it down to one method, I'm
still scratching my head as to how I could get the class empty array constants out of nullToEmpty.
 Identifying the input type using instanceof, getClass seems to be a challenge, as well as
trying to stuff the constants into the return.  

> ArrayUtils should have method to convert null arrays to empty ones to help with Defensive
> ------------------------------------------------------------------------------------------------
>                 Key: LANG-534
>                 URL:
>             Project: Commons Lang
>          Issue Type: New Feature
>            Reporter: Levon Karayan
>            Priority: Trivial
>             Fix For: 3.0
>         Attachments: 20090920LevonArrayUtils-nullToEmpty.patch
> There are APIs that I've come across that return <code>null</code> Arrays
in the event where there are no results.  Often these APIs correctly throw exceptions when
there is an "exceptional event", but no results isn't exceptional, and it often shouldn't
be.   This causes the programmer to make extra tests for null throughout the code to deal
with the null case, and sometimes these null cases are added after a customer searched for
gobleygook and got a NullPointerException.  It's just far cleaner/safer to convert these null
arrays to empty arrays.
> Another benefit to this method is that if the array being passed in is actually already
an empty array, it will swap the pointer for the <code>static final</code> in
the ArrayUtils class to help decrease memory fragmentation.
> e.g.
> try
> {
>   results = customer.getResults(query);
> } catch ( IOException ioex ) {
>   //  ...
> }
> if ( null == results )
> {
>    results = new int[0]{};
> }
> // do stuff
> try
> {
>   results = ArrayUtils.nullToEmpty(customer.getResults(query));
> } catch ( IOException ioex ) {
>   //  ...
> }
> // do stuff

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message