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:com.google.corp.productivity.specialprojects.android.comm.SerializableCookieStore.java

private static void putJs(JSONArray array, int i, String key, Object value) {
    if (value != null) {
        JSONObject object = array.optJSONObject(i);
        try {//from   ww  w . j  a  va2 s .  c  om
            if (object == null) {
                object = new JSONObject();
                array.put(i, object);
            }
            if (value.getClass().isArray()) {
                // android's version of org.json doesn't instantiate array
                // automatically.
                JSONArray newArray = new JSONArray();
                int n = Array.getLength(value);
                for (int j = 0; j < n; j++) {
                    newArray.put(Array.get(value, j));
                }
                value = newArray;
            }
            object.put(key, value);
        } catch (JSONException e) {
            // Ignore.
        }
    }
}

From source file:com.haulmont.cuba.web.app.ui.jmxcontrol.util.AttributeEditor.java

protected List objectToStringArray(Object value) {
    if (value instanceof Collection) {
        return new ArrayList((Collection) value);
    }//from  w w w  . j  a v a 2s  . c  o  m
    int length = Array.getLength(value);
    List<Object> output = new ArrayList<>(length);
    for (int i = 0; i < length; i++) {
        output.add(Array.get(value, i));
    }
    return output;
}

From source file:org.omnaest.utils.structure.array.ArrayUtils.java

/**
 * Returns a non primitive {@link Array} for a primitive one
 * /*w ww . j a  va2s . c  o m*/
 * @param primitiveArray
 * @return
 */
public static Object[] toObject(Object primitiveArray) {
    //    
    Object[] retvals = null;

    //
    if (primitiveArray != null) {
        try {
            //
            Class<? extends Object> primitiveArrayType = primitiveArray.getClass();
            Class<?> componentType = primitiveArrayType.getComponentType();
            if (componentType.isPrimitive()) {
                componentType = ObjectUtils.primitiveWrapperTypeFor(componentType);
            }
            int length = Array.getLength(primitiveArray);
            retvals = (Object[]) Array.newInstance(componentType, length);

            //
            for (int ii = 0; ii < retvals.length; ii++) {
                retvals[ii] = Array.get(primitiveArray, ii);
            }
        } catch (Exception e) {
        }
    }

    //
    return retvals;
}

From source file:org.mypsycho.beans.DefaultInvoker.java

public Object getIndexed(Object bean, int index) {
    if (bean.getClass().isArray()) {
        return (Array.get(bean, index));
    } else if (bean instanceof java.util.List) {
        // get the List's value
        return ((List<?>) bean).get(index);
    } else {/*  ww  w .  j a  v a  2 s .c  o m*/
        throw new IllegalArgumentException("Class '" + bean.getClass() + "' is not indexed");
    }
}

From source file:com.manydesigns.elements.forms.TableForm.java

public void readFromObject(Object obj) {
    Class clazz = obj.getClass();
    if (clazz.isArray()) { // Tratta obj come un array
        // Scorre tutti gli ellementi dell'array obj,
        // indipendentemente da quante righe ci sono nell table form.
        // Eventualmente lancia Eccezione.
        final int arrayLength = Array.getLength(obj);
        for (int i = 0; i < arrayLength; i++) {
            Object currentObj = Array.get(obj, i);
            rows[i].readFromObject(currentObj);
        }//from   www.  j  a v a 2  s.co m

        // Scorre le rimanenti righe del table form,
        // passano null come ottetto di bind.
        for (int i = arrayLength; i < rows.length; i++) {
            rows[i].readFromObject(null);
        }
    } else if (Collection.class.isAssignableFrom(clazz)) {
        // Tratta obj come collection
        Collection collection = (Collection) obj;

        int i = 0;
        for (Object currentObj : collection) {
            rows[i].readFromObject(currentObj);
            i++;
        }

        for (; i < rows.length; i++) {
            rows[i].readFromObject(null);
        }
    }
}

From source file:org.nabucco.alfresco.enhScriptEnv.common.script.converter.general.ArrayConverter.java

/**
 * {@inheritDoc}//  w  w w.ja v  a2 s .com
 */
