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:Main.java

private static Object[] copyOf(Object[] obj, int newSize, Class<? extends Object> newType) {
    Object[] copy = ((Object) newType == (Object) Object[].class) ? (Object[]) new Object[newSize]
            : (Object[]) Array.newInstance(newType.getComponentType(), newSize);
    return copy;//w w  w .j  av a2s  .  com
}

From source file:Main.java

/**
 * Appends an element to a copy of the array and returns the copy.
 * @param array The original array, or null to represent an empty array.
 * @param element The element to add.//  w  w  w . java2s .  c  o m
 * @return A new array that contains all of the elements of the original array
 * with the specified element added at the end.
 */
@SuppressWarnings("unchecked")
public static <T> T[] appendElement(final Class<T> kind, final T[] array, final T element) {
    final T[] result;
    final int end;
    if (array != null) {
        end = array.length;
        result = (T[]) Array.newInstance(kind, end + 1);
        System.arraycopy(array, 0, result, 0, end);
    } else {
        end = 0;
        result = (T[]) Array.newInstance(kind, 1);
    }
    result[end] = element;
    return result;
}

From source file:Main.java

/**
 * Concatenates two arrays/*from  w ww  .  j  av  a  2s .  c o m*/
 * 
 * @param existingArray
 *            an existing array
 * @param elements
 *            the elements to add to the end of it
 * @return
 */
@SuppressWarnings("unchecked")
public static <E> E[] array(final E[] existingArray, final E... elements) {
    if (existingArray == null) {
        return elements;
    }
    Object result = Array.newInstance(existingArray.getClass().getComponentType(),
            existingArray.length + elements.length);
    System.arraycopy(existingArray, 0, result, 0, existingArray.length);
    System.arraycopy(elements, 0, result, existingArray.length, elements.length);
    return (E[]) result;
}

From source file:Main.java

public static <T> T[] concatenate(T[] lhs, T[] rhs) {
    int lhsLen = lhs.length;
    int rhsLen = rhs.length;

    @SuppressWarnings("unchecked")
    T[] result = (T[]) Array.newInstance(lhs.getClass().getComponentType(), lhsLen + rhsLen);
    System.arraycopy(lhs, 0, result, 0, lhsLen);
    System.arraycopy(rhs, 0, result, lhsLen, rhsLen);

    return result;
}

From source file:io.fabric8.jolokia.support.JolokiaHelpers.java

public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException {
    if (clazz.isArray()) {
        if (value instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) value;
            Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size());
            int idx = 0;
            for (Object element : jsonArray) {
                Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element));
            }/*from  w w w.j  ava 2 s  .  co m*/
            return javaArray;
        } else {
            return null;
        }
    } else if (String.class.equals(clazz)) {
        return (value != null) ? value.toString() : null;
    } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) {
        Number number = asNumber(value);
        return number != null ? number.byteValue() : null;
    } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
        Number number = asNumber(value);
        return number != null ? number.shortValue() : null;
    } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
        Number number = asNumber(value);
        return number != null ? number.intValue() : null;
    } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
        Number number = asNumber(value);
        return number != null ? number.longValue() : null;
    } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
        Number number = asNumber(value);
        return number != null ? number.floatValue() : null;
    } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
        Number number = asNumber(value);
        return number != null ? number.doubleValue() : null;
    } else if (value instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) value;
        if (!JSONObject.class.isAssignableFrom(clazz)) {
            String json = jsonObject.toJSONString();
            return getObjectMapper().readerFor(clazz).readValue(json);
        }
    }
    return value;
}

From source file:ArrayUtil.java

/**
 * Adds the object to the array.// ww  w. jav a  2  s. c  o m
 * 
 * @param <T>
 *            the type
 * @param array
 *            the array
 * @param obj
 *            the object
 * @return the added array
 */
@SuppressWarnings("unchecked")
public static <T> T[] add(T[] array, T obj) {
    if (array == null && obj == null) {
        return null;
    }
    Class<?> clazz = obj != null ? obj.getClass() : array.getClass().getComponentType();
    int length = array != null ? array.length : 0;
    T[] newArray = (T[]) Array.newInstance(clazz, length + 1);
    if (array != null) {
        System.arraycopy(array, 0, newArray, 0, length);
    }
    newArray[length] = obj;
    return newArray;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> E[] toArray(Enumeration<E> enumeration, Class<E> type) {
    ArrayList<E> elements = new ArrayList<E>();
    while (enumeration.hasMoreElements()) {
        elements.add(enumeration.nextElement());
    }/* ww w . j  a  v  a2  s  . c  om*/
    E[] array = (E[]) Array.newInstance(type, elements.size());
    elements.toArray(array);
    return array;
}

From source file:Main.java

/**
 * Returns an array containing all of the elements in the given collection. This is a
 * compile-time type-safe alternative to {@link Collection#toArray(Object[])}.
 * /*from   www  .j a v a 2 s  . com*/
 * @param collection the source collection
 * @param clazz the type of the array elements
 * @param <A> the type of the array elements
 * @return an array of type <code>A</code> containing all of the elements in the given
 *         collection
 * 
 * @throws NullPointerException if the specified collection or class is null
 * 
 * @see <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7023484">Sun bug 7023484</a>
 */
public static <A> A[] toArray(Collection<? extends A> collection, Class<A> clazz) {
    Object array = Array.newInstance(clazz, collection.size());
    @SuppressWarnings("unchecked")
    A[] typedArray = collection.toArray((A[]) array);
    return typedArray;
}

From source file:Main.java

public static Object[] addAll(Object[] array1, Object[] array2) {
    if (array1 == null) {
        return clone(array2);
    } else if (array2 == null) {
        return clone(array1);
    }/*ww w.  ja  v a 2 s . co m*/
    Object[] joinedArray = (Object[]) Array.newInstance(array1.getClass().getComponentType(),
            array1.length + array2.length);
    System.arraycopy(array1, 0, joinedArray, 0, array1.length);
    System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
    return joinedArray;
}

From source file:Main.java

/**
 * Returns an empty array of the specified type.  The intent is that
 * it will return the same empty array every time to avoid reallocation,
 * although this is not guaranteed.//  ww w.ja  v a  2 s. c o m
 */
public static <T> T[] emptyArray(Class kind) {
    if (kind == Object.class) {
        return (T[]) EMPTY;
    }

    int bucket = ((System.identityHashCode(kind) / 8) & 0x7FFFFFFF) % CACHE_SIZE;
    Object cache = sCache[bucket];

    if (cache == null || cache.getClass().getComponentType() != kind) {
        cache = Array.newInstance(kind, 0);
        sCache[bucket] = cache;

        // Log.e("cache", "new empty " + kind.getName() + " at " + bucket);
    }

    return (T[]) cache;
}