Example usage for java.lang.reflect Array get

List of usage examples for java.lang.reflect Array get

Introduction

In this page you can find the example usage for java.lang.reflect Array get.

Prototype

public static native Object get(Object array, int index)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Returns the value of the indexed component in the specified array object.

Usage

From source file:Main.java

public static boolean isAllNullArray(Object array) {
    if (array == null) {
        throw new NullPointerException();
    }/*from   w ww  . j av a 2  s . c  o  m*/
    if (!array.getClass().isArray()) {
        throw new IllegalArgumentException("Expected array but received " + array.getClass());
    }
    for (int i = 0; i < Array.getLength(array); i++) {
        if (Array.get(array, i) != null) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

/**
 * {@link Arrays#toString(Object[])} with {@link Arrays#toString(Object[])} for array elements
 *///from w  ww .ja va2s .  c  om
public static Object arrayToString(Object obj) {
    if (obj != null && obj.getClass().isArray()) {
        int len = Array.getLength(obj);
        String[] strings = new String[len];
        for (int i = 0; i < len; i++) {
            strings[i] = String.valueOf(Array.get(obj, i));
        }
        return Arrays.toString(strings);
    } else {
        return obj;
    }
}

From source file:Main.java

public static Object copyOf(Object src) {
    int srcLength = Array.getLength(src);
    Class<?> srcComponentType = src.getClass().getComponentType();
    Object dest = Array.newInstance(srcComponentType, srcLength);
    if (srcComponentType.isArray()) {
        for (int i = 0; i < Array.getLength(src); i++) {
            Array.set(dest, i, copyOf(Array.get(src, i)));
        }/*  w ww.  ja  v a  2s  .  c om*/
    } else {
        System.arraycopy(src, 0, dest, 0, srcLength);
    }
    return dest;
}

From source file:Main.java

private static Object combineArray(Object arrayLhs, Object arrayRhs) {
    Class<?> localClass = arrayLhs.getClass().getComponentType();
    int i = Array.getLength(arrayLhs);
    int j = i + Array.getLength(arrayRhs);
    Object result = Array.newInstance(localClass, j);
    for (int k = 0; k < j; ++k) {
        if (k < i) {
            Array.set(result, k, Array.get(arrayLhs, k));
        } else {//from  w  w w. java2 s.c  o  m
            Array.set(result, k, Array.get(arrayRhs, k - i));
        }
    }
    return result;
}

From source file:ArrayDemo.java

/** 
 * Copy an array and return the copy./*from w w w  .  j a  va  2  s.  c  o  m*/
 *
 * @param input The array to copy.
 *
 * @return The coppied array.
 *
 * @throws IllegalArgumentException If input is not an array.
 */
public static Object copyArray(final Object input) {
    final Class type = input.getClass();
    if (!type.isArray()) {
        throw new IllegalArgumentException();
    }
    final int length = Array.getLength(input);
    final Class componentType = type.getComponentType();

    final Object result = Array.newInstance(componentType, length);
    for (int idx = 0; idx < length; idx++) {
        Array.set(result, idx, Array.get(input, idx));
    }
    return result;
}

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  w ww. j  av  a  2  s. com
}

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  v  a 2s .com
    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

public static int deepHashCode(Object obj) {
    Set visited = new HashSet();
    LinkedList<Object> stack = new LinkedList<Object>();
    stack.addFirst(obj);/* w  w  w  .ja  va 2  s  . 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

static final public void resetThreadLocal(Object lock) {
    synchronized (lock) {
        // Get a reference to the thread locals table of the current thread
        try {//from   www.j  av  a  2 s  . com
            Thread thread = Thread.currentThread();

            threadLocalsField.setAccessible(true);
            Object threadLocalTable = threadLocalsField.get(thread);

            // Get a reference to the array holding the thread local variables inside the
            // ThreadLocalMap of the current thread

            tableField.setAccessible(true);
            Object table = tableField.get(threadLocalTable);

            // The key to the ThreadLocalMap is a WeakReference object. The referent field of this object
            // is a reference to the actual ThreadLocal variable
            referentField.setAccessible(true);

            for (int i = 0; i < Array.getLength(table); i++) {
                // Each entry in the table array of ThreadLocalMap is an Entry object
                // representing the thread local reference and its value
                Object entry = Array.get(table, i);
                if (entry != null) {
                    // Get a reference to the thread local object and remove it from the table
                    ThreadLocal<?> threadLocal = (ThreadLocal<?>) referentField.get(entry);
                    threadLocal.remove();
                }
            }
        } catch (Exception e) {
        } //noop
        finally {
            try {
                if (referentField != null)
                    referentField.setAccessible(false);
                if (tableField != null)
                    tableField.setAccessible(false);
                if (threadLocalsField != null)
                    threadLocalsField.setAccessible(false);
            } catch (Throwable e) {
                // TODO: log error
            }
        }
    }
}

From source file:Main.java

/**
 * Get the path of a certain volume.//from   w w  w  . j  av  a 2  s .  c  o  m
 *
 * @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;
    }
}