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:org.openTwoFactor.clientExt.net.sf.ezmorph.array.LongArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//  w  w w.  ja  v  a  2s  .  co 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:com.nfwork.dbfound.json.JSONDynaBean.java

public Object get(String name, int index) {
    Object value = dynaValues.get(name);
    if (value == null) {
        throw new NullPointerException("Unindexed property name: " + name + " index: " + index);
    } else if (!(value instanceof List) || !(value.getClass().isArray())) {
        throw new IllegalArgumentException("Non-Indexed property name: " + name + " index: " + index);
    }/*from   w  w  w  . ja  v a 2s. c  o m*/
    if (value.getClass().isArray()) {
        value = Array.get(value, index);
    } else if (value instanceof List) {
        value = ((List) value).get(index);
    }

    return value;
}

From source file:org.openTwoFactor.clientExt.net.sf.ezmorph.array.CharArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/* w  w w  .j a  v  a2  s.  co  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.FloatArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from   w  w w  .  j ava 2  s .  c o  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.DoubleArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/*from  w  w w.  ja  v a 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:fredboat.command.fun.RandomImageCommand.java

public void sendRandomFileWithMessage(TextChannel channel, Message message) {
    //Get a random file and send it
    String randomUrl;//w w  w  .j a va  2s.  co m
    synchronized (this) {
        randomUrl = (String) Array.get(urls, new Random().nextInt(urls.length));
    }
    try {
        channel.sendFile(CacheUtil.getImageFromURL(randomUrl), message).queue();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.jkcsoft.web.struts.http.controllers.HttpHelper.java

public Object[] getArrayAttribute(String attributeName, Object[] defaultValue) {
    Object value = request.getAttribute(attributeName);
    Object[] elements;/*from  ww w  . j av  a  2s.  c o m*/

    if (value == null)
        elements = defaultValue;
    else {
        if (value.getClass().isArray()) {
            int length = Array.getLength(value);
            elements = new Object[length];
            for (int i = 0; i < length; i++) {
                elements[i] = Array.get(value, i);
            }
            return elements;
        } else
            elements = defaultValue;
    }

    return elements;
}

From source file:ArrayIterator.java

/**
 * Returns the next element in the array.
 *
 * @return the next element in the array
 * @throws NoSuchElementException if all the elements in the array
 *  have already been returned// w  w w.  j  av a2 s.c  o  m
 */
public Object next() {
    if (hasNext() == false) {
        throw new NoSuchElementException();
    }
    return Array.get(array, index++);
}

From source file:com.mtgi.analytics.aop.BehaviorTrackingAdvice.java

protected static final String toStringArray(Object array) {
    StringBuffer ret = new StringBuffer("[");
    int len = Array.getLength(array);
    int maxLen = Math.min(len, 100);
    for (int i = 0; i < maxLen; ++i) {
        if (i > 0)
            ret.append(", ");
        ret.append(String.valueOf(Array.get(array, i)));
    }/*  ww  w  . j  av  a  2s  . co  m*/
    if (maxLen < len)
        ret.append(", ... (").append(len - maxLen).append(" more)");
    ret.append("]");
    return ret.toString();
}

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

public Object morph(Object array) {
    if (array == null) {
        return null;
    }/* www . j a  v  a  2  s.  c om*/

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(Boolean.class, dimensions);
        if (dims == 1) {
            BooleanMorpher morpher = null;
            if (isUseDefault()) {
                if (defaultValue == null) {
                    for (int index = 0; index < length; index++) {
                        Array.set(result, index, null);
                    }
                    return result;
                } else {
                    morpher = new BooleanMorpher(defaultValue.booleanValue());
                }
            } else {
                morpher = new BooleanMorpher();
            }
            for (int index = 0; index < length; index++) {
                Array.set(result, index, morpher.morph(Array.get(array, index)) ? Boolean.TRUE : Boolean.FALSE);
            }
        } 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());
    }
}