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

public static int[] convertToIntArray(final Object array) {
    if (array == null) {
        return null;
    }//from w w w  .  jav  a  2  s .com
    if (array instanceof int[]) {
        return (int[]) array;
    }
    if (array instanceof Integer[]) {
        return ArrayUtils.toPrimitive((Integer[]) array);
    }
    final int[] newArray = new int[Array.getLength(array)];
    for (int i = 0; i < newArray.length; i++) {
        final Number val = (Number) Array.get(array, i);
        newArray[i] = val.intValue();
    }
    return newArray;
}

From source file:com.espertech.esper.epl.expression.dot.ExprDotEvalArrayGet.java

public Object evaluate(Object target, EventBean[] eventsPerStream, boolean isNewData,
        ExprEvaluatorContext exprEvaluatorContext) {
    if (target == null) {
        return null;
    }//from   w  ww. ja  v  a  2  s.co m

    Object index = indexExpression.evaluate(eventsPerStream, isNewData, exprEvaluatorContext);
    if (index == null) {
        return null;
    }
    if (!(index instanceof Integer)) {
        return null;
    }
    int indexNum = (Integer) index;

    if (Array.getLength(target) <= indexNum) {
        return null;
    }
    return Array.get(target, indexNum);
}

From source file:org.springmodules.validation.util.condition.collection.AtLeastCollectionCondition.java

/**
 * Checks whether at least X objects in the given array adhere to the associated condition. X is determined
 * by the <code>getCount()</code> method call.
 *
 * @param array The array to be checked.
 * @return <code>true</code> if at least X objects in the given array adhere to the associated element condition,
 *         <code>false</code> otherwise.
 *///from w ww.ja  v  a  2  s .  c  om
protected boolean checkArray(Object array) {
    int counter = 0;
    for (int i = 0; i < Array.getLength(array); i++) {
        if (getElementCondition().check(Array.get(array, i))) {
            counter++;
        }
    }
    return counter >= getCount();
}

From source file:ArrayGrowTest.java

/**
 * A convenience method to print all elements in an array
 * /*from  ww w  .j  av  a 2s .com*/
 * @param a
 *          the array to print. It can be an object array or a primitive type
 *          array
 */
static void arrayPrint(Object a) {
    Class cl = a.getClass();
    if (!cl.isArray())
        return;
    Class componentType = cl.getComponentType();
    int length = Array.getLength(a);
    System.out.print(componentType.getName() + "[" + length + "] = { ");
    for (int i = 0; i < Array.getLength(a); i++)
        System.out.print(Array.get(a, i) + " ");
    System.out.println("}");
}

From source file:edu.sdsc.scigraph.services.jersey.writers.BbopJsGraphWriter.java

static Collection<String> getCategories(Vertex vertex) {
    Collection<String> categories = new HashSet<>();
    if (vertex.getPropertyKeys().contains(Concept.CATEGORY)) {
        Object value = vertex.getProperty(Concept.CATEGORY);
        if (value.getClass().isArray()) {
            for (int i = 0; i < Array.getLength(value); i++) {
                categories.add((String) Array.get(value, i));
            }/*from w w w  .  j  ava2s . c o  m*/
        } else if (value instanceof Iterable) {
            categories.addAll(newArrayList((Iterable<String>) value));
        } else {
            categories.add((String) value);
        }
    }
    return categories;
}

From source file:com.eas.client.reports.JSDynaBean.java

@Override
public Object get(String aName, int aIndex) {
    Object value = delegate.getMember(aName);
    if (value == null) {
        throw new NullPointerException("No indexed value for '" + aName + "[" + aIndex + "]'");
    } else if (value instanceof JSObject) {
        return wrap(((JSObject) value).getSlot(aIndex), timezoneOffset);
    } else if (value.getClass().isArray()) {
        return wrap(Array.get(value, aIndex), timezoneOffset);
    } else if (value instanceof List) {
        return wrap(((List) value).get(aIndex), timezoneOffset);
    } else {/*from  www .ja va  2s . c om*/
        throw new IllegalArgumentException("Non-indexed property for '" + aName + "[" + aIndex + "]'");
    }
}

From source file:com.espertech.esper.epl.enummethod.eval.EnumEvalSequenceEqual.java

public Object evaluateEnumMethod(EventBean[] eventsLambda, Collection target, boolean isNewData,
        ExprEvaluatorContext context) {/*from w w w. j  a  v  a 2s .com*/
    Object otherObj = this.getInnerExpression().evaluate(eventsLambda, isNewData, context);

    if (otherObj == null) {
        return false;
    }
    if (!(otherObj instanceof Collection)) {
        if (otherObj.getClass().isArray()) {
            if (target.size() != Array.getLength(otherObj)) {
                return false;
            }

            if (target.isEmpty()) {
                return true;
            }

            Iterator oneit = target.iterator();
            for (int i = 0; i < target.size(); i++) {
                Object first = oneit.next();
                Object second = Array.get(otherObj, i);

                if (first == null) {
                    if (second != null) {
                        return false;
                    }
                    continue;
                }
                if (second == null) {
                    return false;
                }

                if (!first.equals(second)) {
                    return false;
                }
            }

            return true;
        } else {
            log.warn(
                    "Enumeration method 'sequenceEqual' expected a Collection-type return value from its parameter but received '"
                            + otherObj.getClass() + "'");
            return false;
        }
    }

    Collection other = (Collection) otherObj;
    if (target.size() != other.size()) {
        return false;
    }

    if (target.isEmpty()) {
        return true;
    }

    Iterator oneit = target.iterator();
    Iterator twoit = other.iterator();
    for (int i = 0; i < target.size(); i++) {
        Object first = oneit.next();
        Object second = twoit.next();

        if (first == null) {
            if (second != null) {
                return false;
            }
            continue;
        }
        if (second == null) {
            return false;
        }

        if (!first.equals(second)) {
            return false;
        }
    }

    return true;
}

