Example usage for java.lang.reflect Array getLength

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public static native int getLength(Object array) throws IllegalArgumentException;

Source Link

Document

Returns the length of the specified array object, as an int .

Usage

From source file:Main.java

public static boolean isAllNullArray(Object array) {
    if (array == null) {
        throw new NullPointerException();
    }//ww  w  .  j a v 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

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 {// www.j  a  v  a 2s  .  c  om
            Array.set(result, k, Array.get(arrayRhs, k - i));
        }
    }
    return result;
}

From source file:ArrayDemo.java

/** 
 * Copy an array and return the copy.// ww 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:Main.java

@SuppressWarnings("unchecked")
public static Object searchProperty(Object leftParameter, String name) throws Exception {
    Class<?> leftClass = leftParameter.getClass();
    Object result;/*w  ww .  j  av a  2s. com*/
    if (leftParameter.getClass().isArray() && "length".equals(name)) {
        result = Array.getLength(leftParameter);
    } else if (leftParameter instanceof Map) {
        result = ((Map<Object, Object>) leftParameter).get(name);
    } else {
        try {
            String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
            Method method = leftClass.getMethod(getter, new Class<?>[0]);
            if (!method.isAccessible()) {
                method.setAccessible(true);
            }
            result = method.invoke(leftParameter, new Object[0]);
        } catch (NoSuchMethodException e2) {
            try {
                String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1);
                Method method = leftClass.getMethod(getter, new Class<?>[0]);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                result = method.invoke(leftParameter, new Object[0]);
            } catch (NoSuchMethodException e3) {
                Field field = leftClass.getField(name);
                result = field.get(leftParameter);
            }
        }
    }
    return result;
}

From source file:Main.java

public static int arraysLength(@NonNull final Object... arrays) {
    int length = 0;
    for (Object array : arrays) {
        if (array == null)
            continue;
        length += Array.getLength(array);
    }/*from ww  w.j  ava2  s .c om*/
    return length;
}

From source file:Main.java

public static boolean overrideClassLoader(ClassLoader cl, File dex, File opt) {
    try {// w  w w  .  ja  v  a  2  s. co  m
        ClassLoader bootstrap = cl.getParent();
        Field fPathList = BaseDexClassLoader.class.getDeclaredField("pathList");
        fPathList.setAccessible(true);
        Object pathList = fPathList.get(cl);
        Class cDexPathList = bootstrap.loadClass("dalvik.system.DexPathList");
        Field fDexElements = cDexPathList.getDeclaredField("dexElements");
        fDexElements.setAccessible(true);
        Object dexElements = fDexElements.get(pathList);
        DexClassLoader cl2 = new DexClassLoader(dex.getAbsolutePath(), opt.getAbsolutePath(), null, bootstrap);
        Object pathList2 = fPathList.get(cl2);
        Object dexElements2 = fDexElements.get(pathList2);
        Object element2 = Array.get(dexElements2, 0);
        int n = Array.getLength(dexElements) + 1;
        Object newDexElements = Array.newInstance(fDexElements.getType().getComponentType(), n);
        Array.set(newDexElements, 0, element2);
        for (int i = 0; i < n - 1; i++) {
            Object element = Array.get(dexElements, i);
            Array.set(newDexElements, i + 1, element);
        }
        fDexElements.set(pathList, newDexElements);
        return true;
    } catch (Exception e) {
        Log.e("lcast", "fail to override classloader " + cl + " with " + dex, e);
        return false;
    }
}

From source file:com.ewcms.common.lang.EmptyUtil.java

/**
 * //  w w  w . j  a v a2 s  . com
 * 
 * @param value
 * @return 
 */
public static boolean isArrayEmpty(Object[] value) {
    if (isNull(value)) {
        return true;
    }
    return Array.getLength(value) == 0 ? true : false;
}

From source file:Main.java

public static void mergeArray(final Object dest, @NonNull final Object... arrays) {
    for (int i = 0, j = arrays.length, k = 0; i < j; i++) {
        final Object array = arrays[i];
        if (array == null)
            continue;
        final int length = Array.getLength(array);
        //noinspection SuspiciousSystemArraycopy
        System.arraycopy(array, 0, dest, k, length);
        k += length;/* w  ww.j  a  v  a  2 s.  c om*/
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E[] array(Class<E> elementClass, Object existingArray, E... elements) {
    if (existingArray == null) {
        return elements;
    }//  w  w  w  .  jav a2  s  .c  o m
    E[] result;
    if (existingArray.getClass().isArray()) {
        int length = Array.getLength(existingArray);
        result = (E[]) Array.newInstance(elementClass, length + elements.length);
        System.arraycopy(existingArray, 0, result, 0, length);
        System.arraycopy(elements, 0, result, length, elements.length);
    } else {
        result = (E[]) Array.newInstance(elementClass, 1 + elements.length);
        result[0] = (E) existingArray;
        System.arraycopy(elements, 0, result, 1, elements.length);
    }

    return result;
}

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 ww w.j  av a 2  s . co  m*/
            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
            }
        }
    }
}