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:therian.operator.add.AddToArray.java

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override/*w  ww .j a  va2 s  . com*/
public boolean perform(TherianContext context, Add<?, ?> add) {
    final Type targetElementType = context.eval(GetElementType.of(add.getTargetPosition()));
    final Position.Writable target = (Position.Writable) add.getTargetPosition();
    final int origSize = context.eval(Size.of(add.getTargetPosition()));
    final Object enlarged = Array.newInstance(TypeUtils.getRawType(targetElementType, null), origSize + 1);
    if (origSize > 0) {
        System.arraycopy(add.getTargetPosition().getValue(), 0, enlarged, 0, origSize);
    }
    Array.set(enlarged, origSize, add.getSourcePosition().getValue());
    target.setValue(enlarged);
    add.setResult(true);
    return true;
}

From source file:org.datalorax.populace.core.populate.mutator.ensure.EnsureArrayElementsNotNullMutator.java

@Override
public Object mutate(Type type, Object currentValue, final Object parent, PopulatorContext config) {
    Validate.isTrue(TypeUtils.isArrayType(type), "Unsupported type %s", type);
    if (currentValue == null) {
        return null;
    }//from   www  . j a v  a 2s.  c  o m

    final int length = Array.getLength(currentValue);
    if (length == 0) {
        return currentValue;
    }

    final Type valueType = getComponentType(type);

    for (int i = 0; i != length; ++i) {
        if (Array.get(currentValue, i) != null) {
            continue;
        }

        final Object instance = config.createInstance(valueType, null);
        Array.set(currentValue, i, instance);
    }
    return currentValue;
}

From source file:com.dinstone.ut.faststub.internal.StubMethodInvocation.java

/**
 * {@inheritDoc}/*from w w  w.j  a v a 2 s.c o m*/
 * 
 */
public Object invoke(Method method, Object[] args) throws Throwable {
    ApplicationContext stubContext = getStubContext(method);
    Object retObj = stubContext.getBean(method.getName());
    // handle exception
    handleException(retObj);

    // handle array object
    Class<?> retType = method.getReturnType();
    if (retType.isArray() && retObj instanceof List<?>) {
        List<?> list = (List<?>) retObj;
        int len = list.size();
        Object arrObj = Array.newInstance(retType.getComponentType(), len);
        for (int i = 0; i < len; i++) {
            Array.set(arrObj, i, list.get(i));
        }
        return arrObj;
    }

    return retObj;
}

From source file:com.browseengine.bobo.serialize.JSONSerializer.java

private static void loadObject(Object array, Class type, int index, JSONArray jsonArray)
        throws JSONSerializationException, JSONException {
    if (type.isPrimitive()) {
        if (type == Integer.TYPE) {
            Array.setInt(array, index, jsonArray.getInt(index));
        } else if (type == Long.TYPE) {
            Array.setLong(array, index, jsonArray.getInt(index));
        } else if (type == Short.TYPE) {
            Array.setShort(array, index, (short) jsonArray.getInt(index));
        } else if (type == Boolean.TYPE) {
            Array.setBoolean(array, index, jsonArray.getBoolean(index));
        } else if (type == Double.TYPE) {
            Array.setDouble(array, index, jsonArray.getDouble(index));
        } else if (type == Float.TYPE) {
            Array.setFloat(array, index, (float) jsonArray.getDouble(index));
        } else if (type == Character.TYPE) {
            char ch = jsonArray.getString(index).charAt(0);
            Array.setChar(array, index, ch);
        } else if (type == Byte.TYPE) {
            Array.setByte(array, index, (byte) jsonArray.getInt(index));
        } else {//ww w .j a  va 2 s  . co m
            throw new JSONSerializationException("Unknown primitive: " + type);
        }
    } else if (type == String.class) {
        Array.set(array, index, jsonArray.getString(index));
    } else if (JSONSerializable.class.isAssignableFrom(type)) {
        JSONObject jObj = jsonArray.getJSONObject(index);
        JSONSerializable serObj = deSerialize(type, jObj);
        Array.set(array, index, serObj);
    } else if (type.isArray()) {
        Class componentClass = type.getComponentType();
        JSONArray subArray = jsonArray.getJSONArray(index);
        int len = subArray.length();

        Object newArray = Array.newInstance(componentClass, len);
        for (int k = 0; k < len; ++k) {
            loadObject(newArray, componentClass, k, subArray);
        }
    }
}