From source file:org.diorite.utils.collections.arrays.ReflectArrayIterator.java

@SuppressWarnings("IteratorNextCanNotThrowNoSuchElementException")
@Override/*from  w  ww. ja va 2s.c om*/
public Object next() {
    return Array.get(this.array, this.currentIndex++);
}

From source file:com.scit.sling.test.MockValueMap.java

@SuppressWarnings("unchecked")
private <T> T convertType(Object o, Class<T> type) {
    if (o == null) {
        return null;
    }/*  w  w w .  j  a  v a  2  s .  co  m*/
    if (o.getClass().isArray() && type.isArray()) {
        if (type.getComponentType().isAssignableFrom(o.getClass().getComponentType())) {
            return (T) o;
        } else {
            // we need to convert the elements in the array
            Object array = Array.newInstance(type.getComponentType(), Array.getLength(o));
            for (int i = 0; i < Array.getLength(o); i++) {
                Array.set(array, i, convertType(Array.get(o, i), type.getComponentType()));
            }
            return (T) array;
        }
    }
    if (o.getClass().isAssignableFrom(type)) {
        return (T) o;
    }
    if (String.class.isAssignableFrom(type)) {
        // Format dates
        if (o instanceof Calendar) {
            return (T) formatDate((Calendar) o);
        } else if (o instanceof Date) {
            return (T) formatDate((Date) o);
        } else if (o instanceof DateTime) {
            return (T) formatDate((DateTime) o);
        }
        return (T) String.valueOf(o);
    } else if (o instanceof DateTime) {
        DateTime dt = (DateTime) o;
        if (Calendar.class.isAssignableFrom(type)) {
            return (T) dt.toCalendar(Locale.getDefault());
        } else if (Date.class.isAssignableFrom(type)) {
            return (T) dt.toDate();
        } else if (Number.class.isAssignableFrom(type)) {
            return convertType(dt.getMillis(), type);
        }
    } else if (o instanceof Number && Number.class.isAssignableFrom(type)) {
        if (Byte.class.isAssignableFrom(type)) {
            return (T) (Byte) ((Number) o).byteValue();
        } else if (Double.class.isAssignableFrom(type)) {
            return (T) (Double) ((Number) o).doubleValue();
        } else if (Float.class.isAssignableFrom(type)) {
            return (T) (Float) ((Number) o).floatValue();
        } else if (Integer.class.isAssignableFrom(type)) {
            return (T) (Integer) ((Number) o).intValue();
        } else if (Long.class.isAssignableFrom(type)) {
            return (T) (Long) ((Number) o).longValue();
        } else if (Short.class.isAssignableFrom(type)) {
            return (T) (Short) ((Number) o).shortValue();
        } else if (BigDecimal.class.isAssignableFrom(type)) {
            return (T) new BigDecimal(o.toString());
        }
    } else if (o instanceof Number && type.isPrimitive()) {
        final Number num = (Number) o;
        if (type == byte.class) {
            return (T) new Byte(num.byteValue());
        } else if (type == double.class) {
            return (T) new Double(num.doubleValue());
        } else if (type == float.class) {
            return (T) new Float(num.floatValue());
        } else if (type == int.class) {
            return (T) new Integer(num.intValue());
        } else if (type == long.class) {
            return (T) new Long(num.longValue());
        } else if (type == short.class) {
            return (T) new Short(num.shortValue());
        }
    } else if (o instanceof String && Number.class.isAssignableFrom(type)) {
        if (Byte.class.isAssignableFrom(type)) {
            return (T) new Byte((String) o);
        } else if (Double.class.isAssignableFrom(type)) {
            return (T) new Double((String) o);
        } else if (Float.class.isAssignableFrom(type)) {
            return (T) new Float((String) o);
        } else if (Integer.class.isAssignableFrom(type)) {
            return (T) new Integer((String) o);
        } else if (Long.class.isAssignableFrom(type)) {
            return (T) new Long((String) o);
        } else if (Short.class.isAssignableFrom(type)) {
            return (T) new Short((String) o);
        } else if (BigDecimal.class.isAssignableFrom(type)) {
            return (T) new BigDecimal((String) o);
        }
    }
    throw new NotImplementedException(
            "Can't handle conversion from " + o.getClass().getName() + " to " + type.getName());
}

From source file:org.apache.axis2.jaxws.message.OccurrenceArray.java

/**
 * Get the List or array as a Object[]/*from  w w  w.java 2  s .c o m*/
 * @return Object[] 
 */
public Object[] getAsArray() {
    Object[] objects = null;
    if (value == null) {
        return new Object[0];
    } else if (value instanceof List) {
        List l = (List) value;
        objects = new Object[l.size()];
        for (int i = 0; i < l.size(); i++) {
            objects[i] = l.get(i);
        }
    } else {
        objects = new Object[Array.getLength(value)];
        for (int i = 0; i < objects.length; i++) {
            objects[i] = Array.get(value, i);
        }
    }
    return objects;
}