@Override
public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate,
        final Class<?> expectedClass) {
    boolean canConvert = expectedClass.isArray();

    if (canConvert) {
        final Class<?> componentClass = expectedClass.getComponentType();
        if (value instanceof Iterable<?>) {
            final Collection<?> coll = (Collection<?>) value;
            for (final Object element : coll) {
                canConvert = canConvert && globalDelegate.canConvertValueForJava(element, componentClass);

                if (!canConvert) {
                    break;
                }
            }
        } else if (value.getClass().isArray()) {
            final int length = Array.getLength(value);
            for (int idx = 0; idx < length && canConvert; idx++) {
                canConvert = canConvert
                        && globalDelegate.canConvertValueForJava(Array.get(value, idx), componentClass);
            }
        } else {
            canConvert = false;
        }
    }

    return canConvert;
}

From source file:org.deephacks.confit.serialization.ValueSerializationTest.java

public static <T> T[] convert(final Object array, Class<T> wrapperClass) {
    final int arrayLength = Array.getLength(array);
    final T[] result = (T[]) Array.newInstance(wrapperClass, arrayLength);
    for (int i = 0; i < arrayLength; i++) {
        Array.set(result, i, Array.get(array, i));
    }//from   w  ww . j a v  a2s  .  c  om
    return result;
}

From source file:de.cosmocode.lucene.DefaultLuceneQuery.java

@Override
protected DefaultLuceneQuery addArgumentAsArray(Object values, final QueryModifier modifier) {

    if (values == null || !values.getClass().isArray() || Array.getLength(values) == 0) {
        setLastSuccessful(false);/*from www  .  j ava  2 s. com*/
        return this;
    }

    final int arrayLength = Array.getLength(values);

    beforeIteration(modifier);

    // add all items
    final QueryModifier valueModifier = modifier.getMultiValueModifier();
    for (int i = 0; i < arrayLength; i++) {
        addArgument(Array.get(values, i), valueModifier);
    }

    afterIteration();

    return this;
}

From source file:com.dnw.json.J.java

/**
 * Tries to convert the given value to a JSON compatible value. A global internal cache will be
 * used to improve the performance.// ww w .j  a v  a  2  s  .  co  m
 * 
 * @author manbaum
 * @since Oct 11, 2014
 * @param value the value to convert.
 * @return the result value.
 */
public final static Object convert(Object value) {
    if (value == null)
        return value;
    else if (value instanceof CharSequence)
        return ((CharSequence) value).toString();
    else if (value instanceof Character)
        return value.toString();
    else if (value instanceof Number)
        return value;
    else if (value instanceof Boolean)
        return value;
    else if (value instanceof M)
        return ((M) value).map;
    else if (value instanceof Map) {
        Map<?, ?> src = (Map<?, ?>) value;
        Map<String, Object> map = new HashMap<String, Object>();
        for (Map.Entry<?, ?> e : src.entrySet()) {
            String key = String.valueOf(e.getKey());
            map.put(key, convert(e.getValue()));
        }
        return map;
    } else if (value instanceof L)
        return ((L) value).list;
    else if (value instanceof Iterable) {
        Iterable<?> src = (List<?>) value;
        List<Object> list = new ArrayList<Object>();
        for (Object v : src) {
            list.add(convert(v));
        }
        return list;
    } else if (value.getClass().isArray()) {
        List<Object> list = new ArrayList<Object>();
        int length = Array.getLength(value);
        for (int i = 0; i < length; i++) {
            list.add(convert(Array.get(value, i)));
        }
        return list;
    } else
        return tryConverter(value);
}

From source file:br.msf.commons.util.ArrayUtils.java

public static String toString(final Object array, final String descriptionPath, final String separator,
        final String lastSeparator) {
    if (separator == null || lastSeparator == null) {
        throw new IllegalArgumentException("Argumento nulo.");
    }//from   w  w w .j av  a 2s.c  om
    if (isEmptyOrNull(array)) {
        return "";
    }
    final EnhancedStringBuilder builder = new EnhancedStringBuilder();
    final int size = getSize(array);
    for (int i = 0; i < size; i++) {
        Object toAdd = Array.get(array, i);
        if (CharSequenceUtils.isNotEmpty(descriptionPath)) {
            toAdd = ReflectionUtils.invokeCascadeGetterFor(toAdd, descriptionPath);
        }
        builder.append(toAdd).appendIfTrue((i < (size - 2)), separator).appendIfTrue((i == (size - 2)),
                lastSeparator);
    }
    return builder.toString();
}