Example usage for java.lang.reflect Array set

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

Introduction

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

Prototype

public static native void set(Object array, int index, Object value)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Sets the value of the indexed component of the specified array object to the specified new value.

Usage

From source file:org.kordamp.ezmorph.array.IntArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*w w  w  . j  a v  a2 s  . c  om*/

    if (INT_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (int[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(int.class, dimensions);
        IntMorpher morpher = isUseDefault() ? new IntMorpher(defaultValue) : new IntMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)));
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}

From source file:org.kordamp.ezmorph.array.BooleanArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from  ww w . j av a 2  s. c  om

    if (BOOLEAN_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (boolean[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(boolean.class, dimensions);
        BooleanMorpher morpher = isUseDefault() ? new BooleanMorpher(defaultValue) : new BooleanMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)) ? Boolean.TRUE : Boolean.FALSE);
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}

From source file:org.kordamp.ezmorph.array.CharArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from  w  ww . j ava2  s  .  c  o m

    if (CHAR_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (char[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(char.class, dimensions);
        CharMorpher morpher = isUseDefault() ? new CharMorpher(defaultValue) : new CharMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)));
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}

From source file:org.kordamp.ezmorph.array.LongArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }// w  w w  .j a  v  a  2s . co  m

    if (LONG_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (long[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(long.class, dimensions);
        LongMorpher morpher = isUseDefault() ? new LongMorpher(defaultValue) : new LongMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)));
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}

From source file:org.kordamp.ezmorph.array.FloatArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*from w w  w  .j a  v a 2  s.co m*/

    if (FLOAT_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (float[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(float.class, dimensions);
        FloatMorpher morpher = isUseDefault() ? new FloatMorpher(defaultValue) : new FloatMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)));
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}

From source file:org.kordamp.ezmorph.array.ShortArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from w  ww.  j  av a 2 s.com

    if (SHORT_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (short[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(short.class, dimensions);
        ShortMorpher morpher = isUseDefault() ? new ShortMorpher(defaultValue) : new ShortMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)));
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}

From source file:com.britesnow.snow.util.ObjectUtil.java

/**
 * <div class="notes"> <strong>Notes:</strong>
 * <ul>//from ww w.j  av  a  2s.  c  om
 * <li>So far does not safeguard any type conversion (will throw an runtime exception if parsing fail)</li>
 * </ul>
 * </div>
 * 
 * @param <T>
 * @param values
 *            String arrays of the value to be converted
 * @param cls
 * @param defaultValues
 * @return The typed array given a value of the String.
 */
public static final <T> T getValue(String[] values, Class<T> cls, T defaultValues) {
    if (values != null && cls.isArray()) {
        Class compCls = cls.getComponentType();
        T resultArray = (T) Array.newInstance(compCls, values.length);
        int i = 0;
        for (String v : values) {
            Object r = getValue(v, compCls, null);
            Array.set(resultArray, i++, r);
        }
        return resultArray;
    } else {
        return defaultValues;
    }

}

From source file:org.kordamp.ezmorph.array.DoubleArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//ww w  .ja va 2  s .  c om

    if (DOUBLE_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (double[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(double.class, dimensions);
        DoubleMorpher morpher = isUseDefault() ? new DoubleMorpher(defaultValue) : new DoubleMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)));
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}

From source file:org.openmrs.module.webservices.rest.web.ConversionUtil.java

