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 the size of the collection or array */// w w w . j ava 2 s . c om public static int size(Object collection) { if (collection instanceof Collection) { Collection value = (Collection) collection; return value.size(); } else if (collection != null) { if (collection.getClass().isArray()) { return Array.getLength(collection); } } return 0; }
From source file:SampleGetArrayReflection.java
public static void copyArray(Object source, Object dest) { for (int i = 0; i < Array.getLength(source); i++) { Array.set(dest, i, Array.get(source, i)); System.out.println(Array.get(dest, i)); }/*from ww w. j av a 2 s. c om*/ }
From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java
public static short[] convertToShortArray(final Object array) { if (array == null) { return null; }/* ww w .j a va 2s . co m*/ if (array instanceof short[]) { return (short[]) array; } if (array instanceof Short[]) { return ArrayUtils.toPrimitive((Short[]) array); } final short[] newArray = new short[Array.getLength(array)]; for (int i = 0; i < newArray.length; i++) { final Number val = (Number) Array.get(array, i); newArray[i] = val.shortValue(); } return newArray; }
From source file:Main.java
private static boolean compareScalarValues(Object myValue, Object otherValue) { if (myValue == null && otherValue != null || myValue != null && otherValue == null) return false; if (myValue == null) return true; if (myValue.getClass().isArray() && !otherValue.getClass().isArray() || !myValue.getClass().isArray() && otherValue.getClass().isArray()) return false; if (myValue.getClass().isArray() && otherValue.getClass().isArray()) { final int myArraySize = Array.getLength(myValue); final int otherArraySize = Array.getLength(otherValue); if (myArraySize != otherArraySize) return false; for (int i = 0; i < myArraySize; i++) if (!Array.get(myValue, i).equals(Array.get(otherValue, i))) return false; return true; }//from ww w. j av a2s .co m if (myValue instanceof Number && otherValue instanceof Number) { final Number myNumberValue = (Number) myValue; final Number otherNumberValue = (Number) otherValue; if (isInteger(myNumberValue) && isInteger(otherNumberValue)) return myNumberValue.longValue() == otherNumberValue.longValue(); else if (isFloat(myNumberValue) && isFloat(otherNumberValue)) return myNumberValue.doubleValue() == otherNumberValue.doubleValue(); } return myValue.equals(otherValue); }
From source file:Main.java
public static int deepHashCode(Object obj) { Set visited = new HashSet(); LinkedList<Object> stack = new LinkedList<Object>(); stack.addFirst(obj);//from w w w . j ava 2s.c o m int hash = 0; while (!stack.isEmpty()) { obj = stack.removeFirst(); if (obj == null || visited.contains(obj)) { continue; } visited.add(obj); if (obj.getClass().isArray()) { int len = Array.getLength(obj); for (int i = 0; i < len; i++) { stack.addFirst(Array.get(obj, i)); } continue; } if (obj instanceof Collection) { stack.addAll(0, (Collection) obj); continue; } if (obj instanceof Map) { stack.addAll(0, ((Map) obj).keySet()); stack.addAll(0, ((Map) obj).values()); continue; } if (hasCustomHashCode(obj.getClass())) { hash += obj.hashCode(); continue; } Collection<Field> fields = getDeepDeclaredFields(obj.getClass()); for (Field field : fields) { try { stack.addFirst(field.get(obj)); } catch (Exception ignored) { } } } return hash; }
From source file:Main.java
public static void outputArrays(final Object first, final Object second) { if (!first.getClass().isArray()) { throw new IllegalArgumentException("first is not an array."); }/*from w ww . j av a 2 s. c o m*/ if (!second.getClass().isArray()) { throw new IllegalArgumentException("second is not an array."); } final int lengthFirst = Array.getLength(first); final int lengthSecond = Array.getLength(second); final int length = Math.max(lengthFirst, lengthSecond); for (int idx = 0; idx < length; idx++) { System.out.print("[" + idx + "]\t"); if (idx < lengthFirst) { System.out.print(Array.get(first, idx) + "\t\t"); } else { System.out.print("\t\t"); } if (idx < lengthSecond) { System.out.print(Array.get(second, idx) + "\t"); } System.out.println(); } }
From source file:ArrayEnumeration.java
public ArrayEnumeration(Object obj) { Class type = obj.getClass();/* ww w .java 2 s . co m*/ if (!type.isArray()) { throw new IllegalArgumentException("Invalid type: " + type); } size = Array.getLength(obj); array = obj; }
From source file:Main.java
/** * Method to merge two arrays.// www .j a v a 2s. c o m * * @param firstObject * The first array to be merged * * @param secondObject * The second array to be merged * * @return an object containing the elements of the merged arrays */ private static Object joinArrays(Object firstObject, Object secondObject) { Class<?> o1Type = firstObject.getClass().getComponentType(); Class<?> o2Type = secondObject.getClass().getComponentType(); if (o1Type != o2Type) throw new IllegalArgumentException(); int firstObjectSize = Array.getLength(firstObject); int secondObjectSize = Array.getLength(secondObject); Object array = Array.newInstance(o1Type, firstObjectSize + secondObjectSize); int offset = 0, i; for (i = 0; i < firstObjectSize; i++, offset++) Array.set(array, offset, Array.get(firstObject, i)); for (i = 0; i < secondObjectSize; i++, offset++) Array.set(array, offset, Array.get(secondObject, i)); return array; }
From source file:Main.java
/** * Get the path of a certain volume.//from ww w . ja v a 2 s. c om * * @param volumeId The volume id. * @return The path. */ private static String getVolumePath(final String volumeId) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return null; } try { StorageManager mStorageManager = (StorageManager) applicationContext .getSystemService(Context.STORAGE_SERVICE); Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); Method getUuid = storageVolumeClazz.getMethod("getUuid"); Method getPath = storageVolumeClazz.getMethod("getPath"); Method isPrimary = storageVolumeClazz.getMethod("isPrimary"); Object result = getVolumeList.invoke(mStorageManager); final int length = Array.getLength(result); for (int i = 0; i < length; i++) { Object storageVolumeElement = Array.get(result, i); String uuid = (String) getUuid.invoke(storageVolumeElement); Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement); // primary volume? if (primary && PRIMARY_VOLUME_NAME.equals(volumeId)) { return (String) getPath.invoke(storageVolumeElement); } // other volumes? if (uuid != null) { if (uuid.equals(volumeId)) { return (String) getPath.invoke(storageVolumeElement); } } } // not found. return null; } catch (Exception ex) { return null; } }
From source file:Main.java
private static Object arrayRemoveInternal(Object array, Object elementToRemove) { boolean found = false; final int oldLength = Array.getLength(array); if (oldLength == 0) { return array; }/*from w ww . j av a2 s . com*/ final int newLength = oldLength - 1; final Object result = Array.newInstance(array.getClass().getComponentType(), newLength); int nextIndex = 0; for (int i = 0; i < oldLength; i++) { final Object e = Array.get(array, i); if (e.equals(elementToRemove)) { found = true; } else { if (nextIndex == newLength) { break; } Array.set(result, nextIndex, e); nextIndex++; } } if (!found) { return array; } return result; }