Example usage for java.lang.reflect Array getLength

List of usage examples for java.lang.reflect Array getLength

Introduction

In this page you can find the example usage for java.lang.reflect Array getLength.

Prototype

@HotSpotIntrinsicCandidate
public static native int getLength(Object array) throws IllegalArgumentException;

Source Link

Document

Returns the length of the specified array object, as an int .

Usage

From source file:jef.tools.ArrayUtils.java

/**
 * ?:?//ww w.  j  a  v  a  2  s .  co m
 * 
 * @deprecated please use the method in JDK {@link Array#getLength(Object)}
 */
public static int length(Object obj) {
    return Array.getLength(obj);
}

From source file:ArrayUtils.java

/**
 * A utility version of equals that allows comparison of null objects and
 * arrays. WARNING: Does not account for an array that contains a reference
 * to itself, directly or indirectly//from w w  w. j  ava2s  .  com
 * 
 * @param o1
 *            The first object to compare
 * @param o2
 *            The second object to compare
 * @return true if <code>o1</code> and <code>o2</code> are arrays and their
 *         elements are equivalent or if either
 *         <code>o1==null && o2==null</code> or <code>o1.equals(o2)</code>,
 *         false otherwise
 */
public static boolean equals(Object o1, Object o2) {
    if (o1 == null)
        return o2 == null;
    if (o2 == null)
        return false;
    if (o1 instanceof Object[] && o2 instanceof Object[])
        return equals((Object[]) o1, (Object[]) o2);
    if (o1.getClass().isArray() && o2.getClass().isArray()) {
        if (!o1.getClass().equals(o2.getClass()))
            return false;
        int len = Array.getLength(o1);
        if (len != Array.getLength(o2))
            return false;
        for (int i = 0; i < len; i++) {
            if (!equals(Array.get(o1, i), Array.get(o2, i)))
                return false;
        }
        return true;
    }
    return o1.equals(o2);
}

From source file:jef.tools.ArrayUtils.java

/**
 * ?:set ??-1?-2/*ww w  .j ava  2 s. c  o  m*/
 * set???
 */
public static Object setValueAndExpandArray(Object obj, int index, Object value) {
    int length = Array.getLength(obj);
    Object result = obj;
    if (index < 0 && index + length >= 0) {
        index += length;
    } else if (index < 0) {// ?
        result = toFixLength(obj, -index);
    } else if (index >= length) {// 
        result = toFixLength(obj, index + 1);
    }
    set(result, index, value);
    return result;
}

From source file:cn.remex.core.util.ObjectUtils.java

/**
 * Convert the given array (which may be a primitive array) to an
 * object array (if necessary of primitive wrapper objects).
 * <p>A <code>null</code> source value will be converted to an
 * empty Object array./*  w  ww . j a v a2  s.com*/
 * @param source the (potentially primitive) array
 * @return the corresponding object array (never <code>null</code>)
 * @throws IllegalArgumentException if the parameter is not an array
 */
public static Object[] toObjectArray(final Object source) {
    if (source instanceof Object[]) {
        return (Object[]) source;
    }
    if (source == null) {
        return new Object[0];
    }
    if (!source.getClass().isArray()) {
        throw new IllegalArgumentException("Source is not an array: " + source);
    }
    int length = Array.getLength(source);
    if (length == 0) {
        return new Object[0];
    }
    Class<?> wrapperType = Array.get(source, 0).getClass();
    Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
    for (int i = 0; i < length; i++) {
        newArray[i] = Array.get(source, i);
    }
    return newArray;
}

From source file:jef.tools.ArrayUtils.java

/**
 * ?:get/*from   w ww.  ja  v  a  2s  .  c om*/
 * 
 * @param obj
 *            
 * @param index
 *            ?? ??-1?-2
 */
public final static Object get(Object obj, int index) {
    if (index >= 0) {
        return Array.get(obj, index);
    } else {
        return Array.get(obj, Array.getLength(obj) + index);
    }
}

From source file:jef.tools.ArrayUtils.java

/**
 * ?:set//from   w w w  .  j  a v a2  s .c o  m
 * 
 * @param: index
 *             ??-1?-2
 */
public final static void set(Object obj, int index, Object value) {
    if (index >= 0) {
        Array.set(obj, index, value);
    } else {
        Array.set(obj, Array.getLength(obj) + index, value);
    }
}

From source file:com.github.helenusdriver.driver.tools.Tool.java

/**
 * Gets the initial objects to insert using the specified initial method and
 * suffixes/*from  www.  j a  va 2 s  . com*/
 *
 * @author paouelle
 *
 * @param  initial a non-<code>null</code> initial method to retreive objects with
 * @param  suffixes the non-<code>null</code> map of suffixes configured
 * @return the initial objects to insert in the table or <code>null</code>
 *         if none needs to be inserted
 */
