List of usage examples for java.lang.reflect Array getLength
@HotSpotIntrinsicCandidate public static native int getLength(Object array) throws IllegalArgumentException;
From source file:edu.sdsc.scigraph.services.jersey.writers.BbopJsGraphWriter.java
static Collection<String> getCategories(Vertex vertex) { Collection<String> categories = new HashSet<>(); if (vertex.getPropertyKeys().contains(Concept.CATEGORY)) { Object value = vertex.getProperty(Concept.CATEGORY); if (value.getClass().isArray()) { for (int i = 0; i < Array.getLength(value); i++) { categories.add((String) Array.get(value, i)); }/*from w w w .ja va 2s . c o m*/ } else if (value instanceof Iterable) { categories.addAll(newArrayList((Iterable<String>) value)); } else { categories.add((String) value); } } return categories; }
From source file:org.atemsource.atem.impl.common.attribute.collection.ArrayAttributeImpl.java
@Override public int getSize(Object entity) { return Array.getLength(getAccessor().getValue(entity)); }
From source file:br.msf.commons.util.ArrayUtils.java
/** * Indicates whether the given object is a non-empty array. * * @param array The array to test./*from w w w . ja v a 2 s . c om*/ * @return {@code true} if the array is not null and not empty; */ public static boolean isNotEmpty(final Object array) { return ObjectUtils.isArray(array) && Array.getLength(array) > 0; }
From source file:Main.java
/** * Gets the size of the collection/iterator specified. * <p>// w w w . j a v a 2 s . c om * This method can handles objects as follows * <ul> * <li>Collection - the collection size * <li>Map - the map size * <li>Array - the array size * <li>Iterator - the number of elements remaining in the iterator * <li>Enumeration - the number of elements remaining in the enumeration * </ul> * * @param object the object to get the size of, may be null * @return the size of the specified collection or 0 if the object was null * @throws IllegalArgumentException thrown if object is not recognised * @since 3.1 */ public static int size(final Object object) { if (object == null) { return 0; } int total = 0; if (object instanceof Map<?, ?>) { total = ((Map<?, ?>) object).size(); } else if (object instanceof Collection<?>) { total = ((Collection<?>) object).size(); } else if (object instanceof Object[]) { total = ((Object[]) object).length; } else if (object instanceof Iterator<?>) { final Iterator<?> it = (Iterator<?>) object; while (it.hasNext()) { total++; it.next(); } } else if (object instanceof Enumeration<?>) { final Enumeration<?> it = (Enumeration<?>) object; while (it.hasMoreElements()) { total++; it.nextElement(); } } else { try { total = Array.getLength(object); } catch (final IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } return total; }
From source file:edu.sdsc.scigraph.neo4j.GraphUtil.java
static Object getNewPropertyValue(Object originalValue, Object newValue) { Class<?> clazz = checkNotNull(newValue).getClass(); boolean reduceToString = false; if (null != originalValue && originalValue.getClass().isArray()) { Class<?> originalClazz = Array.get(originalValue, 0).getClass(); if (!originalClazz.equals(clazz)) { reduceToString = true;//w ww .jav a 2s . co m clazz = String.class; } newValue = reduceToString ? newValue.toString() : newValue; Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1); for (int i = 0; i < Array.getLength(originalValue); i++) { Object val = Array.get(originalValue, i); if (newValue.equals(val)) { return originalValue; } Array.set(newArray, i, reduceToString ? val.toString() : val); } Array.set(newArray, Array.getLength(originalValue), newValue); return newArray; } else if (null != originalValue) { if (!clazz.equals(originalValue.getClass())) { reduceToString = true; clazz = String.class; } originalValue = reduceToString ? originalValue.toString() : originalValue; newValue = reduceToString ? newValue.toString() : newValue; if (!originalValue.equals(newValue)) { Object newArray = Array.newInstance(clazz, 2); Array.set(newArray, 0, originalValue); Array.set(newArray, 1, newValue); return newArray; } else { return originalValue; } } else { return newValue; } }
From source file:Main.java
/** * Returns a copy of the given array of size 1 greater than the argument. * The last value of the array is left to the default value. * /* w w w . j av a2 s . c om*/ * @param array The array to copy, must not be <code>null</code>. * @param newArrayComponentType If <code>array</code> is <code>null</code>, create a * size 1 array of this type. * @return A new copy of the array of size 1 greater than the input. */ private static Object copyArrayGrow1(Object array, Class newArrayComponentType) { if (array != null) { int arrayLength = Array.getLength(array); Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1); System.arraycopy(array, 0, newArray, 0, arrayLength); return newArray; } return Array.newInstance(newArrayComponentType, 1); }
From source file:com.scit.sling.test.MockValueMap.java
@SuppressWarnings("unchecked") private <T> T convertType(Object o, Class<T> type) { if (o == null) { return null; }// w w w . j a v a 2 s .c om if (o.getClass().isArray() && type.isArray()) { if (type.getComponentType().isAssignableFrom(o.getClass().getComponentType())) { return (T) o; } else { // we need to convert the elements in the array Object array = Array.newInstance(type.getComponentType(), Array.getLength(o)); for (int i = 0; i < Array.getLength(o); i++) { Array.set(array, i, convertType(Array.get(o, i), type.getComponentType())); } return (T) array; } } if (o.getClass().isAssignableFrom(type)) { return (T) o; } if (String.class.isAssignableFrom(type)) { // Format dates if (o instanceof Calendar) { return (T) formatDate((Calendar) o); } else if (o instanceof Date) { return (T) formatDate((Date) o); } else if (o instanceof DateTime) { return (T) formatDate((DateTime) o); } return (T) String.valueOf(o); } else if (o instanceof DateTime) { DateTime dt = (DateTime) o; if (Calendar.class.isAssignableFrom(type)) { return (T) dt.toCalendar(Locale.getDefault()); } else if (Date.class.isAssignableFrom(type)) { return (T) dt.toDate(); } else if (Number.class.isAssignableFrom(type)) { return convertType(dt.getMillis(), type); } } else if (o instanceof Number && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) (Byte) ((Number) o).byteValue(); } else if (Double.class.isAssignableFrom(type)) { return (T) (Double) ((Number) o).doubleValue(); } else if (Float.class.isAssignableFrom(type)) { return (T) (Float) ((Number) o).floatValue(); } else if (Integer.class.isAssignableFrom(type)) { return (T) (Integer) ((Number) o).intValue(); } else if (Long.class.isAssignableFrom(type)) { return (T) (Long) ((Number) o).longValue(); } else if (Short.class.isAssignableFrom(type)) { return (T) (Short) ((Number) o).shortValue(); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal(o.toString()); } } else if (o instanceof Number && type.isPrimitive()) { final Number num = (Number) o; if (type == byte.class) { return (T) new Byte(num.byteValue()); } else if (type == double.class) { return (T) new Double(num.doubleValue()); } else if (type == float.class) { return (T) new Float(num.floatValue()); } else if (type == int.class) { return (T) new Integer(num.intValue()); } else if (type == long.class) { return (T) new Long(num.longValue()); } else if (type == short.class) { return (T) new Short(num.shortValue()); } } else if (o instanceof String && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) new Byte((String) o); } else if (Double.class.isAssignableFrom(type)) { return (T) new Double((String) o); } else if (Float.class.isAssignableFrom(type)) { return (T) new Float((String) o); } else if (Integer.class.isAssignableFrom(type)) { return (T) new Integer((String) o); } else if (Long.class.isAssignableFrom(type)) { return (T) new Long((String) o); } else if (Short.class.isAssignableFrom(type)) { return (T) new Short((String) o); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal((String) o); } } throw new NotImplementedException( "Can't handle conversion from " + o.getClass().getName() + " to " + type.getName()); }
From source file:cloudnet.util.Dumper.java
/** * * @param o/*from w w w .j a v a 2 s . c om*/ * @param ctx * @return */ protected static String dump(Object o, DumpContext ctx) { if (o == null) { return "<null>"; } ctx.callCount++; StringBuilder tabs = new StringBuilder(); for (int k = 0; k < ctx.callCount; k++) { tabs.append("\t"); } StringBuilder buffer = new StringBuilder(); Class oClass = o.getClass(); String oSimpleName = getSimpleNameWithoutArrayQualifier(oClass); if (ctx.ignoreList.get(oSimpleName + ":") != null) { return "<Ignored>"; } if (oClass.isArray()) { buffer.append("\n"); buffer.append(tabs.toString().substring(1)); buffer.append("[\n"); int rowCount = ctx.maxArrayElements == 0 ? Array.getLength(o) : FastMath.min(ctx.maxArrayElements, Array.getLength(o)); for (int i = 0; i < rowCount; i++) { buffer.append(tabs.toString()); try { Object value = Array.get(o, i); buffer.append(dumpValue(value, ctx)); } catch (Exception e) { buffer.append(e.getMessage()); } if (i < Array.getLength(o) - 1) { buffer.append(","); } buffer.append("\n"); } if (rowCount < Array.getLength(o)) { buffer.append(tabs.toString()); buffer.append(Array.getLength(o) - rowCount + " more array elements..."); buffer.append("\n"); } buffer.append(tabs.toString().substring(1)); buffer.append("]"); } else { buffer.append("\n"); buffer.append(tabs.toString().substring(1)); buffer.append("{\n"); buffer.append(tabs.toString()); buffer.append("hashCode: " + o.hashCode()); buffer.append("\n"); while (oClass != null && oClass != Object.class) { Field[] fields = oClass.getDeclaredFields(); if (ctx.ignoreList.get(oClass.getSimpleName()) == null) { if (oClass != o.getClass()) { buffer.append(tabs.toString().substring(1)); buffer.append(" Inherited from superclass " + oSimpleName + ":\n"); } for (int i = 0; i < fields.length; i++) { String fSimpleName = getSimpleNameWithoutArrayQualifier(fields[i].getType()); String fName = fields[i].getName(); fields[i].setAccessible(true); buffer.append(tabs.toString()); buffer.append(fName + "(" + fSimpleName + ")"); buffer.append("="); if (ctx.ignoreList.get(":" + fName) == null && ctx.ignoreList.get(fSimpleName + ":" + fName) == null && ctx.ignoreList.get(fSimpleName + ":") == null) { try { Object value = fields[i].get(o); buffer.append(dumpValue(value, ctx)); } catch (Exception e) { buffer.append(e.getMessage()); } buffer.append("\n"); } else { buffer.append("<Ignored>"); buffer.append("\n"); } } oClass = oClass.getSuperclass(); oSimpleName = oClass.getSimpleName(); } else { oClass = null; oSimpleName = ""; } } buffer.append(tabs.toString().substring(1)); buffer.append("}"); } ctx.callCount--; return buffer.toString(); }
From source file:org.activiti.rest.builder.management.TableDataObjectBuilder.java
protected void putColumnValue(JSONObject json, String key, Object value) throws JSONException { if (value == null) { JSONUtil.putRetainNull(json, key, null); } else if (value instanceof List<?>) { json.put(key, ((List<?>) value).size()); } else if (value.getClass().isArray()) { json.put(key, Array.getLength(value)); } else if (value instanceof Date) { String date = JSONUtil.formatISO8601Date((Date) value); JSONUtil.putRetainNull(json, key, date); } else {//from w ww . j a v a 2 s .c om // All other types can be handled by JSONObject JSONUtil.putRetainNull(json, key, value); } }
From source file:org.apache.axis2.jaxws.message.OccurrenceArray.java
/** * Get the List or array as a Object[]//ww w.j a v a 2s. co m * @return Object[] */ public Object[] getAsArray() { Object[] objects = null; if (value == null) { return new Object[0]; } else if (value instanceof List) { List l = (List) value; objects = new Object[l.size()]; for (int i = 0; i < l.size(); i++) { objects[i] = l.get(i); } } else { objects = new Object[Array.getLength(value)]; for (int i = 0; i < objects.length; i++) { objects[i] = Array.get(value, i); } } return objects; }