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:SampleGetArrayReflection.java

public static void copyArray(Object source, Object dest) {
    for (int i = 0; i < Array.getLength(source); i++) {
        Array.set(dest, i, Array.get(source, i));
        System.out.println(Array.get(dest, i));
    }//  w  w  w  . j a va  2s . co  m
}

From source file:Main.java

public static Object arrayExpandAddElements(Object array, Object[] elementsToAdd) {
    Class cl = array.getClass();/*from   w w  w  . j  av a2 s  .com*/
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + elementsToAdd.length;
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    for (int i = 0; i < elementsToAdd.length; i++) {
        Array.set(newArray, length + i, elementsToAdd[i]);
    }
    return newArray;
}

From source file:Main.java

private static Object collectionToArray(Iterator it, Class componentTypeClass, int size) {
    int index = 0;
    Object array = Array.newInstance(componentTypeClass, size);
    while (it.hasNext()) {
        Object item = it.next();/*from w w w .  j  a va2  s. co  m*/
        Array.set(array, index, item);
        index++;
    }
    return array;
}

From source file:io.fabric8.jolokia.support.JolokiaHelpers.java

public static Object convertJolokiaToJavaType(Class<?> clazz, Object value) throws IOException {
    if (clazz.isArray()) {
        if (value instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) value;
            Object[] javaArray = (Object[]) Array.newInstance(clazz.getComponentType(), jsonArray.size());
            int idx = 0;
            for (Object element : jsonArray) {
                Array.set(javaArray, idx++, convertJolokiaToJavaType(clazz.getComponentType(), element));
            }// w  w w .  j  av a2 s  . c  o m
            return javaArray;
        } else {
            return null;
        }
    } else if (String.class.equals(clazz)) {
        return (value != null) ? value.toString() : null;
    } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) {
        Number number = asNumber(value);
        return number != null ? number.byteValue() : null;
    } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
        Number number = asNumber(value);
        return number != null ? number.shortValue() : null;
    } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
        Number number = asNumber(value);
        return number != null ? number.intValue() : null;
    } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
        Number number = asNumber(value);
        return number != null ? number.longValue() : null;
    } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
        Number number = asNumber(value);
        return number != null ? number.floatValue() : null;
    } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
        Number number = asNumber(value);
        return number != null ? number.doubleValue() : null;
    } else if (value instanceof JSONObject) {
        JSONObject jsonObject = (JSONObject) value;
        if (!JSONObject.class.isAssignableFrom(clazz)) {
            String json = jsonObject.toJSONString();
            return getObjectMapper().readerFor(clazz).readValue(json);
        }
    }
    return value;
}

From source file:Main.java

/**
 * Method to merge two arrays./*from ww w  . j ava  2  s. co m*/
 * 
 * @param firstObject
 *            The first array to be merged
 * 
 * @param secondObject
 *            The second array to be merged
 * 
 * @return an object containing the elements of the merged arrays
 */
private static Object joinArrays(Object firstObject, Object secondObject) {
    Class<?> o1Type = firstObject.getClass().getComponentType();
    Class<?> o2Type = secondObject.getClass().getComponentType();

    if (o1Type != o2Type)
        throw new IllegalArgumentException();

    int firstObjectSize = Array.getLength(firstObject);
    int secondObjectSize = Array.getLength(secondObject);
    Object array = Array.newInstance(o1Type, firstObjectSize + secondObjectSize);

    int offset = 0, i;
    for (i = 0; i < firstObjectSize; i++, offset++)
        Array.set(array, offset, Array.get(firstObject, i));
    for (i = 0; i < secondObjectSize; i++, offset++)
        Array.set(array, offset, Array.get(secondObject, i));

    return array;
}

From source file:Main.java

