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

public Object nextElement() {
    return Array.get(array, cursor++);
}

From source file:Main.java

private static boolean compareScalarValues(Object myValue, Object otherValue) {
    if (myValue == null && otherValue != null || myValue != null && otherValue == null)
        return false;

    if (myValue == null)
        return true;

    if (myValue.getClass().isArray() && !otherValue.getClass().isArray()
            || !myValue.getClass().isArray() && otherValue.getClass().isArray())
        return false;

    if (myValue.getClass().isArray() && otherValue.getClass().isArray()) {
        final int myArraySize = Array.getLength(myValue);
        final int otherArraySize = Array.getLength(otherValue);

        if (myArraySize != otherArraySize)
            return false;

        for (int i = 0; i < myArraySize; i++)
            if (!Array.get(myValue, i).equals(Array.get(otherValue, i)))
                return false;

        return true;
    }// w  w w .j a v a  2  s.c o m

    if (myValue instanceof Number && otherValue instanceof Number) {
        final Number myNumberValue = (Number) myValue;
        final Number otherNumberValue = (Number) otherValue;

        if (isInteger(myNumberValue) && isInteger(otherNumberValue))
            return myNumberValue.longValue() == otherNumberValue.longValue();
        else if (isFloat(myNumberValue) && isFloat(otherNumberValue))
            return myNumberValue.doubleValue() == otherNumberValue.doubleValue();
    }

    return myValue.equals(otherValue);
}

From source file:Main.java

/**
 * @param propertyValue node.getProperty value.
 * @return a collection of all the values from a property. If the value is
 *         just a plain "single" value the collection will contain that
 *         single value. If the value is an array of values, all those
 *         values are added to the collection.
 *///from  w w w.  j  a  v  a 2 s  .  c o m
public static Collection<Object> propertyValueToCollection(Object propertyValue) {
    Set<Object> values = new HashSet<Object>();
    try {
        int length = Array.getLength(propertyValue);
        for (int i = 0; i < length; i++) {
            values.add(Array.get(propertyValue, i));
        }
    } catch (IllegalArgumentException e) {
        values.add(propertyValue);
    }
    return values;
}

From source file:Main.java

public static void cleanThreadLocals(Thread thread)
        throws NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException {
    if (thread == null)
        return;/*  w  ww  .  j  a va  2s.c  o m*/
    Field threadLocalsField = Thread.class.getDeclaredField("threadLocals");
    threadLocalsField.setAccessible(true);

    Class<?> threadLocalMapKlazz = Class.forName("java.lang.ThreadLocal$ThreadLocalMap");
    Field tableField = threadLocalMapKlazz.getDeclaredField("table");
    tableField.setAccessible(true);

    Object fieldLocal = threadLocalsField.get(thread);
    if (fieldLocal == null) {
        return;
    }
    Object table = tableField.get(fieldLocal);

    int threadLocalCount = Array.getLength(table);

    for (int i = 0; i < threadLocalCount; i++) {
        Object entry = Array.get(table, i);
        if (entry != null) {
            Field valueField = entry.getClass().getDeclaredField("value");
            valueField.setAccessible(true);
            Object value = valueField.get(entry);
            if (value != null) {

                if ("java.util.concurrent.ConcurrentLinkedQueue".equals(value.getClass().getName())
                        || "java.util.concurrent.ConcurrentHashMap".equals(value.getClass().getName())) {
                    valueField.set(entry, null);
                }
            }

        }
    }
}

From source file:Main.java

public static void outputArrays(final Object first, final Object second) {
    if (!first.getClass().isArray()) {
        throw new IllegalArgumentException("first is not an array.");
    }/*from  ww  w.  j  av  a 2 s . c  o m*/
    if (!second.getClass().isArray()) {
        throw new IllegalArgumentException("second is not an array.");
    }

    final int lengthFirst = Array.getLength(first);
    final int lengthSecond = Array.getLength(second);
    final int length = Math.max(lengthFirst, lengthSecond);

    for (int idx = 0; idx < length; idx++) {
        System.out.print("[" + idx + "]\t");
        if (idx < lengthFirst) {
            System.out.print(Array.get(first, idx) + "\t\t");
        } else {
            System.out.print("\t\t");
        }
        if (idx < lengthSecond) {
            System.out.print(Array.get(second, idx) + "\t");
        }
        System.out.println();
    }
}

From source file:Main.java

/**
 * Method to merge two arrays./*from  w  w  w .  j  a va  2 s. c  o  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

private static Object arrayRemoveInternal(Object array, Object elementToRemove) {
    boolean found = false;
    final int oldLength = Array.getLength(array);
    if (oldLength == 0) {
        return array;
    }//from   w w w.  jav  a2  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

/**
 * Converts an Object reference that is known to be an array into an
 * Object[]. If the array is assignable to Object[], the array passed in is
 * simply cast and returned. Otherwise a new Object[] of equal size is
 * constructed and the elements are wrapped and inserted into the new array
 * before being returned./*from www.  j  ava  2s .  c o m*/
 * 
 * @param in
 *            an array of Objects or primitives
 * @return an Object[], either the array passed in, or in the case of
 *         primitives, a new Object[] containing a wrapper for each element
 *         in the input array
 * @throws IllegalArgumentException
 *             thrown if the in parameter is null or not an array
 */
public static Object[] asObjectArray(Object in) {
    if (in == null || !in.getClass().isArray()) {
        throw new IllegalArgumentException("Parameter to asObjectArray must be a non-null array.");
    } else if (in instanceof Object[]) {
        return (Object[]) in;
    } else {
        int length = Array.getLength(in);
        Object[] out = new Object[length];
        for (int i = 0; i < length; ++i) {
            out[i] = Array.get(in, i);
        }

        return out;
    }
}

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  . c o  m
    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:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java

public static byte[] convertToByteArray(final Object array) {
    if (array == null) {
        return null;
    }/*w  ww.  java2 s  .c  o  m*/
    if (array instanceof byte[]) {
        return (byte[]) array;
    }
    if (array instanceof Byte[]) {
        return ArrayUtils.toPrimitive((Byte[]) array);
    }
    final byte[] newArray = new byte[Array.getLength(array)];
    for (int i = 0; i < newArray.length; i++) {
        final Number val = (Number) Array.get(array, i);
        newArray[i] = val.byteValue();
    }
    return newArray;
}