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:com.espertech.esper.core.ResultDeliveryStrategyTypeArr.java

private Object convert(EventBean[] events) {
    if ((events == null) || (events.length == 0)) {
        return null;
    }//  w w  w .  j  ava2  s  .  c  o m

    Object array = Array.newInstance(componentType, events.length);
    int length = 0;
    for (int i = 0; i < events.length; i++) {
        if (events[i] instanceof NaturalEventBean) {
            NaturalEventBean natural = (NaturalEventBean) events[i];
            Array.set(array, length, natural.getNatural()[0]);
            length++;
        }
    }

    if (length == 0) {
        return null;
    }
    if (length != events.length) {
        Object reduced = Array.newInstance(componentType, events.length);
        System.arraycopy(array, 0, reduced, 0, length);
        array = reduced;
    }
    return array;
}

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

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

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(Character.class, dimensions);
        if (dims == 1) {
            CharMorpher morpher = null;
            if (isUseDefault()) {
                if (defaultValue == null) {
                    for (int index = 0; index < length; index++) {
                        Array.set(result, index, null);
                    }
                    return result;
                } else {
                    morpher = new CharMorpher(defaultValue.charValue());
                }
            } else {
                morpher = new CharMorpher();
            }
            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.openTwoFactor.clientExt.net.sf.ezmorph.array.ByteArrayMorpher.java

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

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(byte.class, dimensions);
        ByteMorpher morpher = isUseDefault() ? new ByteMorpher(defaultValue) : new ByteMorpher();
        if (dims == 1) {
            for (int index = 0; index < length; index++) {
                Array.set(result, index, new Byte(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.openTwoFactor.clientExt.net.sf.ezmorph.array.IntArrayMorpher.java

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

    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, new Integer(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.openTwoFactor.clientExt.net.sf.ezmorph.array.CharArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from  ww  w . j a va 2s  . 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, new Character(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.openTwoFactor.clientExt.net.sf.ezmorph.array.LongArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*  w  w w.  ja  v a2 s . c  o  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, new Long(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.openTwoFactor.clientExt.net.sf.ezmorph.array.FloatArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*from   ww  w . ja va2s  . 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, new Float(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.openTwoFactor.clientExt.net.sf.ezmorph.array.ShortArrayMorpher.java

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

    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, new Short(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.openTwoFactor.clientExt.net.sf.ezmorph.array.DoubleArrayMorpher.java

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

    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, new Double(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.astamuse.asta4d.data.DefaultContextDataFinder.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override//ww  w  . j  av  a2s.  c o  m
public ContextDataHolder findDataInContext(Context context, String scope, String name, Class<?> targetType)
        throws DataOperationException {
    ContextDataHolder dataHolder = findByType(context, scope, name, targetType);

    if (dataHolder != null) {
        return dataHolder;
    }

    if (StringUtils.isEmpty(scope)) {
        dataHolder = findDataByScopeOrder(context, 0, name);
    } else {
        dataHolder = context.getDataHolder(scope, name);
    }

    if (dataHolder == null) {
        return null;
    }

    Object foundData = dataHolder.getValue();
    Object transformedData = null;
    UnsupportedValueException usve = null;

    Class<?> srcType = new TypeInfo(foundData.getClass()).getType();
    if (targetType.isAssignableFrom(srcType)) {
        transformedData = foundData;
    } else if (srcType.isArray() && targetType.isAssignableFrom(srcType.getComponentType())) {
        transformedData = Array.get(foundData, 0);
    } else if (targetType.isArray() && targetType.getComponentType().isAssignableFrom(srcType)) {
        Object array = Array.newInstance(srcType, 1);
        Array.set(array, 0, foundData);
        transformedData = array;
    } else {
        try {
            transformedData = Configuration.getConfiguration().getDataTypeTransformer().transform(srcType,
                    targetType, foundData);
        } catch (UnsupportedValueException ex) {
            usve = ex;
        }
    }
    if (usve == null) {
        dataHolder.setData(dataHolder.getName(), dataHolder.getScope(), foundData, transformedData);
    } else {
        dataHolder.setData(dataHolder.getName(), InjectUtil.ContextDataTypeUnMatchScope, foundData,
                transformedData);
    }
    return dataHolder;
}