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.processing.core.PApplet.java

static public Object reverse(Object list) {
    Class<?> type = list.getClass().getComponentType();
    int length = Array.getLength(list);
    Object outgoing = Array.newInstance(type, length);
    for (int i = 0; i < length; i++) {
        Array.set(outgoing, i, Array.get(list, (length - 1) - i));
    }//w ww . ja  va  2  s.com
    return outgoing;
}

From source file:com.clark.func.Functions.java

/**
 * Clone an object.//from w  ww. j a  v a 2 s . c o  m
 * 
 * @param <T>
 *            the type of the object
 * @param o
 *            the object to clone
 * @return the clone if the object implements {@link Cloneable} otherwise
 *         <code>null</code>
 * @throws CloneFailedException
 *             if the object is cloneable and the clone operation fails
 * @since 3.0
 */
public static <T> T objectClone(final T o) {
    if (o instanceof Cloneable) {
        final Object result;
        if (o.getClass().isArray()) {
            final Class<?> componentType = o.getClass().getComponentType();
            if (!componentType.isPrimitive()) {
                result = ((Object[]) o).clone();
            } else {
                int length = Array.getLength(o);
                result = Array.newInstance(componentType, length);
                while (length-- > 0) {
                    Array.set(result, length, Array.get(o, length));
                }
            }
        } else {
            try {
                final Method clone = o.getClass().getMethod("clone");
                result = clone.invoke(o);
            } catch (final NoSuchMethodException e) {
                throw new CloneFailedException(
                        "Cloneable type " + o.getClass().getName() + " has no clone method", e);
            } catch (final IllegalAccessException e) {
                throw new CloneFailedException("Cannot clone Cloneable type " + o.getClass().getName(), e);
            } catch (final InvocationTargetException e) {
                throw new CloneFailedException("Exception cloning Cloneable type " + o.getClass().getName(),
                        e.getCause());
            }
        }
        @SuppressWarnings("unchecked")
        final T checked = (T) result;
        return checked;
    }

    return null;
}