private static List<Object> getInitialObjects(Method initial, Map<String, String> suffixes) {
    try {
        final Object array = initial.invoke(null, suffixes);

        if (array == null) {
            return Collections.emptyList();
        }
        final int length = Array.getLength(array);
        final List<Object> objects = new ArrayList<>(length);

        for (int i = 0; i < length; i++) {
            objects.add(Array.get(array, i));
        }
        return objects;
    } catch (IllegalAccessException e) { // should not happen
        throw new IllegalStateException(e);
    } catch (InvocationTargetException e) {
        final Throwable t = e.getTargetException();

        if (t instanceof Error) {
            throw (Error) t;
        } else if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else { // we don't expect any of those
            throw new IllegalStateException(t);
        }
    }
}

From source file:demo.config.PropertyConverter.java

/**
 * Returns a collection with all values contained in the specified object.
 * This method is used for instance by the {@code addProperty()}
 * implementation of the default configurations to gather all values of the
 * property to add. Depending on the type of the passed in object the
 * following things happen://from   www. ja v a  2s.  c  om
 * <ul>
 * <li>Strings are checked for delimiter characters and split if necessary.</li>
 * <li>For objects implementing the {@code Iterable} interface, the
 * corresponding {@code Iterator} is obtained, and contained elements are
 * added to the resulting collection.</li>
 * <li>Arrays are treated as {@code Iterable} objects.</li>
 * <li>All other types are directly inserted.</li>
 * <li>Recursive combinations are supported, e.g. a collection containing an
 * array that contains strings: The resulting collection will only contain
 * primitive objects (hence the name &quot;flatten&quot;).</li>
 * </ul>
 * 
 * @param value
 *            the value to be processed
 * @param delimiter
 *            the delimiter for String values
 * @return a &quot;flat&quot; collection containing all primitive values of
 *         the passed in object
 */
private static Collection<?> flatten(Object value, char delimiter) {
    if (value instanceof String) {
        String s = (String) value;
        if (s.indexOf(delimiter) > -1) {
            return split(s, delimiter);
        }
    }

    Collection<Object> result = new LinkedList<Object>();
    if (value instanceof Iterable) {
        flattenIterator(result, ((Iterable<?>) value).iterator(), delimiter);
    } else if (value instanceof Iterator) {
        flattenIterator(result, (Iterator<?>) value, delimiter);
    } else if (value != null) {
        if (value.getClass().isArray()) {
            for (int len = Array.getLength(value), idx = 0; idx < len; idx++) {
                result.addAll(flatten(Array.get(value, idx), delimiter));
            }
        } else {
            result.add(value);
        }
    }

    return result;
}

From source file:lodsve.core.config.properties.PropertyConverter.java

/**
 * Returns a collection with all values contained in the specified object.
 * This method is used for instance by the {@code addProperty()}
 * implementation of the default configurations to gather all values of the
 * property to add. Depending on the type of the passed in object the
 * following things happen:/*from   w  w w  .ja va 2  s . c  o  m*/
 * <ul>
 * <li>Strings are checked for delimiter characters and split if necessary.</li>
 * <li>For objects implementing the {@code Iterable} interface, the
 * corresponding {@code Iterator} is obtained, and contained elements are
 * added to the resulting collection.</li>
 * <li>Arrays are treated as {@code Iterable} objects.</li>
 * <li>All other types are directly inserted.</li>
 * <li>Recursive combinations are supported, e.g. a collection containing an
 * array that contains strings: The resulting collection will only contain
 * primitive objects (hence the name &quot;flatten&quot;).</li>
 * </ul>
 * 
 * @param value
 *            the value to be processed
 * @param delimiter
 *            the delimiter for String values
 * @return a &quot;flat&quot; collection containing all primitive values of
 *         the passed in object
 */
private static Collection<?> flatten(Object value, char delimiter) {
    if (value instanceof String) {
        String s = (String) value;
        if (s.indexOf(delimiter) > 0) {
            return split(s, delimiter);
        }
    }

    Collection<Object> result = new LinkedList<Object>();
    if (value instanceof Iterable) {
        flattenIterator(result, ((Iterable<?>) value).iterator(), delimiter);
    } else if (value instanceof Iterator) {
        flattenIterator(result, (Iterator<?>) value, delimiter);
    } else if (value != null) {
        if (value.getClass().isArray()) {
            for (int len = Array.getLength(value), idx = 0; idx < len; idx++) {
                result.addAll(flatten(Array.get(value, idx), delimiter));
            }
        } else {
            result.add(value);
        }
    }

    return result;
}