List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:Main.java
/** * Converts an Object reference that is known to be an array into an * Object[]. If the array is assignable to Object[], the array passed in is * simply cast and returned. Otherwise a new Object[] of equal size is * constructed and the elements are wrapped and inserted into the new array * before being returned./*from ww w . jav a2s.c o m*/ * * @param in * an array of Objects or primitives * @return an Object[], either the array passed in, or in the case of * primitives, a new Object[] containing a wrapper for each element * in the input array * @throws IllegalArgumentException * thrown if the in parameter is null or not an array */ public static Object[] asObjectArray(Object in) { if (in == null || !in.getClass().isArray()) { throw new IllegalArgumentException("Parameter to asObjectArray must be a non-null array."); } else if (in instanceof Object[]) { return (Object[]) in; } else { int length = Array.getLength(in); Object[] out = new Object[length]; for (int i = 0; i < length; ++i) { out[i] = Array.get(in, i); } return out; } }
From source file:Main.java
/** * Returns the size of the collection if it can be determined to be a collection * * @param value the collection/*from ww w.ja v a 2s . co m*/ * @return the size, or <tt>null</tt> if not a collection */ public static Integer size(Object value) { if (value != null) { if (value instanceof Collection) { Collection<?> collection = (Collection<?>) value; return collection.size(); } else if (value instanceof Map) { Map<?, ?> map = (Map<?, ?>) value; return map.size(); } else if (value instanceof Object[]) { Object[] array = (Object[]) value; return array.length; } else if (value.getClass().isArray()) { return Array.getLength(value); } else if (value instanceof NodeList) { NodeList nodeList = (NodeList) value; return nodeList.getLength(); } } return null; }
From source file:com.chadekin.jadys.commons.expression.SqlExpressionBuilder.java
public static boolean isBlankValue(Object value) { boolean isBlank = value == null || StringUtils.isBlank(value.toString()); if (!isBlank) { boolean isEmptyCollection = value instanceof Collection && CollectionUtils.isEmpty((Collection) value); boolean isEmptyArray = value.getClass().isArray() && Array.getLength(value) == 0; isBlank = isEmptyCollection || isEmptyArray; }//from ww w .j a v a 2 s .com return isBlank; }
From source file:Main.java
/** * General purpose list converter method. Will convert arrays, collections, * iterables etc. into lists.//from w w w .ja v a 2s. co m * * If the argument is a single object (such as a String or a POJO) it will * be wrapped in a single-element list. * * Null will be converted to the empty list. * * @param obj * any object * @return a list representation of the object */ public static List<?> toList(Object obj) { final List<Object> result; if (obj == null) { result = Collections.emptyList(); } else if (obj instanceof List) { @SuppressWarnings("unchecked") List<Object> list = (List<Object>) obj; result = list; } else if (obj.getClass().isArray()) { int length = Array.getLength(obj); result = new ArrayList<Object>(length); for (int i = 0; i < length; i++) { result.add(Array.get(obj, i)); } } else if (obj instanceof Iterable) { result = new ArrayList<Object>(); for (Object item : (Iterable<?>) obj) { result.add(item); } } else if (obj instanceof Iterator) { result = new ArrayList<Object>(); Iterator<?> it = (Iterator<?>) obj; while (it.hasNext()) { result.add(it.next()); } } else { result = new ArrayList<Object>(1); result.add(obj); } return result; }
From source file:br.msf.commons.util.ArrayUtils.java
/** * Returns the length of the given array. * * @param array The array to be evaluated. * @return The array length./* w w w. j a v a 2 s . co m*/ */ public static int getSize(final Object array) { return ObjectUtils.isArray(array) ? Array.getLength(array) : 0; }
From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java
public static byte[] convertToByteArray(final Object array) { if (array == null) { return null; }/* www.j ava 2s .c o m*/ if (array instanceof byte[]) { return (byte[]) array; } if (array instanceof Byte[]) { return ArrayUtils.toPrimitive((Byte[]) array); } final byte[] newArray = new byte[Array.getLength(array)]; for (int i = 0; i < newArray.length; i++) { final Number val = (Number) Array.get(array, i); newArray[i] = val.byteValue(); } return newArray; }
From source file:Main.java
/** * @return an Object array for the given object. * * @param obj Object to convert to an array. Converts primitive * arrays to Object arrays consisting of their wrapper * classes. If the object is not an array (object or primitve) * then a new array of the given type is created and the * object is set as the sole element. *///from w ww. j av a 2 s . co m public static Object[] toArray(final Object obj) { // if the object is an array, the cast and return it. if (obj instanceof Object[]) { return (Object[]) obj; } // if the object is an array of primitives then wrap the array Class type = obj.getClass(); Object array; if (type.isArray()) { int length = Array.getLength(obj); Class componentType = type.getComponentType(); array = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(array, i, Array.get(obj, i)); } } else { array = Array.newInstance(type, 1); Array.set(array, 0, obj); } return (Object[]) array; }
From source file:Main.java
/** * <p>//from w w w .j av a 2s .c o m * Converts an Object reference that is known to be an array into a List. * Semantically very similar to {@link java.util.Arrays#asList(Object[])} * except that this method can deal with arrays of primitives in the same * manner as arrays as objects. * </p> * * <p> * A new List is created of the same size as the array, and elements are * copied from the array into the List. If elements are primitives then they * are converted to the appropriate wrapper types in order to return a List. * </p> * * @param in * an array of Objects or primitives (null values are not * allowed) * @return a List containing an element for each element in the input array * @throws IllegalArgumentException * thrown if the in parameter is null or not an array */ public static List<Object> asList(Object in) { if (in == null || !in.getClass().isArray()) { throw new IllegalArgumentException("Parameter to asObjectArray must be a non-null array."); } else { int length = Array.getLength(in); LinkedList<Object> list = new LinkedList<Object>(); for (int i = 0; i < length; ++i) { list.add(i, Array.get(in, i)); } return list; } }
From source file:cloudnet.util.Dumper.java
public static String dump(Object o, int maxDepth, int maxArrayElements, String[] ignoreList) { DumpContext ctx = Dumper.getInstance().new DumpContext(); ctx.maxDepth = maxDepth;/* w w w.j a v a2s. c om*/ ctx.maxArrayElements = maxArrayElements; if (ignoreList != null) { for (int i = 0; i < Array.getLength(ignoreList); i++) { int colonIdx = ignoreList[i].indexOf(':'); if (colonIdx == -1) { ignoreList[i] = ignoreList[i] + ":"; } ctx.ignoreList.put(ignoreList[i], ignoreList[i]); } } return dump(o, ctx); }
From source file:ArrayConverter.java
private static void addToList(final List list, final Object array) { final int length = Array.getLength(array); for (int i = 0; i < length; i++) { final Object value = Array.get(array, i); if (value == null) { list.add(null);//from w w w.j a v a2s. c o m continue; } if (value.getClass().isArray() == false) { list.add(value); continue; } ArrayConverter.addToList(list, value); } }