Example usage for java.lang.reflect Array set

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

Introduction

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

Prototype

public static native void set(Object array, int index, Object value)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Sets the value of the indexed component of the specified array object to the specified new value.

Usage

From source file:Main.java

public static Object copyArray(final Object input) {
    final Class type = input.getClass();
    if (!type.isArray()) {
        throw new IllegalArgumentException();
    }/*w  w w .  j a  v  a  2s . co m*/
    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

/**
 * Get a double array from a list given//from w w w  .  ja  va  2  s . c  om
 * @param list List with Objects that can be translated to double
 * @param klass Class type of array
 * @return Object array of elements
 */
public static Object getObjectArray(List list, Class klass) {
    Object array = Array.newInstance(klass, list.size());
    for (int i = 0; i < list.size(); i++) {
        Array.set(array, i, list.get(i));
    }
    return array;
}

From source file:Main.java

public static Object combineArray(Object firstArray, Object secondArray) {
    int firstLength = Array.getLength(firstArray);
    int secondLength = Array.getLength(secondArray);
    int length = firstLength + secondLength;

    Class<?> componentType = firstArray.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, length);
    for (int i = 0; i < length; i++) {
        if (i < firstLength) {
            Array.set(newArray, i, Array.get(firstArray, i));
        } else {/*from w w  w.  j a v  a  2  s .c  o  m*/
            Array.set(newArray, i, Array.get(secondArray, i - firstLength));
        }
    }
    return newArray;
}

From source file:Main.java

public static <T> Object convertListToArray(List<T> list, Class<T> destEntryType) {

    Object outArray = Array.newInstance(destEntryType, list.size());
    int count = 0;
    int size = list.size();
    for (int i = 0; i < size; i++) {
        Object element = list.get(i);
        Array.set(outArray, count, element);
        count++;//from w ww  .j av a 2s .  c  o  m
    }
    if (destEntryType.isPrimitive())
        return outArray;
    else
        return (T[]) outArray;
}

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)));
        }/*from w  w  w  .j  a va 2 s  .c o  m*/
    } else {
        System.arraycopy(src, 0, dest, 0, srcLength);
    }
    return dest;
}

From source file:Main.java

public static Object arrayExpandAddSingle(Object array, Object elementsToAdd) {
    Class cl = array.getClass();/* www. j  a va2s .  c o  m*/
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + 1;
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    Array.set(newArray, length, elementsToAdd);
    return newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E[] arrayOf(Class<E> elementClass, Object arrayOrElement) {
    if (arrayOrElement == null) {
        return null;
    }//from www  .  j  a  va2  s  .  co  m
    if (arrayOrElement.getClass().isArray()) {
        return (E[]) arrayOrElement;
    }
    Object result = Array.newInstance(elementClass, 1);
    Array.set(result, 0, arrayOrElement);
    return (E[]) result;
}

From source file:Main.java

public static boolean overrideClassLoader(ClassLoader cl, File dex, File opt) {
    try {/*from w  ww  . j  a 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: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  ww  .  ja  v a2 s  .  co 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   www. ja  v  a 2s. 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;
}