/**
 *  Copies elements of source to dest. If adjust is -1 the element at
 *  colindex is not copied. If adjust is +1 that element is filled with
 *  the Object addition. All the rest of the elements in source are
 *  shifted left or right accordingly when they are copied. If adjust is 0
 *  the addition is copied over the element at colindex.
 *
 *  No checks are perfomed on array sizes and an exception is thrown
 *  if they are not consistent with the other arguments.
 *//*w  ww.  j  a  v a2s . c  o m*/
public static void copyAdjustArray(Object source, Object dest, Object addition, int colindex, int adjust) {

    int length = Array.getLength(source);

    if (colindex < 0) {
        System.arraycopy(source, 0, dest, 0, length);

        return;
    }

    System.arraycopy(source, 0, dest, 0, colindex);

    if (adjust == 0) {
        int endcount = length - colindex - 1;

        Array.set(dest, colindex, addition);

        if (endcount > 0) {
            System.arraycopy(source, colindex + 1, dest, colindex + 1, endcount);
        }
    } else if (adjust < 0) {
        int endcount = length - colindex - 1;

        if (endcount > 0) {
            System.arraycopy(source, colindex + 1, dest, colindex, endcount);
        }
    } else {
        int endcount = length - colindex;

        Array.set(dest, colindex, addition);

        if (endcount > 0) {
            System.arraycopy(source, colindex, dest, colindex + 1, endcount);
        }
    }
}

From source file:Main.java

/**
 * //  w w w  .  j av a  2  s  . c om
 * 
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(Collection<T> collection, Class<T> elementClass, int index, int count) {
    int n = collection.size();
    int end = Math.min(index + count, n);
    Object result = Array.newInstance(elementClass, end - index);
    Iterator<T> it = collection.iterator();
    int resultIndex = 0;
    for (int i = 0; i < end; i++) {
        if (!it.hasNext())
            break;
        Object value = it.next();
        if (i < index)
            continue;
        Array.set(result, resultIndex, value);
        resultIndex++;
    }
    return (T[]) result;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static Object joinArrays(Object paramObject1, Object paramObject2) {
    Class localClass = paramObject1.getClass().getComponentType();
    int i = Array.getLength(paramObject1);
    int j = i + Array.getLength(paramObject2);
    Object localObject = Array.newInstance(localClass, j);
    int k = 0;/*from www  .  j  a v  a 2  s  .com*/
    while (k < i) {
        Array.set(localObject, k, Array.get(paramObject1, k));
        k++;
    }
    while (k < j) {
        Array.set(localObject, k, Array.get(paramObject2, k - i));
        k++;
    }
    return localObject;
}

From source file:Main.java

private static Object arrayRemoveInternal(Object array, Object elementToRemove) {
    boolean found = false;
    final int oldLength = Array.getLength(array);
    if (oldLength == 0) {
        return array;
    }/*from  w  ww.  j a va2 s . c o  m*/
    final int newLength = oldLength - 1;
    final Object result = Array.newInstance(array.getClass().getComponentType(), newLength);
    int nextIndex = 0;
    for (int i = 0; i < oldLength; i++) {
        final Object e = Array.get(array, i);
        if (e.equals(elementToRemove)) {
            found = true;
        } else {
            if (nextIndex == newLength) {
                break;
            }
            Array.set(result, nextIndex, e);
            nextIndex++;
        }
    }
    if (!found) {
        return array;
    }
    return result;
}

From source file:Main.java

public static Object arrayExpandAddElements(Object array, Collection elementsToAdd) {
    Class cl = array.getClass();/*www .j  av  a2 s  .c om*/
    if (!cl.isArray())
        return null;
    int length = Array.getLength(array);
    int newLength = length + elementsToAdd.size();
    Class componentType = array.getClass().getComponentType();
    Object newArray = Array.newInstance(componentType, newLength);
    System.arraycopy(array, 0, newArray, 0, length);
    int count = 0;
    for (Object element : elementsToAdd) {
        Array.set(newArray, length + count, element);
        count++;
    }
    return newArray;
}