/**
 * Converts the given object to the given type
 * //  w w w  . j a v  a 2  s .  com
 * @param object
 * @param toType a simple class or generic type
 * @return
 * @throws ConversionException
 * @should convert strings to locales
 * @should convert strings to enum values
 * @should convert to an array
 * @should convert to a class
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object convert(Object object, Type toType) throws ConversionException {
    if (object == null)
        return object;

    Class<?> toClass = toType instanceof Class ? ((Class<?>) toType)
            : (Class<?>) (((ParameterizedType) toType).getRawType());

    // if we're trying to convert _to_ a collection, handle it as a special case
    if (Collection.class.isAssignableFrom(toClass) || toClass.isArray()) {
        if (!(object instanceof Collection))
            throw new ConversionException("Can only convert a Collection to a Collection/Array. Not "
                    + object.getClass() + " to " + toType, null);

        if (toClass.isArray()) {
            Class<?> targetElementType = toClass.getComponentType();
            Collection input = (Collection) object;
            Object ret = Array.newInstance(targetElementType, input.size());

            int i = 0;
            for (Object element : (Collection) object) {
                Array.set(ret, i, convert(element, targetElementType));
                ++i;
            }
            return ret;
        }

        Collection ret = null;
        if (SortedSet.class.isAssignableFrom(toClass)) {
            ret = new TreeSet();
        } else if (Set.class.isAssignableFrom(toClass)) {
            ret = new HashSet();
        } else if (List.class.isAssignableFrom(toClass)) {
            ret = new ArrayList();
        } else {
            throw new ConversionException("Don't know how to handle collection class: " + toClass, null);
        }

        if (toType instanceof ParameterizedType) {
            // if we have generic type information for the target collection, we can use it to do conversion
            ParameterizedType toParameterizedType = (ParameterizedType) toType;
            Type targetElementType = toParameterizedType.getActualTypeArguments()[0];
            for (Object element : (Collection) object)
                ret.add(convert(element, targetElementType));
        } else {
            // otherwise we must just add all items in a non-type-safe manner
            ret.addAll((Collection) object);
        }
        return ret;
    }

    // otherwise we're converting _to_ a non-collection type

    if (toClass.isAssignableFrom(object.getClass()))
        return object;

    // Numbers with a decimal are always assumed to be Double, so convert to Float, if necessary
    if (toClass.isAssignableFrom(Float.class) && object instanceof Double) {
        return new Float((Double) object);
    }

    if (object instanceof String) {
        String string = (String) object;
        Converter<?> converter = getConverter(toClass);
        if (converter != null)
            return converter.getByUniqueId(string);

        if (toClass.isAssignableFrom(Date.class)) {
            ParseException pex = null;
            String[] supportedFormats = { "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS",
                    "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd" };
            for (int i = 0; i < supportedFormats.length; i++) {
                try {
                    Date date = new SimpleDateFormat(supportedFormats[i]).parse(string);
                    return date;
                } catch (ParseException ex) {
                    pex = ex;
                }
            }
            throw new ConversionException(
                    "Error converting date - correct format (ISO8601 Long): yyyy-MM-dd'T'HH:mm:ss.SSSZ", pex);
        } else if (toClass.isAssignableFrom(Locale.class)) {
            return LocaleUtility.fromSpecification(object.toString());
        } else if (toClass.isEnum()) {
            return Enum.valueOf((Class<? extends Enum>) toClass, object.toString());
        } else if (toClass.isAssignableFrom(Class.class)) {
            try {
                return Context.loadClass((String) object);
            } catch (ClassNotFoundException e) {
                throw new ConversionException("Could not convert from " + object.getClass() + " to " + toType,
                        e);
            }
        }
        // look for a static valueOf(String) method (e.g. Double, Integer, Boolean)
        try {
            Method method = toClass.getMethod("valueOf", String.class);
            if (Modifier.isStatic(method.getModifiers()) && toClass.isAssignableFrom(method.getReturnType())) {
                return method.invoke(null, string);
            }
        } catch (Exception ex) {
        }
    } else if (object instanceof Map) {
        return convertMap((Map<String, ?>) object, toClass);
    }
    if (toClass.isAssignableFrom(Double.class) && object instanceof Number) {
        return ((Number) object).doubleValue();
    } else if (toClass.isAssignableFrom(Integer.class) && object instanceof Number) {
        return ((Number) object).intValue();
    }
    throw new ConversionException("Don't know how to convert from " + object.getClass() + " to " + toType,
            null);
}

From source file:org.kordamp.ezmorph.array.BooleanObjectArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*w  w w  .  j a  va 2 s.  c  o m*/

    if (BOOLEAN_OBJECT_ARRAY_CLASS.isAssignableFrom(array.getClass())) {
        // no conversion needed
        return (Boolean[]) array;
    }

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(Boolean.class, dimensions);
        if (dims == 1) {
            BooleanMorpher morpher = null;
            if (isUseDefault()) {
                if (defaultValue == null) {
                    for (int index = 0; index < length; index++) {
                        Array.set(result, index, null);
                    }
                    return result;
                } else {
                    morpher = new BooleanMorpher(defaultValue.booleanValue());
                }
            } else {
                morpher = new BooleanMorpher();
            }
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)) ? Boolean.TRUE : Boolean.FALSE);
            }
        } else {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morph(Array.get(array, index)));
            }
        }
        return result;
    } else {
        throw new MorphException("argument is not an array: " + array.getClass());
    }
}