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:com.processing.core.PApplet.java

/**
 * Shortcut to copy the entire contents of
 * the source into the destination array.
 * Identical to <CODE>arraycopy(src, 0, dst, 0, src.length);</CODE>
 *///from  w  w  w  .  j a  v a 2 s  . c o m
static public void arrayCopy(Object src, Object dst) {
    System.arraycopy(src, 0, dst, 0, Array.getLength(src));
}

From source file:com.processing.core.PApplet.java

static public Object expand(Object array) {
    return expand(array, Array.getLength(array) << 1);
}

From source file:com.processing.core.PApplet.java

static public Object expand(Object list, int newSize) {
    Class<?> type = list.getClass().getComponentType();
    Object temp = Array.newInstance(type, newSize);
    System.arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize));
    return temp;/*from   w w w  . j  av  a2s  .c  om*/
}

From source file:com.processing.core.PApplet.java

static public Object append(Object b, Object value) {
    int length = Array.getLength(b);
    b = expand(b, length + 1);/*w w w  .  j a  v  a2  s .c o m*/
    Array.set(b, length, value);
    return b;
}

From source file:com.processing.core.PApplet.java

static public Object shorten(Object list) {
    int length = Array.getLength(list);
    return subset(list, 0, length - 1);
}

From source file:com.processing.core.PApplet.java

static final public Object splice(Object list, Object v, int index) {
    Object[] outgoing = null;/*from  w w  w.ja  v  a 2s  .  co m*/
    int length = Array.getLength(list);

    // check whether item being spliced in is an array
    if (v.getClass().getName().charAt(0) == '[') {
        int vlength = Array.getLength(v);
        outgoing = new Object[length + vlength];
        System.arraycopy(list, 0, outgoing, 0, index);
        System.arraycopy(v, 0, outgoing, index, vlength);
        System.arraycopy(list, index, outgoing, index + vlength, length - index);

    } else {
        outgoing = new Object[length + 1];
        System.arraycopy(list, 0, outgoing, 0, index);
        Array.set(outgoing, index, v);
        System.arraycopy(list, index, outgoing, index + 1, length - index);
    }
    return outgoing;
}

From source file:com.processing.core.PApplet.java

static public Object subset(Object list, int start) {
    int length = Array.getLength(list);
    return subset(list, start, length - start);
}

From source file:com.processing.core.PApplet.java

static public Object concat(Object a, Object b) {
    Class<?> type = a.getClass().getComponentType();
    int alength = Array.getLength(a);
    int blength = Array.getLength(b);
    Object outgoing = Array.newInstance(type, alength + blength);
    System.arraycopy(a, 0, outgoing, 0, alength);
    System.arraycopy(b, 0, outgoing, alength, blength);
    return outgoing;
}

From source file:com.processing.core.PApplet.java

static public Object reverse(Object list) {
    Class<?> type = list.getClass().getComponentType();
    int length = Array.getLength(list);
    Object outgoing = Array.newInstance(type, length);
    for (int i = 0; i < length; i++) {
        Array.set(outgoing, i, Array.get(list, (length - 1) - i));
    }//from  w  w  w  .j  a v a 2s .  c  o m
    return outgoing;
}

From source file:ca.oson.json.Oson.java

private int[] json2ArrayInt(FieldData objectDTO) {
    if (objectDTO.valueToProcess == null) {
        return null;
    }//from ww  w .  j  av a2s. c o m

    Function function = objectDTO.getDeserializer();

    Object returnedValue = objectDTO.valueToProcess;

    if (function != null) {
        try {

            // suppose to return String, but in case not, try to process
            if (function instanceof Json2DataMapperFunction) {
                DataMapper classData = new DataMapper(objectDTO.returnType, objectDTO.valueToProcess,
                        objectDTO.classMapper, objectDTO.level, getPrettyIndentation());
                returnedValue = ((Json2DataMapperFunction) function).apply(classData);

            } else if (function instanceof Json2FieldDataFunction) {
                Json2FieldDataFunction f = (Json2FieldDataFunction) function;
                FieldData fieldData = objectDTO.clone();

                returnedValue = f.apply(fieldData);

            } else if (function instanceof Json2ArrayFunction) {
                returnedValue = ((Json2ArrayFunction) function).apply(objectDTO.valueToProcess);

            } else {
                returnedValue = function.apply(objectDTO.valueToProcess);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    if (returnedValue == null) {
        return null;

    } else if (returnedValue.getClass().isArray()) {
        if (returnedValue.getClass() == int[].class) {
            return (int[]) returnedValue;
        }

        int size = Array.getLength(returnedValue);
        int[] arr = new int[size];
        int i = 0, j = 0;
        while (j < size) {
            try {
                arr[i] = (int) NumberUtil.getNumber(Array.get(returnedValue, j), int.class);
                i++;
            } catch (Exception ex) {
            }
            j++;
        }

        if (i == size) {
            return arr;
        }
        return Arrays.copyOfRange(arr, 0, i);

    } else if (!Collection.class.isAssignableFrom(returnedValue.getClass())) {
        return null;
    }

    Collection values = (Collection) returnedValue;

    int size = values.size();
    int[] arr = new int[size];
    int i = 0;

    for (Object value : values) {
        if (value != null) {
            try {
                arr[i] = (int) NumberUtil.getNumber(value, int.class);
                i++;
            } catch (Exception ex) {
            }
        }
    }

    if (i == size) {
        return arr;
    }
    return Arrays.copyOfRange(arr, 0, i);
}