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:kx.c.java

public static Object[] autoboxArray(Object data) {
    if (data.getClass().isArray()) {
        Object[] dstArray = new Object[Array.getLength(data)];
        for (int row = 0; row < Array.getLength(data); row++) {
            Object o = Array.get(data, row);
            if (o == null) {
                o = "::";
            }// w  w w .jav  a 2  s  .  com
            if (o instanceof char[]) {
                // char arrays are strings...
                o = new String((char[]) o);
            } else if (o.getClass().isArray()) {
                o = autoboxArray(o);
            }
            Array.set(dstArray, row, o);

        }
        return dstArray;
    }
    throw new IncompleteArgumentException("data was not an array");
}

From source file:de.undercouch.gradle.tasks.download.DownloadAction.java

@Override
public void src(Object src) throws MalformedURLException {
    if (src instanceof Closure) {
        //lazily evaluate closure
        Closure<?> closure = (Closure<?>) src;
        src = closure.call();/*from www . java  2  s. co  m*/
    }

    if (src instanceof CharSequence) {
        sources.add(new URL(src.toString()));
    } else if (src instanceof URL) {
        sources.add((URL) src);
    } else if (src instanceof Collection) {
        Collection<?> sc = (Collection<?>) src;
        for (Object sco : sc) {
            src(sco);
        }
    } else if (src != null && src.getClass().isArray()) {
        int len = Array.getLength(src);
        for (int i = 0; i < len; ++i) {
            Object sco = Array.get(src, i);
            src(sco);
        }
    } else {
        throw new IllegalArgumentException(
                "Download source must " + "either be a URL, a CharSequence, a Collection or an array.");
    }
}

From source file:com.taobao.adfs.util.Utilities.java

public static boolean deepEquals(Object object0, Object object1) {
    if (object0 == null && object1 == null)
        return true;
    if (object0 == null && object1 != null)
        return false;
    if (object0 != null && object1 == null)
        return false;
    if (!object0.getClass().equals(object1.getClass()))
        return false;
    if (object0.equals(object1))
        return true;
    if (object0.getClass().isArray() && Array.getLength(object0) == Array.getLength(object1)) {
        int length = Array.getLength(object0);
        for (int i = 0; i < length; ++i) {
            if (!deepEquals(Array.get(object0, i), Array.get(object1, i)))
                return false;
        }/*from w w w. j  a  v a  2 s .c  om*/
        return true;
    }
    return false;
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static String getVolumePath(StorageManager mStorageManager, final String volumeId) {
    try {/*w w  w .ja va 2  s  .  c o m*/
        Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");

        Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
        Method getUuid = storageVolumeClazz.getMethod("getUuid");
        Method getPath = storageVolumeClazz.getMethod("getPath");
        Method isPrimary = storageVolumeClazz.getMethod("isPrimary");
        Object result = getVolumeList.invoke(mStorageManager);

        final int length = Array.getLength(result);
        for (int i = 0; i < length; i++) {
            Object storageVolumeElement = Array.get(result, i);
            String uuid = (String) getUuid.invoke(storageVolumeElement);
            Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement);

            // primary volume?
            if (primary && "primary".equals(volumeId)) {
                return (String) getPath.invoke(storageVolumeElement);
            }

            // other volumes?
            if (uuid != null) {
                if (uuid.equals(volumeId)) {
                    return (String) getPath.invoke(storageVolumeElement);
                }
            }
        }

        // not found.
        return null;
    } catch (Exception ex) {
        return null;
    }
}

From source file:com.flexive.shared.FxFormatUtils.java

/**
 * Generic SQL escape method./*  w w w.ja v  a 2  s. co m*/
 *
 * @param value the value to be formatted
 * @return the formatted value
 */
