public Object copyOf(List<Object> list) { Object ret = Array.newInstance(elementMetaInfo.getType(), list.size()); for (int i = 0; i < list.size(); i++) { try { ReflectUtils.arraySet(ret, i, list.get(i)); } catch (Throwable e) { e.printStackTrace(); } } return ret; }
public Object copyOf(List<Object> list) { Object ret = Array.newInstance(elementMetaInfo.getExtractedType(), list.size()); for (int i = 0; i < list.size(); i++) { try { ReflectUtils.arraySet(ret, i, list.get(i)); } catch (Throwable e) { throw new CommonRuntimeException(e); } } return ret; }
public Object copyOf(List<Object> list) { Object ret = Array.newInstance(elementMetaInfo.getExtractedType(), list.size()); for (int i = 0; i < list.size(); i++) { try { ReflectUtils.arraySet(ret, i, list.get(i)); } catch (Throwable e) { throw new CommonRuntimeException(e); } } return ret; }
/** * Returns an array object, this method converts a collection object to an * array object through the specified element type of the array. * * @param collection The collection that needs be converted * @param arrayType The element type of an array * @return a array object and the element is the parameter specified type. */ @SuppressWarnings("unchecked") public static Object convert(Collection<?> collection, Class<?> arrayType) { int size = collection.size(); // Allocate a new Array Object newArray = Array.newInstance(Optional.ofNullable(arrayType) .filter(Class::isArray) .map(Class::getComponentType) .orElse((Class) Object.class), size); Iterator<?> iterator = collection.iterator(); // Convert and set each element in the new Array for (int i = 0; i < size; i++) { Object element = iterator.next(); try { ReflectUtils.arraySet(newArray, i, element); } catch (Throwable e) { System.err.println("set element to array exception, " + e.getMessage()); } } return newArray; }
Object element = iterator.next(); try { ReflectUtils.arraySet(newArray, i, element); } catch (Throwable e) { e.printStackTrace();
/** * Returns an array object, this method converts a collection object to an * array object through the specified element type of the array. * * @param collection The collection that needs be converted * @param arrayType The element type of an array * @return a array object and the element is the parameter specified type. */ @SuppressWarnings("unchecked") public static Object convert(Collection<?> collection, Class<?> arrayType) { int size = collection.size(); // Allocate a new Array Object newArray = Array.newInstance(Optional.ofNullable(arrayType) .filter(Class::isArray) .map(Class::getComponentType) .orElse((Class) Object.class), size); Iterator<?> iterator = collection.iterator(); // Convert and set each element in the new Array for (int i = 0; i < size; i++) { Object element = iterator.next(); try { ReflectUtils.arraySet(newArray, i, element); } catch (Throwable e) { System.err.println("set element to array exception, " + e.getMessage()); } } return newArray; }