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.kordamp.ezmorph.array.CharacterObjectArrayMorpher.java

public Object morph(Object array) {
    if (array == null) {
        return null;
    }//from w  w w .  j  a v a  2  s  . co m

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

    if (array.getClass().isArray()) {
        int length = Array.getLength(array);
        int dims = getDimensions(array.getClass());
        int[] dimensions = createDimensions(dims, length);
        Object result = Array.newInstance(Character.class, dimensions);
        if (dims == 1) {
            CharMorpher morpher = null;
            if (isUseDefault()) {
                if (defaultValue == null) {
                    for (int index = 0; index < length; index++) {
                        Array.set(result, index, null);
                    }
                    return result;
                } else {
                    morpher = new CharMorpher(defaultValue.charValue());
                }
            } else {
                morpher = new CharMorpher();
            }
            for (int index = 0; index < length; index++) {
                Array.set(result, index, 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.apache.click.util.RequestTypeConverter.java

/**
 * Return the converted value for the given value object and target type.
 *
 * @param value the value object to convert
 * @param toType the target class type to convert the value to
 * @return a converted value into the specified type
 *//*from   w  w w  . ja va 2 s . c  o  m*/
protected Object convertValue(Object value, Class<?> toType) {
    Object result = null;

    if (value != null) {

        // If array -> array then convert components of array individually
        if (value.getClass().isArray() && toType.isArray()) {
            Class<?> componentType = toType.getComponentType();

            result = Array.newInstance(componentType, Array.getLength(value));

            for (int i = 0, icount = Array.getLength(value); i < icount; i++) {
                Array.set(result, i, convertValue(Array.get(value, i), componentType));
            }

        } else {
            if ((toType == Integer.class) || (toType == Integer.TYPE)) {
                result = Integer.valueOf((int) OgnlOps.longValue(value));

            } else if ((toType == Double.class) || (toType == Double.TYPE)) {
                result = new Double(OgnlOps.doubleValue(value));

            } else if ((toType == Boolean.class) || (toType == Boolean.TYPE)) {
                result = Boolean.valueOf(value.toString());

            } else if ((toType == Byte.class) || (toType == Byte.TYPE)) {
                result = Byte.valueOf((byte) OgnlOps.longValue(value));

            } else if ((toType == Character.class) || (toType == Character.TYPE)) {
                result = Character.valueOf((char) OgnlOps.longValue(value));

            } else if ((toType == Short.class) || (toType == Short.TYPE)) {
                result = Short.valueOf((short) OgnlOps.longValue(value));

            } else if ((toType == Long.class) || (toType == Long.TYPE)) {
                result = Long.valueOf(OgnlOps.longValue(value));

            } else if ((toType == Float.class) || (toType == Float.TYPE)) {
                result = new Float(OgnlOps.doubleValue(value));

            } else if (toType == BigInteger.class) {
                result = OgnlOps.bigIntValue(value);

            } else if (toType == BigDecimal.class) {
                result = bigDecValue(value);

            } else if (toType == String.class) {
                result = OgnlOps.stringValue(value);

            } else if (toType == java.util.Date.class) {
                long time = getTimeFromDateString(value.toString());
                if (time > Long.MIN_VALUE) {
                    result = new java.util.Date(time);
                }

            } else if (toType == java.sql.Date.class) {
                long time = getTimeFromDateString(value.toString());
                if (time > Long.MIN_VALUE) {
                    result = new java.sql.Date(time);
                }

            } else if (toType == java.sql.Time.class) {
                long time = getTimeFromDateString(value.toString());
                if (time > Long.MIN_VALUE) {
                    result = new java.sql.Time(time);
                }

            } else if (toType == java.sql.Timestamp.class) {
                long time = getTimeFromDateString(value.toString());
                if (time > Long.MIN_VALUE) {
                    result = new java.sql.Timestamp(time);
                }
            }
        }

    } else {
        if (toType.isPrimitive()) {
            result = OgnlRuntime.getPrimitiveDefaultValue(toType);
        }
    }
    return result;
}

From source file:com.bstek.dorado.view.type.property.MappingPropertyOutputter.java

protected void outputArray(Mapping mapping, Object elements, OutputContext context) throws Exception {
    String keyProperty = StringUtils.defaultString(mapping.getKeyProperty(), "key");
    String valueProperty = StringUtils.defaultString(mapping.getValueProperty(), "value");

    JsonBuilder json = context.getJsonBuilder();
    json.array();//  w  w  w.  ja v  a 2s  .  com

    int length = Array.getLength(elements);
    for (int i = 0; i < length; i++) {
        Object element = Array.get(elements, i);
        EntityWrapper entity = EntityWrapper.create(element);
        json.object();

        json.key("key");
        outputData(entity.get(keyProperty), context);
        json.endKey();

        json.key("value");
        outputData(entity.get(valueProperty), context);
        json.endKey();

        json.endObject();
    }
    json.endArray();
}

From source file:es.caib.zkib.jxpath.util.BasicTypeConverter.java

/**
 * Returns true if it can convert the supplied
 * object to the specified class./*from  w w  w. j  ava  2 s.  c o  m*/
 * @param object to check
 * @param toType prospective destination class
 * @return boolean
 */
public boolean canConvert(Object object, final Class toType) {
    if (object == null) {
        return true;
    }
    final Class useType = TypeUtils.wrapPrimitive(toType);
    Class fromType = object.getClass();

    if (useType.isAssignableFrom(fromType)) {
        return true;
    }

    if (useType == String.class) {
        return true;
    }

    if (object instanceof Boolean && (Number.class.isAssignableFrom(useType)
            || "java.util.concurrent.atomic.AtomicBoolean".equals(useType.getName()))) {
        return true;
    }
    if (object instanceof Number && (Number.class.isAssignableFrom(useType) || useType == Boolean.class)) {
        return true;
    }
    if (object instanceof String && (useType == Boolean.class || useType == Character.class
            || useType == Byte.class || useType == Short.class || useType == Integer.class
            || useType == Long.class || useType == Float.class || useType == Double.class)) {
        return true;
    }
    if (fromType.isArray()) {
        // Collection -> array
        if (useType.isArray()) {
            Class cType = useType.getComponentType();
            int length = Array.getLength(object);
            for (int i = 0; i < length; i++) {
                Object value = Array.get(object, i);
                if (!canConvert(value, cType)) {
                    return false;
                }
            }
            return true;
        }
        if (Collection.class.isAssignableFrom(useType)) {
            return canCreateCollection(useType);
        }
        if (Array.getLength(object) > 0) {
            Object value = Array.get(object, 0);
            return canConvert(value, useType);
        }
        return canConvert("", useType);
    }
    if (object instanceof Collection) {
        // Collection -> array
        if (useType.isArray()) {
            Class cType = useType.getComponentType();
            Iterator it = ((Collection) object).iterator();
            while (it.hasNext()) {
                Object value = it.next();
                if (!canConvert(value, cType)) {
                    return false;
                }
            }
            return true;
        }
        if (Collection.class.isAssignableFrom(useType)) {
            return canCreateCollection(useType);
        }
        if (((Collection) object).size() > 0) {
            Object value;
            if (object instanceof List) {
                value = ((List) object).get(0);
            } else {
                Iterator it = ((Collection) object).iterator();
                value = it.next();
            }
            return canConvert(value, useType);
        }
        return canConvert("", useType);
    }
    if (object instanceof NodeSet) {
        return canConvert(((NodeSet) object).getValues(), useType);
    }
    if (object instanceof Pointer) {
        return canConvert(((Pointer) object).getValue(), useType);
    }
    return ConvertUtils.lookup(useType) != null;
}

From source file:com.googlecode.jmxtrans.connections.JMXConnectionParams.java

private ImmutableList<?> asList(Object array) {
    ImmutableList.Builder<Object> builder = ImmutableList.builder();
    for (int i = 0; i < getLength(array); i++) {
        builder.add(Array.get(array, i));
    }/*  w ww . j  av a 2 s.  c o  m*/
    return builder.build();
}

From source file:Main.java

/**
 Hash code for an Object.//from w w  w. ja  v a2s  .c o  m
        
 <P><tt>aObject</tt> is a possibly-null object field, and possibly an array.
        
 If <tt>aObject</tt> is an array, then each element may be a primitive
 or a possibly-null object.
 */
public static int hash(int aSeed, Object aObject) {

    int result = aSeed;

    if (aObject == null) {

        result = hash(result, 0);
    } else if (!isArray(aObject)) {

        result = hash(result, aObject.hashCode());
    } else {

        int length = Array.getLength(aObject);

        for (int idx = 0; idx < length; ++idx) {

            Object item = Array.get(aObject, idx);

            //recursive call!
            result = hash(result, item);
        }
    }

    return result;
}

From source file:com.bxf.hradmin.common.utils.QueryParameterTransformer.java

@SuppressWarnings("rawtypes")
private static Object[] asArray(Object value) {
    if (value.getClass().isArray()) {
        Object[] result = new Object[Array.getLength(value)];
        for (int i = 0; i < result.length; ++i) {
            result[i] = Array.get(value, i);
        }//w  w w  . jav  a 2  s  . c o m
        return result;
    } else if (value instanceof Collection) {
        return ((Collection) value).toArray();
    }
    return new Object[] { value };
}

From source file:de.alpharogroup.lang.ObjectExtensions.java

/**
 * Try to clone the given object.//from ww  w .j  a  v a 2s.co  m
 *
 * @param object
 *            The object to clone.
 * @return The cloned object or null if the clone process failed.
 * @throws NoSuchMethodException
 *             the no such method exception
 * @throws SecurityException
 *             Thrown if the security manager indicates a security violation.
 * @throws IllegalAccessException
 *             the illegal access exception
 * @throws IllegalArgumentException
 *             the illegal argument exception
 * @throws InvocationTargetException
 *             the invocation target exception
 * @throws ClassNotFoundException
 *             the class not found exception
 * @throws InstantiationException
 *             the instantiation exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Object cloneObject(final Object object)
        throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
        InvocationTargetException, ClassNotFoundException, InstantiationException, IOException {
    Object clone = null;
    // Try to clone the object if it implements Serializable.
    if (object instanceof Serializable) {
        clone = SerializedObjectUtils.copySerializedObject((Serializable) object);
        if (clone != null) {
            return clone;
        }
    }
    // Try to clone the object if it is Cloneble.
    if (clone == null && object instanceof Cloneable) {

        if (object.getClass().isArray()) {
            final Class<?> componentType = object.getClass().getComponentType();
            if (componentType.isPrimitive()) {
                int length = Array.getLength(object);
                clone = Array.newInstance(componentType, length);
                while (length-- > 0) {
                    Array.set(clone, length, Array.get(object, length));
                }
            } else {
                clone = ((Object[]) object).clone();
            }
            if (clone != null) {
                return clone;
            }
        }
        final Class<?> clazz = object.getClass();
        final Method cloneMethod = clazz.getMethod("clone", (Class[]) null);
        clone = cloneMethod.invoke(object, (Object[]) null);
        if (clone != null) {
            return clone;
        }
    }
    // Try to clone the object by copying all his properties with
    // the BeanUtils.copyProperties() method.
    if (clone == null) {
        clone = ReflectionUtils.getNewInstance(object);
        BeanUtils.copyProperties(clone, object);
    }
    return clone;
}

From source file:com.github.ibole.infrastructure.persistence.db.mybatis.pagination.PagingParametersFinder.java

/**
 * Find whether it contains the <code>PaginationCriteria</code> object in the array.
 *
 * @param array the array.//  w w w.j a  v a 2s . c  o m
 * @return PageQuery
 */
private PagingCriteria findCriteriaFromArray(Object array) {
    if (searchMap.containsKey(array)) {
        return null;
    }

    Object object;
    PagingCriteria pc;
    for (int i = 0; i < Array.getLength(array); i++) {
        object = Array.get(array, i);
        pc = findCriteriaFromObject(object);
        if (pc != null) {
            searchMap.put(array, SqlHelper.EMPTY);
            return pc;
        }
    }
    searchMap.put(array, SqlHelper.EMPTY);
    return null;
}

From source file:mil.jpeojtrs.sca.util.PrimitiveArrayUtils.java

public static char[] convertToCharArray(final Object array) {
    if (array == null) {
        return null;
    }//w w  w.ja v a2 s. co m
    if (array instanceof char[]) {
        return (char[]) array;
    }
    if (array instanceof Character[]) {
        return ArrayUtils.toPrimitive((Character[]) array);
    }
    final char[] newArray = new char[Array.getLength(array)];
    for (int i = 0; i < newArray.length; i++) {
        final Object obj = Array.get(array, i);
        if (obj instanceof Character) {
            newArray[i] = (Character) Array.get(array, i);
        } else {
            newArray[i] = (char) ((Number) Array.get(array, i)).byteValue();
        }
    }
    return newArray;
}