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
/** * 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 .ja va2 s. c o m*/ * * @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(final Object array, final 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:Main.java
/** * Copies elements of source to dest. If adjust is -1 the element at * colindex is not copied. If adjust is +1 that element is filled with * the Object addition. All the rest of the elements in source are * shifted left or right accordingly when they are copied. If adjust is 0 * the addition is copied over the element at colindex. * * No checks are perfomed on array sizes and an exception is thrown * if they are not consistent with the other arguments. *///from ww w. j a v a 2 s . c o m public static void copyAdjustArray(Object source, Object dest, Object addition, int colindex, int adjust) { int length = Array.getLength(source); if (colindex < 0) { System.arraycopy(source, 0, dest, 0, length); return; } System.arraycopy(source, 0, dest, 0, colindex); if (adjust == 0) { int endcount = length - colindex - 1; Array.set(dest, colindex, addition); if (endcount > 0) { System.arraycopy(source, colindex + 1, dest, colindex + 1, endcount); } } else if (adjust < 0) { int endcount = length - colindex - 1; if (endcount > 0) { System.arraycopy(source, colindex + 1, dest, colindex, endcount); } } else { int endcount = length - colindex; Array.set(dest, colindex, addition); if (endcount > 0) { System.arraycopy(source, colindex, dest, colindex + 1, endcount); } } }
From source file:Main.java
/** * @param propertyValue node.getProperty value. * @return a collection of all the values from a property. If the value is * just a plain "single" value the collection will contain that * single value. If the value is an array of values, all those * values are added to the collection. */// ww w. ja v a2 s .com public static Collection<Object> propertyValueToCollection(Object propertyValue) { Set<Object> values = new HashSet<Object>(); try { int length = Array.getLength(propertyValue); for (int i = 0; i < length; i++) { values.add(Array.get(propertyValue, i)); } } catch (IllegalArgumentException e) { values.add(propertyValue); } return values; }
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. * * @param array The array to copy, must not be {@code null}. * @param newArrayComponentType If {@code array} is {@code null}, create a * size 1 array of this type.// w ww .j av a 2 s .c om * @return A new copy of the array of size 1 greater than the input. */ private static Object copyArrayGrow1(final Object array, final Class<?> newArrayComponentType) { if (array != null) { final int arrayLength = Array.getLength(array); final 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:Main.java
public static void cleanThreadLocals(Thread thread) throws NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException { if (thread == null) return;// w ww. j ava 2s . co m Field threadLocalsField = Thread.class.getDeclaredField("threadLocals"); threadLocalsField.setAccessible(true); Class<?> threadLocalMapKlazz = Class.forName("java.lang.ThreadLocal$ThreadLocalMap"); Field tableField = threadLocalMapKlazz.getDeclaredField("table"); tableField.setAccessible(true); Object fieldLocal = threadLocalsField.get(thread); if (fieldLocal == null) { return; } Object table = tableField.get(fieldLocal); int threadLocalCount = Array.getLength(table); for (int i = 0; i < threadLocalCount; i++) { Object entry = Array.get(table, i); if (entry != null) { Field valueField = entry.getClass().getDeclaredField("value"); valueField.setAccessible(true); Object value = valueField.get(entry); if (value != null) { if ("java.util.concurrent.ConcurrentLinkedQueue".equals(value.getClass().getName()) || "java.util.concurrent.ConcurrentHashMap".equals(value.getClass().getName())) { valueField.set(entry, null); } } } } }
From source file:Main.java
public static Object arrayExpandAddElements(Object array, Collection elementsToAdd) { Class cl = array.getClass();// w w w. j a v a 2s. com if (!cl.isArray()) return null; int length = Array.getLength(array); int newLength = length + elementsToAdd.size(); Class componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(array, 0, newArray, 0, length); int count = 0; for (Object element : elementsToAdd) { Array.set(newArray, length + count, element); count++; } return newArray; }
From source file:Main.java
/** * Checks if the specified collection/array/iterator is empty. * <p>/* www . j av a 2s . c o m*/ * This method can handles objects as follows * <ul> * <li>Collection - via collection isEmpty * <li>Map - via map isEmpty * <li>Array - using array size * <li>Iterator - via hasNext * <li>Enumeration - via hasMoreElements * </ul> * <p> * Note: This method is named to avoid clashing with * {@link #isEmpty(Collection)}. * * @param object * the object to get the size of, not null * @return true if empty * @throws IllegalArgumentException * thrown if object is not recognised or null */ public static boolean sizeIsEmpty(Object object) { if (object instanceof Collection) { return ((Collection<?>) object).isEmpty(); } else if (object instanceof Map) { return ((Map<?, ?>) object).isEmpty(); } else if (object instanceof Object[]) { return ((Object[]) object).length == 0; } else if (object instanceof Iterator) { return !((Iterator<?>) object).hasNext(); } else if (object instanceof Enumeration) { return !((Enumeration<?>) object).hasMoreElements(); } else if (object == null) { throw new IllegalArgumentException("Unsupported object type: null"); } else { try { return Array.getLength(object) == 0; } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); } } }
From source file:Main.java
@SuppressWarnings("rawtypes") protected static boolean isNullOrEmpty(Object obj) { if (obj == null) { return true; }/*from w ww . j ava 2 s . co m*/ if (obj instanceof String) { if (((String) obj).length() == 0) { return true; } } if (obj instanceof Collection) { if (((Collection) obj).size() == 0) { return true; } } if (obj instanceof Map) { if (((Map) obj).size() == 0) { return true; } } if (isArray(obj)) { if (Array.getLength(obj) == 0) { return true; } } return false; }
From source file:ArrayExpander.java
public static Object expandAtHead(Object array, int newSize) { if (array == null) { return null; }/*from w w w . j a va 2s. c o m*/ Class c = array.getClass(); if (c.isArray()) { int len = Array.getLength(array); if (len >= newSize) { return array; } else { Class cc = c.getComponentType(); Object newArray = Array.newInstance(cc, newSize); System.arraycopy(array, 0, newArray, newSize - len, len); return newArray; } } else { throw new ClassCastException("need array"); } }
From source file:Main.java
@SuppressWarnings("rawtypes") private static Object joinArrays(Object paramObject1, Object paramObject2) { Class localClass = paramObject1.getClass().getComponentType(); int i = Array.getLength(paramObject1); int j = i + Array.getLength(paramObject2); Object localObject = Array.newInstance(localClass, j); int k = 0;// w w w . ja va 2 s . c o m while (k < i) { Array.set(localObject, k, Array.get(paramObject1, k)); k++; } while (k < j) { Array.set(localObject, k, Array.get(paramObject2, k - i)); k++; } return localObject; }