public static String escapeForSql(Object value) {
    if (value instanceof FxValue) {
        return ((FxValue) value).getSqlValue();
    } else if (value instanceof String) {
        return "'" + StringUtils.replace((String) value, "'", "''") + "'";
    } else if (value instanceof Date) {
        return "'" + new Formatter().format("%tF", (Date) value) + "'";
    } else if (value instanceof FxSelectListItem) {
        return String.valueOf(((FxSelectListItem) value).getId());
    } else if (value instanceof SelectMany) {
        final SelectMany selectMany = (SelectMany) value;
        final List<Long> selectedIds = selectMany.getSelectedIds();
        if (selectedIds.size() > 1) {
            return "(" + StringUtils.join(selectedIds.iterator(), ',') + ")";
        } else if (selectedIds.size() == 1) {
            return String.valueOf(selectedIds.get(0));
        } else {
            return "-1";
        }
    } else if (value != null && value.getClass().isArray()) {
        // decode array via reflection to support primitive arrays
        final List<Object> result = new ArrayList<Object>();
        final int len = Array.getLength(value);
        for (int i = 0; i < len; i++) {
            result.add(Array.get(value, i));
        }
        return makeTuple(result);
    } else if (value instanceof Collection) {
        return makeTuple((Collection) value);
    } else if (value != null) {
        return value.toString();
    } else {
        return "null";
    }
}

From source file:org.apache.torque.util.SqlExpression.java

/**
 * Takes a columnName and criteria (which must be an array) and builds a SQL 'IN' expression taking into account the ignoreCase
 * flag./*from   w ww  .  j a  va2s  . c o m*/
 *
 * @param  columnName  A column.
 * @param  criteria  The value to compare the column against.
 * @param  comparison  Either " IN " or " NOT IN ".
 * @param  ignoreCase  If true and columns represent Strings, the appropriate function defined for the database will be used to
 *                     ignore differences in case.
 * @param  db  Represents the database in use, for vendor specific functions.
 * @param  whereClause  A StringBuffer to which the sql expression will be appended.
 */
static void buildIn(String columnName, Object criteria, SqlEnum comparison, boolean ignoreCase, DB db,
        StringBuffer whereClause) {
    if (ignoreCase) {
        whereClause.append(db.ignoreCase(columnName));
    } else {
        whereClause.append(columnName);
    }

    whereClause.append(comparison);
    HashSet inClause = new HashSet();
    if (criteria instanceof List) {
        Iterator iter = ((List) criteria).iterator();
        while (iter.hasNext()) {
            Object value = iter.next();

            // The method processInValue() quotes the string
            // and/or wraps it in UPPER().
            inClause.add(processInValue(value, ignoreCase, db));
        }
    } else if (criteria instanceof String) {
        // subquery
        inClause.add(criteria);
    } else {
        // Assume array.
        for (int i = 0; i < Array.getLength(criteria); i++) {
            Object value = Array.get(criteria, i);

            // The method processInValue() quotes the string
            // and/or wraps it in UPPER().
            inClause.add(processInValue(value, ignoreCase, db));
        }
    }
    whereClause.append('(').append(StringUtils.join(inClause.iterator(), ",")).append(')');
}

From source file:com.flexive.faces.FxJsfComponentUtils.java

/**
 * Check if the given array contains an actual value
 *
 * @param valueArray value array// w  w  w  .  jav a2  s .  c o m
 * @return if an actual value is set in the array
 */
