[ https://issues.apache.org/jira/browse/LANG-537?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12778775#action_12778775 ]
Stephen Colebourne commented on LANG-537: ----------------------------------------- I get it now, and looks good. However, the tests needs strengthening. {code} Object obj = ArrayUtils.toArray("foo", "bar"); // does this compile? // what is the actual type of obj? String[] ? {code} There should be tests with other types, and combinations of types {code} Object obj = ArrayUtils.toArray(new Integer(6), new Short(6)); // does this compile? // what is the actual type of obj? Number[] ? {code} > Add ArrayUtils.toArray to create generic arrays > ----------------------------------------------- > > Key: LANG-537 > URL: https://issues.apache.org/jira/browse/LANG-537 > Project: Commons Lang > Issue Type: New Feature > Affects Versions: 3.0 > Reporter: Joerg Schaible > Assignee: Joerg Schaible > Fix For: 3.0 > > > {code:java} > /** > * Create a type-safe generic array. > * > * <p>Arrays are covariant i.e. they cannot be created from a generic > type:</p> > * > * <pre> > public static <T> T[] toArray(int size) > { > return T[size]; // compiler error here > } > public static <T> T[] toArray(int size) > { > return (T[])Object[size]; // ClassCastException at runtime > } > * </pre> > * > * <p>Therefore new arrays of generic types can be created with this method, > e.g. an arrays of Strings:</p> > * > * <pre> > String[] array = ArrayUtils.toArray("1", "2"); > String[] emptyArray = ArrayUtils.<String>toArray(); > * </pre> > * > * @param <T> the array's element type > * @param items the items of the array > * @return the array > * @author Jörg Schaible > * @since 3.0 > */ > public static <T> T[] toArray(final T... items) > { > return items; > } > {code} > Tests: > {code:java} > /** > * Tests generic array creation with specified type. > */ > public void testArrayCreation() > { > final String[] array = ArrayUtils.toArray("foo", "bar"); > assertEquals(2, array.length); > assertEquals("foo", array[0]); > assertEquals("bar", array[1]); > } > /** > * Tests generic array creation with generic type. > */ > public void testIndirectArrayCreation() > { > final String[] array = toArray("foo", "bar"); > assertEquals(2, array.length); > assertEquals("foo", array[0]); > assertEquals("bar", array[1]); > } > /** > * Tests generic empty array creation with generic type. > */ > public void testEmptyArrayCreation() > { > final String[] array = ArrayUtils.<String>toArray(); > assertEquals(0, array.length); > } > /** > * Tests indirect generic empty array creation with generic type. > */ > public void testIndirectEmptyArrayCreation() > { > final String[] array = toArray(); > assertEquals(0, array.length); > } > private static <T> T[] toArray(final T... items) > { > return ArrayUtils.toArray(items); > } > {code} -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.