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

public static long[] newUnpaddedLongArray(int minLen) {
    return (long[]) Array.newInstance(long.class, minLen);
}

From source file:Main.java

public static byte[] newUnpaddedByteArray(int minLen) {
    return (byte[]) Array.newInstance(byte.class, minLen);
}

From source file:Main.java

public static float[] newUnpaddedFloatArray(int minLen) {
    return (float[]) Array.newInstance(float.class, minLen);
}

From source file:Main.java

public static Object[] newUnpaddedObjectArray(int minLen) {
    return (Object[]) Array.newInstance(Object.class, minLen);
}

From source file:Main.java

public static boolean[] newUnpaddedBooleanArray(int minLen) {
    return (boolean[]) Array.newInstance(boolean.class, minLen);
}

From source file:Main.java

public static String[] getStackTrace(long threadID) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE);

    List<String> buf = new ArrayList<String>();

    if (threadInfo == null) {
        return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
    }/*from w w w  .jav  a2s. c o m*/

    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        buf.add("\t" + ste.toString());

        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch (ts) {
            case BLOCKED:
                buf.add("\t-  blocked on " + threadInfo.getLockInfo());
                break;
            case WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            case TIMED_WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                buf.add("\t-  locked " + mi);
            }
        }
    }

    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        buf.add("\n\tNumber of locked synchronizers = " + locks.length);
        for (LockInfo li : locks) {
            buf.add("\t- " + li);
        }
    }

    return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] newUnpaddedArray(Class<T> clazz, int minLen) {
    return (T[]) Array.newInstance(clazz, minLen);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] toArray(Set<T> items, Class<T> tClass) {
    if (items == null || items.size() == 0)
        return null;
    int size = items.size();
    try {//from  ww  w . j av  a2 s  . com
        T[] array = (T[]) Array.newInstance(tClass, size);
        return items.toArray(array);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] arrayMerge(final T[]... arrays) {
    int count = 0;
    for (final T[] array : arrays) {
        count += array.length;/*from ww w. j a  v a2 s  .c  om*/
    }

    final T[] mergedArray = (T[]) Array.newInstance(arrays[0][0].getClass(), count);
    int start = 0;
    for (final T[] array : arrays) {
        System.arraycopy(array, 0, mergedArray, start, array.length);
        start += array.length;
    }
    return mergedArray;
}

From source file:Main.java

public static <T> T[] concat(T[] A, T[] B) {
    final Class<?> typeofA = A.getClass().getComponentType();
    @SuppressWarnings("unchecked")
    T[] C = (T[]) Array.newInstance(typeofA, A.length + B.length);
    System.arraycopy(A, 0, C, 0, A.length);
    System.arraycopy(B, 0, C, A.length, B.length);

    return C;/*  ww w .  j a v  a 2s.  c om*/
}