Example usage for java.lang.reflect Array newInstance

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

Introduction

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

Prototype

public static Object newInstance(Class<?> componentType, int... dimensions)
        throws IllegalArgumentException, NegativeArraySizeException 

Source Link

Document

Creates a new array with the specified component type and dimensions.

Usage

From source file:Util.java

/*********************************************************************
* Prepends an Object to an Object array.
*
* <p>//from   ww w.j a va2 s.  co  m
* Example:
* <code>
* <pre>
* String [ ]  stringArray
*   = ( String [ ] ) ArrayLib.prepend ( new String [ ] { }, "" );
* </pre>
* </code>
* </p>
*
* @throws NullArgumentException
*
*   If either argument is null.
*
* @return
*
*   Returns a new array with the same component type as the old array.
*********************************************************************/
public static Object[] prepend(Object[] oldArray, Object o)
//////////////////////////////////////////////////////////////////////
{

    Object[] newArray = (Object[]) Array.newInstance(oldArray.getClass().getComponentType(),
            oldArray.length + 1);

    System.arraycopy(oldArray, 0, newArray, 1, oldArray.length);

    newArray[0] = o;

    return newArray;
}

From source file:Util.java

/*********************************************************************
 * Appends an Object to an Object array.
 *
 * <p>//from  ww  w  .  ja  va  2 s  . com
 * Example:
 * <code>
 * <pre>
 * String [ ]  stringArray
 *   = ( String [ ] ) ArrayLib.append ( new String [ ] { }, "" );
 * </pre>
 * </code>
 * </p>
 *
 * @throws NullArgumentException
 *
 *   If either argument is null.
 *
 * @return
 *
 *   Returns a new array with the same component type as the old array.
 *********************************************************************/
public static Object[] append(Object[] oldArray, Object o)
//////////////////////////////////////////////////////////////////////
{
    Object[] newArray = (Object[]) Array.newInstance(oldArray.getClass().getComponentType(),
            oldArray.length + 1);

    System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);

    newArray[oldArray.length] = o;

    return newArray;
}

From source file:Main.java

/**
 * This will return a class of the type T[] from a given class. When we read
 * from the AG pipe, Java needs a reference to a generic array type.
 *
 * @param klass a class to turn into an array class
 * @param <T> the type of the class.
 * /* w  ww  .ja  v  a2  s  . c om*/
 * @return an array of klass with a length of 1
 */
public static <T> Class<T[]> asArrayClass(Class<T> klass) {
    return (Class<T[]>) Array.newInstance(klass, 1).getClass();
}

From source file:Main.java

public static Object arrayExpandAddElements(Object array, Object[] elementsToAdd) {
    Class cl = array.getClass();/*from  w  ww.  j a v a  2 s  .c o  m*/
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + elementsToAdd.length;
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    for (int i = 0; i < elementsToAdd.length; i++) {
        Array.set(newArray, length + i, elementsToAdd[i]);
    }
    return newArray;
}

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. jav  a  2s  . com*/
            Array.set(result, k, Array.get(arrayRhs, k - i));
        }
    }
    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E[] arrayOf(Class<E> elementClass, Object arrayOrElement) {
    if (arrayOrElement == null) {
        return null;
    }/*from   w w  w. j  ava2 s.c  o 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 {// w  w w. j av a 2  s.  c  o 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:ArrayDemo.java

/** 
 * Copy an array and return the copy.//from   w  w  w  .  j a v  a 2s  .  c om
 *
 * @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:ArrayUtil.java

/**
 * Copies the elements of the {@code oldArray} to a new array with extra space to 
 * append the given element {@code toAppend}.
 *//*from  w  ww  .j  a  v a  2 s  . co m*/
@SuppressWarnings("unchecked")
public static <T> T[] append(T[] oldArray, T toAppend) {
    Class<?> component = oldArray.getClass().getComponentType();
    T[] array = (T[]) Array.newInstance(component, oldArray.length + 1);
    System.arraycopy(oldArray, 0, array, 0, oldArray.length);
    array[oldArray.length] = toAppend;
    return array;
}

From source file:Main.java

protected static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]
            : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;// ww w .ja  v a  2  s.c  o m
}