From source file:therian.operator.convert.CollectionToArray.java

@SuppressWarnings("unchecked")
@Override// www  .  j  av  a2 s.  c  o  m
public boolean perform(TherianContext context, Convert<? extends Collection, ?> convert) {
    final Type targetElementType = context.eval(GetElementType.of(convert.getTargetPosition()));
    final int size = convert.getSourcePosition().getValue().size();

    final Object result = Array.newInstance(TypeUtils.getRawType(targetElementType, null), size);

    final Object[] toFill;
    final boolean primitiveTargetElementType = targetElementType instanceof Class<?>
            && ((Class<?>) targetElementType).isPrimitive();

    if (primitiveTargetElementType) {
        toFill = (Object[]) Array.newInstance(ClassUtils.primitiveToWrapper((Class<?>) targetElementType),
                size);
    } else {
        toFill = (Object[]) result;
    }
    convert.getSourcePosition().getValue().toArray(toFill);
    if (primitiveTargetElementType) {
        for (int i = 0; i < size; i++) {
            Array.set(result, i, toFill[i]);
        }
    }
    ((Position.Writable<Object>) convert.getTargetPosition()).setValue(result);
    return true;
}

From source file:com.scit.sling.test.MockValueMap.java

@SuppressWarnings("unchecked")
private <T> T convertType(Object o, Class<T> type) {
    if (o == null) {
        return null;
    }/*  w w w  .  ja v a  2 s  . co m*/
    if (o.getClass().isArray() && type.isArray()) {
        if (type.getComponentType().isAssignableFrom(o.getClass().getComponentType())) {
            return (T) o;
        } else {
            // we need to convert the elements in the array
            Object array = Array.newInstance(type.getComponentType(), Array.getLength(o));
            for (int i = 0; i < Array.getLength(o); i++) {
                Array.set(array, i, convertType(Array.get(o, i), type.getComponentType()));
            }
            return (T) array;
        }
    }
    if (o.getClass().isAssignableFrom(type)) {
        return (T) o;
    }
    if (String.class.isAssignableFrom(type)) {
        // Format dates
        if (o instanceof Calendar) {
            return (T) formatDate((Calendar) o);
        } else if (o instanceof Date) {
            return (T) formatDate((Date) o);
        } else if (o instanceof DateTime) {
            return (T) formatDate((DateTime) o);
        }
        return (T) String.valueOf(o);
    } else if (o instanceof DateTime) {
        DateTime dt = (DateTime) o;
        if (Calendar.class.isAssignableFrom(type)) {
            return (T) dt.toCalendar(Locale.getDefault());
        } else if (Date.class.isAssignableFrom(type)) {
            return (T) dt.toDate();
        } else if (Number.class.isAssignableFrom(type)) {
            return convertType(dt.getMillis(), type);
        }
    } else if (o instanceof Number && Number.class.isAssignableFrom(type)) {
        if (Byte.class.isAssignableFrom(type)) {
            return (T) (Byte) ((Number) o).byteValue();
        } else if (Double.class.isAssignableFrom(type)) {
            return (T) (Double) ((Number) o).doubleValue();
        } else if (Float.class.isAssignableFrom(type)) {
            return (T) (Float) ((Number) o).floatValue();
        } else if (Integer.class.isAssignableFrom(type)) {
            return (T) (Integer) ((Number) o).intValue();
        } else if (Long.class.isAssignableFrom(type)) {
            return (T) (Long) ((Number) o).longValue();
        } else if (Short.class.isAssignableFrom(type)) {
            return (T) (Short) ((Number) o).shortValue();
        } else if (BigDecimal.class.isAssignableFrom(type)) {
            return (T) new BigDecimal(o.toString());
        }
    } else if (o instanceof Number && type.isPrimitive()) {
        final Number num = (Number) o;
        if (type == byte.class) {
            return (T) new Byte(num.byteValue());
        } else if (type == double.class) {
            return (T) new Double(num.doubleValue());
        } else if (type == float.class) {
            return (T) new Float(num.floatValue());
        } else if (type == int.class) {
            return (T) new Integer(num.intValue());
        } else if (type == long.class) {
            return (T) new Long(num.longValue());
        } else if (type == short.class) {
            return (T) new Short(num.shortValue());
        }
    } else if (o instanceof String && Number.class.isAssignableFrom(type)) {
        if (Byte.class.isAssignableFrom(type)) {
            return (T) new Byte((String) o);
        } else if (Double.class.isAssignableFrom(type)) {
            return (T) new Double((String) o);
        } else if (Float.class.isAssignableFrom(type)) {
            return (T) new Float((String) o);
        } else if (Integer.class.isAssignableFrom(type)) {
            return (T) new Integer((String) o);
        } else if (Long.class.isAssignableFrom(type)) {
            return (T) new Long((String) o);
        } else if (Short.class.isAssignableFrom(type)) {
            return (T) new Short((String) o);
        } else if (BigDecimal.class.isAssignableFrom(type)) {
            return (T) new BigDecimal((String) o);
        }
    }
    throw new NotImplementedException(
            "Can't handle conversion from " + o.getClass().getName() + " to " + type.getName());
}