public static boolean containsaValue(Object valueArray) {
    if (null != valueArray) {
        int len = Array.getLength(valueArray);
        for (int i = 0; i < len; i++) {
            Object value = Array.get(valueArray, i);
            if (value != null && !(value.equals(FxSelectRenderer.NO_VALUE))) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.espertech.esper.support.util.ArrayAssertionUtil.java

private static Object[] toObjectArray(Object array) {
    if ((array == null) || (!array.getClass().isArray())) {
        throw new IllegalArgumentException("Object not an array but type '" + array.getClass() + "'");
    }//w w w .  j  a  va  2s  .c  o m
    int size = Array.getLength(array);
    Object[] val = new Object[size];
    for (int i = 0; i < size; i++) {
        val[i] = Array.get(array, i);
    }
    return val;
}

From source file:de.micromata.genome.util.bean.PrivateBeanUtils.java

/**
 * Gets the bean size intern.//ww w. jav  a  2s  .  co m
 *
 * @param bean the bean
 * @param clazz the clazz
 * @param m the m
 * @param classNameMatcher the class name matcher
 * @param fieldNameMatcher the field name matcher
 * @return the bean size intern
 */
public static int getBeanSizeIntern(Object bean, Class<?> clazz, IdentityHashMap<Object, Object> m,
        Matcher<String> classNameMatcher, Matcher<String> fieldNameMatcher) {
    if (classNameMatcher.match(clazz.getName()) == false) {
        return 0;
    }
    if (clazz.isArray() == true) {
        if (clazz == boolean[].class) {
            return (((boolean[]) bean).length * 4);
        } else if (clazz == char[].class) {
            return (((char[]) bean).length * 2);
        } else if (clazz == byte[].class) {
            return (((byte[]) bean).length * 1);
        } else if (clazz == short[].class) {
            return (((short[]) bean).length * 2);
        } else if (clazz == int[].class) {
            return (((int[]) bean).length * 4);
        } else if (clazz == long[].class) {
            return (((long[]) bean).length * 4);
        } else if (clazz == float[].class) {
            return (((float[]) bean).length * 4);
        } else if (clazz == double[].class) {
            return (((double[]) bean).length * 8);
        } else {
            int length = Array.getLength(bean);
            int ret = (length * 4);
            for (int i = 0; i < length; ++i) {
                ret += getBeanSize(Array.get(bean, i), m, classNameMatcher, fieldNameMatcher);
            }
            return ret;
        }
    }
    int ret = 0;
    try {
        for (Field f : clazz.getDeclaredFields()) {
            int mod = f.getModifiers();
            if (Modifier.isStatic(mod) == true) {
                continue;
            }
            if (fieldNameMatcher.match(clazz.getName() + "." + f.getName()) == false) {
                continue;
            }
            if (f.getType() == Boolean.TYPE) {
                ret += 4;
            } else if (f.getType() == Character.TYPE) {
                ret += 2;
            } else if (f.getType() == Byte.TYPE) {
                ret += 1;
            } else if (f.getType() == Short.TYPE) {
                ret += 2;
            } else if (f.getType() == Integer.TYPE) {
                ret += 4;
            } else if (f.getType() == Long.TYPE) {
                ret += 8;
            } else if (f.getType() == Float.TYPE) {
                ret += 4;
            } else if (f.getType() == Double.TYPE) {
                ret += 8;
            } else {

                ret += 4;
                Object o = null;
                try {
                    o = readField(bean, f);
                    if (o == null) {
                        continue;
                    }
                } catch (NoClassDefFoundError ex) {
                    // nothing
                    continue;
                }
                int nestedsize = getBeanSize(o, o.getClass(), m, classNameMatcher, fieldNameMatcher);
                ret += nestedsize;
            }
        }
    } catch (NoClassDefFoundError ex) {
        // ignore here.
    }
    if (clazz == Object.class || clazz.getSuperclass() == null) {
        return ret;
    }
    ret += getBeanSizeIntern(bean, clazz.getSuperclass(), m, classNameMatcher, fieldNameMatcher);
    return ret;
}

From source file:com.gigaspaces.persistency.metadata.DefaultSpaceDocumentMapper.java

private BasicDBList toArray(Object property) {
    BasicDBList builder = new BasicDBList();

    int length = Array.getLength(property);

    builder.add(property.getClass().getName());

    for (int i = 0; i < length; i++) {
        Object obj = toObject(Array.get(property, i));
        setArray(builder, obj);/*  ww w . jav  a 2s .  c o m*/
    }

    return builder;
}