Example usage for java.lang.reflect Array get

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

Introduction

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

Prototype

public static native Object get(Object array, int index)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Returns the value of the indexed component in the specified array object.

Usage

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

private byte[] json2ArrayByte(FieldData objectDTO) {
    if (objectDTO.valueToProcess == null) {
        return null;
    }//from  ww w  .  ja  va2s.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() == byte[].class) {
            return (byte[]) returnedValue;
        }

        int size = Array.getLength(returnedValue);
        byte[] arr = new byte[size];
        int i = 0, j = 0;
        while (j < size) {
            try {
                arr[i] = (byte) NumberUtil.getNumber(Array.get(returnedValue, j), byte.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();
    byte[] arr = new byte[size];
    int i = 0;

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

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

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

private char[] json2ArrayChar(FieldData objectDTO) {
    if (objectDTO.valueToProcess == null) {
        return null;
    }//w  w w .  jav a2  s  .  com

    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() == char[].class) {
            return (char[]) returnedValue;
        }

        int size = Array.getLength(returnedValue);
        char[] arr = new char[size];
        int i = 0, j = 0;
        while (j < size) {
            try {
                Object obj = Array.get(returnedValue, j);
                if (obj.getClass() == Character.class || obj.getClass() == char.class) {
                    arr[i] = (char) obj;

                } else {
                    String str = StringUtil.unquote(obj.toString(), isEscapeHtml());
                    arr[i] = str.charAt(0);
                }
                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();
    char[] arr = new char[size];
    int i = 0;

    for (Object obj : values) {
        if (obj != null) {
            try {
                if (obj.getClass() == Character.class || obj.getClass() == char.class) {
                    arr[i] = (char) obj;

                } else {
                    String str = StringUtil.unquote(obj.toString(), isEscapeHtml());
                    arr[i] = str.charAt(0);
                }
                i++;
            } catch (Exception ex) {
            }
        }
    }

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

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

private float[] json2ArrayFloat(FieldData objectDTO) {
    if (objectDTO.valueToProcess == null) {
        return null;
    }/*from   ww  w .j  a v  a2  s  . 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() == float[].class) {
            return (float[]) returnedValue;
        }

        int size = Array.getLength(returnedValue);
        float[] arr = new float[size];
        int i = 0, j = 0;
        while (j < size) {
            try {
                arr[i] = (float) NumberUtil.getNumber(Array.get(returnedValue, j), float.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();
    float[] arr = new float[size];
    int i = 0;

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

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

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

private double[] json2ArrayDouble(FieldData objectDTO) {
    if (objectDTO.valueToProcess == null) {
        return null;
    }//w  w  w.j a  v a 2 s.  c  om

    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() == double[].class) {
            return (double[]) returnedValue;
        }

        int size = Array.getLength(returnedValue);
        double[] arr = new double[size];
        int i = 0, j = 0;
        while (j < size) {
            try {
                arr[i] = (double) NumberUtil.getNumber(Array.get(returnedValue, j), double.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();
    double[] arr = new double[size];
    int i = 0;

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

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

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

private long[] json2ArrayLong(FieldData objectDTO) {
    if (objectDTO.valueToProcess == null) {
        return null;
    }/*from  w  w w.  j  a  va 2 s . 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() == long[].class) {
            return (long[]) returnedValue;
        }

        int size = Array.getLength(returnedValue);
        long[] arr = new long[size];
        int i = 0, j = 0;
        while (j < size) {
            try {
                arr[i] = (long) NumberUtil.getNumber(Array.get(returnedValue, j), long.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();
    long[] arr = new long[size];
    int i = 0;

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

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

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

private short[] json2ArrayShort(FieldData objectDTO) {
    if (objectDTO.valueToProcess == null) {
        return null;
    }//from   www . jav a2  s  .  c om

    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() == short[].class) {
            return (short[]) returnedValue;
        }

        int size = Array.getLength(returnedValue);
        short[] arr = new short[size];
        int i = 0, j = 0;
        while (j < size) {
            try {
                arr[i] = (short) NumberUtil.getNumber(Array.get(returnedValue, j), short.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();
    short[] arr = new short[size];
    int i = 0;

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

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

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

private boolean[] json2ArrayBoolean(FieldData objectDTO) {
    if (objectDTO.valueToProcess == null) {
        return null;
    }//w ww.  j  a v  a  2s .  com

    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() == boolean[].class) {
            return (boolean[]) returnedValue;
        }

        int size = Array.getLength(returnedValue);
        boolean[] arr = new boolean[size];
        int i = 0, j = 0;
        while (j < size) {
            try {
                arr[i] = BooleanUtil.object2Boolean(Array.get(returnedValue, j));
                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();
    boolean[] arr = new boolean[size];
    int i = 0;

    for (Object value : values) {
        if (value != null) {
            try {
                arr[i] = BooleanUtil.object2Boolean(value);
                i++;
            } catch (Exception ex) {
            }
        }
    }

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

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

private <E> String array2Json(FieldData objectDTO) {
    Object value = objectDTO.valueToProcess;
    Class<?> returnType = objectDTO.returnType;

    int size = 0;
    if (value != null && (size = Array.getLength(value)) > 0) {
        Function function = objectDTO.getSerializer();
        String valueToReturn = null;

        if (function != null) {
            try {

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

                } else if (function instanceof Array2JsonFunction) {
                    return ((Array2JsonFunction) function).apply((E[]) value);

                } else {

                    Object returnedValue = null;
                    if (function instanceof FieldData2JsonFunction) {
                        FieldData2JsonFunction f = (FieldData2JsonFunction) function;
                        FieldData fieldData = objectDTO.clone();
                        returnedValue = f.apply(fieldData);
                    } else {
                        returnedValue = function.apply(value);
                    }//from w  w  w . j  a  va2  s  .  c  o  m

                    if (returnedValue instanceof Optional) {
                        returnedValue = ObjectUtil.unwrap(returnedValue);
                    }

                    if (returnedValue == null) {
                        return null;

                    } else if (Array.class.isAssignableFrom(returnedValue.getClass())) {
                        // keep on processing
                        value = returnedValue;
                        size = Array.getLength(value);

                    } else {
                        objectDTO.valueToProcess = returnedValue;
                        return object2String(objectDTO);
                    }
                }

            } catch (Exception ex) {
            }
        }

        String repeated = getPrettyIndentationln(objectDTO.level);
        objectDTO.incrLevel();
        String repeatedItem = getPrettyIndentationln(objectDTO.level);
        Class ctype = objectDTO.getComponentType(getJsonClassType());

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

        for (int i = 0; i < size; i++) {
            Object componentValue = Array.get(value, i);
            String str = null;
            if (componentValue != null) {
                FieldData newFieldData = new FieldData(componentValue, componentValue.getClass(),
                        objectDTO.json2Java, objectDTO.level, objectDTO.set);
                newFieldData.returnType = guessComponentType(newFieldData);
                newFieldData.fieldMapper = objectDTO.fieldMapper;
                str = object2Json(newFieldData);
            }

            if (str == null
                    && ((ctype != null && ctype.isPrimitive()) || getDefaultType() == JSON_INCLUDE.DEFAULT)) {
                str = getDefaultValue(ctype).toString();
            }

            list.add(str);
        }

        if (objectDTO.classMapper.orderArrayAndList) {
            Collections.sort(list);
        }

        StringBuilder sbuilder = new StringBuilder();
        for (String str : list) {
            sbuilder.append(repeatedItem + str + ",");
        }

        String str = sbuilder.toString();
        size = str.length();
        if (size > 0) {
            return "[" + str.substring(0, size - 1) + repeated + "]";
        }
    }

    return array2JsonDefault(objectDTO);
}

From source file:com.clark.func.Functions.java

/**
 * Clone an object.//from w w  w.j av  a 2 s. c o  m
 * 
 * @param <T>
 *            the type of the object
 * @param o
 *            the object to clone
 * @return the clone if the object implements {@link Cloneable} otherwise
 *         <code>null</code>
 * @throws CloneFailedException
 *             if the object is cloneable and the clone operation fails
 * @since 3.0
 */
public static <T> T objectClone(final T o) {
    if (o instanceof Cloneable) {
        final Object result;
        if (o.getClass().isArray()) {
            final Class<?> componentType = o.getClass().getComponentType();
            if (!componentType.isPrimitive()) {
                result = ((Object[]) o).clone();
            } else {
                int length = Array.getLength(o);
                result = Array.newInstance(componentType, length);
                while (length-- > 0) {
                    Array.set(result, length, Array.get(o, length));
                }
            }
        } else {
            try {
                final Method clone = o.getClass().getMethod("clone");
                result = clone.invoke(o);
            } catch (final NoSuchMethodException e) {
                throw new CloneFailedException(
                        "Cloneable type " + o.getClass().getName() + " has no clone method", e);
            } catch (final IllegalAccessException e) {
                throw new CloneFailedException("Cannot clone Cloneable type " + o.getClass().getName(), e);
            } catch (final InvocationTargetException e) {
                throw new CloneFailedException("Exception cloning Cloneable type " + o.getClass().getName(),
                        e.getCause());
            }
        }
        @SuppressWarnings("unchecked")
        final T checked = (T) result;
        return checked;
    }

    return null;
}