From source file:com.carmanconsulting.cassidy.pojo.assembly.step.ArrayStep.java

@Override
public void execute(AssemblyStack assemblyStack) {
    final List<Object> elements = assemblyStack.drain();
    Object array = Array.newInstance(elementType, elements.size());
    for (int i = 0; i < elements.size(); i++) {
        Object element = elements.get(i);
        Array.set(array, i, element);
    }/*from   w w  w  .j  a  va 2 s . co m*/
    assemblyStack.push(array);
}

From source file:com.l2jserver.util.transformer.impl.ArrayTransformer.java

@Override
@SuppressWarnings("unchecked")
public T[] untransform(Class<? extends T[]> type, String stringValue) {
    final Transformer<T> transformer = (Transformer<T>) TransformerFactory
            .getTransfromer(type.getComponentType());
    final String[] stringValues = StringUtils.split(stringValue, '|');
    final Object values = Array.newInstance(type.getComponentType(), stringValues.length);
    int i = 0;//from  ww  w  .ja  va  2  s  . com
    for (final String value : stringValues) {
        Array.set(values, i++, transformer.untransform((Class<T>) type.getComponentType(), value));
    }

    return type.cast(values);
}

From source file:net.radai.beanz.codecs.ArrayCodec.java

@Override
public Object decode(String encoded) {
    if (encoded == null || (encoded = encoded.trim()).isEmpty()) {
        return null;
    }/*from   w ww. j  av a2s .  c om*/
    if (!(encoded.startsWith("[") && encoded.endsWith("]"))) {
        throw new IllegalArgumentException();
    }
    String[] elements = encoded.substring(1, encoded.length() - 1).split("\\s*,\\s*");
    Class<?> erased = erase(getElementType());
    Object array = Array.newInstance(erased, elements.length);
    for (int i = 0; i < elements.length; i++) {
        Array.set(array, i, elementCodec.decode(elements[i]));
    }
    return array;
}

From source file:edu.sdsc.scigraph.neo4j.GraphUtil.java

static Object getNewPropertyValue(Object originalValue, Object newValue) {
    Class<?> clazz = checkNotNull(newValue).getClass();
    boolean reduceToString = false;
    if (null != originalValue && originalValue.getClass().isArray()) {
        Class<?> originalClazz = Array.get(originalValue, 0).getClass();
        if (!originalClazz.equals(clazz)) {
            reduceToString = true;/*w w w .  j  a v  a 2 s.co m*/
            clazz = String.class;
        }
        newValue = reduceToString ? newValue.toString() : newValue;
        Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1);
        for (int i = 0; i < Array.getLength(originalValue); i++) {
            Object val = Array.get(originalValue, i);
            if (newValue.equals(val)) {
                return originalValue;
            }
            Array.set(newArray, i, reduceToString ? val.toString() : val);
        }
        Array.set(newArray, Array.getLength(originalValue), newValue);
        return newArray;
    } else if (null != originalValue) {
        if (!clazz.equals(originalValue.getClass())) {
            reduceToString = true;
            clazz = String.class;
        }
        originalValue = reduceToString ? originalValue.toString() : originalValue;
        newValue = reduceToString ? newValue.toString() : newValue;
        if (!originalValue.equals(newValue)) {
            Object newArray = Array.newInstance(clazz, 2);
            Array.set(newArray, 0, originalValue);
            Array.set(newArray, 1, newValue);
            return newArray;
        } else {
            return originalValue;
        }
    } else {
        return newValue;
    }
}