DataUtil { public static String[] getStringArray(Object object, String[] defValue) { if (object == null) { return defValue; } String[] result; if (object instanceof String[]) { result = (String[])object; } else if (object instanceof String) { String str = (String)object; str = str.trim(); result = StringUtils.split(str, ','); } else if (object instanceof Object[]) { Object[] array = (Object[])object; result = new String[array.length]; for (int i = 0; i < array.length; i++) { result[i] = getString(array[i], null); } } else if (object instanceof List) { List list = (List)object; result = new String[list.size()]; for (int i = 0; i < list.size(); i++) { result[i] = getString(list.get(i), null); } } else if (object.getClass().isArray()) { int len = Array.getLength(object); result = new String[len]; for (int i = 0; i < len; i++) { result[i] = getString(Array.get(object, i), null); } } else if (object instanceof Number || object instanceof Boolean || object instanceof Character) { result = new String[]{object.toString()}; } else { result = defValue; } return result; }  static boolean getBoolean(Object value, boolean defValue); static Boolean doGetBoolean(Object value, Boolean defValue); static boolean toBoolean(String value, boolean defValue); static Boolean convertToBoolean(String value, Boolean defValue); static boolean toBoolean(String value); static int getInt(Object value, int defValue); static long getLong(Object value, long defValue); static double getDouble(Object value, double defValue); static String getString(Object value, String defValue); static String[] getStringArray(Object object, String[] defValue); static int[] getIntArray(Object object, int[] defValue); static List<String> getStringList(Object object);  }
@Test public void getStringArray() throws Exception { assertArrayEquals(DataUtil.getStringArray(null, defaultStringArray), defaultStringArray); assertArrayEquals(DataUtil.getStringArray(Boolean.TRUE, defaultStringArray), new String[] {"true"}); assertArrayEquals(DataUtil.getStringArray(Boolean.FALSE, defaultStringArray), new String[] {"false"}); assertArrayEquals(DataUtil.getStringArray("True", defaultStringArray), new String[] { "True" }); assertArrayEquals(DataUtil.getStringArray("99", defaultStringArray), new String[] {"99"}); assertArrayEquals(DataUtil.getStringArray(" 88 ", defaultStringArray), new String[] {"88"}); assertArrayEquals(DataUtil.getStringArray(new String[] {"DDDDD"}, defaultStringArray), new String[] {"DDDDD"}); assertArrayEquals(DataUtil.getStringArray(new Object(), defaultStringArray), defaultStringArray); assertArrayEquals(DataUtil.getStringArray(888l, defaultStringArray), new String[] {"888"}); assertArrayEquals(DataUtil.getStringArray('a', defaultStringArray), new String[] {"a"}); assertArrayEquals(DataUtil.getStringArray(new Object[] {'a', 123} , defaultStringArray), new String[] {"a", "123"}); assertArrayEquals(DataUtil.getStringArray(new int[] {345, 123} , defaultStringArray), new String[] {"345", "123"}); }