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:MultiMap.java

/** Add element to an array
 * @param array The array to add to (or null)
 * @param item The item to add//from w w w.  j  av a2 s. co  m
 * @param type The type of the array (in case of null array)
 * @return new array with contents of array plus item
 */
public static Object[] addToArray(Object[] array, Object item, Class type) {
    if (array == null) {
        if (type == null && item != null)
            type = item.getClass();
        Object[] na = (Object[]) Array.newInstance(type, 1);
        na[0] = item;
        return na;
    } else {
        Class c = array.getClass().getComponentType();
        Object[] na = (Object[]) Array.newInstance(c, Array.getLength(array) + 1);
        System.arraycopy(array, 0, na, 0, array.length);
        na[array.length] = item;
        return na;
    }
}

From source file:com.sun.faces.renderkit.html_basic.MenuRenderer.java

boolean isSelected(Object itemValue, 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) {
                if (itemValue == null) {
                    return true;
                }// w  w  w.j  ava2 s.  c  o m
            } else if (value.equals(itemValue)) {
                return true;
            }
        }
    }
    return false;
}

From source file:org.apache.myfaces.shared_impl.renderkit.RendererUtils.java

private static Set internalSubmittedOrSelectedValuesAsSet(FacesContext context, UIComponent component,
        Converter converter, UISelectMany uiSelectMany, Object values) {
    if (values == null || EMPTY_STRING.equals(values)) {
        return Collections.EMPTY_SET;
    } else if (values instanceof Object[]) {
        //Object array
        Object[] ar = (Object[]) values;
        if (ar.length == 0) {
            return Collections.EMPTY_SET;
        }/*from ww w  .  j  a v a  2s . c  om*/

        HashSet set = new HashSet(HashMapUtils.calcCapacity(ar.length));
        for (int i = 0; i < ar.length; i++) {
            set.add(getConvertedStringValue(context, component, converter, ar[i]));
        }
        return set;
    } else if (values.getClass().isArray()) {
        //primitive array
        int len = Array.getLength(values);
        HashSet set = new HashSet(org.apache.myfaces.shared_impl.util.HashMapUtils.calcCapacity(len));
        for (int i = 0; i < len; i++) {
            set.add(getConvertedStringValue(context, component, converter, Array.get(values, i)));
        }
        return set;
    } else if (values instanceof List) {
        List lst = (List) values;
        if (lst.size() == 0) {
            return Collections.EMPTY_SET;
        } else {
            HashSet set = new HashSet(HashMapUtils.calcCapacity(lst.size()));
            for (Iterator i = lst.iterator(); i.hasNext();)
                set.add(getConvertedStringValue(context, component, converter, i.next()));

            return set;
        }
    } else {
        throw new IllegalArgumentException("Value of UISelectMany component with path : "
                + getPathToComponent(uiSelectMany) + " is not of type Array or List");
    }
}

From source file:com.icesoft.faces.renderkit.dom_html_basic.MenuRenderer.java

boolean isSelected(Object sentinel, Object selectedValues, FacesContext facesContext, UIComponent uiComponent) {
    boolean isSelected = false;
    if (selectedValues == null || sentinel == null) {
        return isSelected;
    }//  ww  w . j a va2s  .co m
    String formattedSelectedValue;
    String formattedSentinel = formatComponentValue(facesContext, uiComponent, sentinel);
    int length = Array.getLength(selectedValues);
    for (int index = 0; index < length; index++) {
        Object nextSelectedValue = Array.get(selectedValues, index);
        formattedSelectedValue = formatComponentValue(facesContext, uiComponent, nextSelectedValue);
        if (nextSelectedValue == null && sentinel == null) {
            isSelected = true;
            break;
        } else if (nextSelectedValue != null && nextSelectedValue.equals(sentinel)) {
            isSelected = true;
            break;
        } else if (sentinel instanceof String) {
            if (isConversionMatched(sentinel.toString(), nextSelectedValue)) {
                isSelected = true;
                break;
            }
            if (formattedSelectedValue.equals(sentinel)) {
                isSelected = true;
                break;
            }
        } else if (formattedSelectedValue != null && formattedSelectedValue.equals(formattedSentinel)) {
            isSelected = true;
            break;
        }
    }
    return isSelected;
}

From source file:org.apache.openjpa.kernel.AttachStrategy.java

/**
 * Returns an attached version of the <code>frma</code>
 * array if it is different than <code>toa</code>. If the arrays
 * will be identical, returns <code>toa</code>.
 *//*from ww w . j  av a 2  s  .c  om*/
private Object replaceArray(AttachManager manager, Object frma, Object toa, OpenJPAStateManager sm,
        FieldMetaData fmd) {
    int len = Array.getLength(frma);
    boolean diff = toa == null || len != Array.getLength(toa);

    // populate an array copy on the initial assumption that the array
    // is dirty
    Object newa = Array.newInstance(fmd.getElement().getDeclaredType(), len);
    ValueMetaData vmd = fmd.getElement();
    boolean pc = vmd.isDeclaredTypePC();
    Object elem;
    for (int i = 0; i < len; i++) {
        elem = Array.get(frma, i);
        if (pc) {
            if (vmd.getCascadeAttach() == ValueMetaData.CASCADE_NONE)
                elem = getReference(manager, elem, sm, vmd);
            else
                elem = manager.attach(elem, null, sm, vmd, false);
        }
        diff = diff || !equals(elem, Array.get(toa, i), pc);
        Array.set(newa, i, elem);
    }
    return (diff) ? newa : toa;
}

From source file:org.apache.struts.action.DynaActionForm.java

/**
 * <p>Render a String representation of this object.</p>
 *
 * @return A string representation of this object.
 *//*www.  ja v a2  s  . c  om*/
public String toString() {
    StringBuffer sb = new StringBuffer("DynaActionForm[dynaClass=");
    DynaClass dynaClass = getDynaClass();

    if (dynaClass == null) {
        return sb.append("null]").toString();
    }

    sb.append(dynaClass.getName());

    DynaProperty[] props = dynaClass.getDynaProperties();

    if (props == null) {
        props = new DynaProperty[0];
    }

    for (int i = 0; i < props.length; i++) {
        sb.append(',');
        sb.append(props[i].getName());
        sb.append('=');

        Object value = get(props[i].getName());

        if (value == null) {
            sb.append("<NULL>");
        } else if (value.getClass().isArray()) {
            int n = Array.getLength(value);

            sb.append("{");

            for (int j = 0; j < n; j++) {
                if (j > 0) {
                    sb.append(',');
                }

                sb.append(Array.get(value, j));
            }

            sb.append("}");
        } else if (value instanceof List) {
            int n = ((List) value).size();

            sb.append("{");

            for (int j = 0; j < n; j++) {
                if (j > 0) {
                    sb.append(',');
                }

                sb.append(((List) value).get(j));
            }

            sb.append("}");
        } else if (value instanceof Map) {
            int n = 0;
            Iterator keys = ((Map) value).keySet().iterator();

            sb.append("{");

            while (keys.hasNext()) {
                if (n > 0) {
                    sb.append(',');
                }

                n++;

                Object key = keys.next();

                sb.append(key);
                sb.append('=');
                sb.append(((Map) value).get(key));
            }

            sb.append("}");
        } else {
            sb.append(value);
        }
    }

    sb.append("]");

    return (sb.toString());
}

From source file:org.crank.javax.faces.component.MenuRenderer.java

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(RIConstants.NO_VALUE))) {
                return true;
            }/*from  w  w w  . j  a  va2 s  .c  o m*/
        }
    }
    return false;

}

From source file:MultiMap.java

public static Object[] removeFromArray(Object[] array, Object item) {
    if (item == null || array == null)
        return array;
    for (int i = array.length; i-- > 0;) {
        if (item.equals(array[i])) {
            Class c = array == null ? item.getClass() : array.getClass().getComponentType();
            Object[] na = (Object[]) Array.newInstance(c, Array.getLength(array) - 1);
            if (i > 0)
                System.arraycopy(array, 0, na, 0, i);
            if (i + 1 < array.length)
                System.arraycopy(array, i + 1, na, i, array.length - (i + 1));
            return na;
        }//from  w w w.jav a 2s . com
    }
    return array;
}

From source file:net.sf.jasperreports.engine.query.JRJdbcQueryExecuter.java

protected int setStatementMultiParameters(int parameterIndex, String parameterName, boolean ignoreNulls)
        throws SQLException {
    JRValueParameter parameter = getValueParameter(parameterName);
    Object paramValue = parameter.getValue();
    int count;//from   ww w  .  jav  a  2s. c  o m
    int index = 0;
    if (paramValue.getClass().isArray()) {
        int arrayCount = Array.getLength(paramValue);
        for (count = 0; count < arrayCount; ++count) {
            Object value = Array.get(paramValue, count);
            if (!ignoreNulls || value != null) {
                setStatementMultiParameter(parameterIndex + index, parameterName, count, value, parameter);
                ++index;
            }
        }
    } else if (paramValue instanceof Collection<?>) {
        Collection<?> values = (Collection<?>) paramValue;
        count = 0;
        for (Iterator<?> it = values.iterator(); it.hasNext(); ++count) {
            Object value = it.next();

            if (!ignoreNulls || value != null) {
                setStatementMultiParameter(parameterIndex + index, parameterName, count, value, parameter);
                ++index;
            }
        }
    } else {
        throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_UNEXPECTED_MULTI_PARAMETER_TYPE, (Object[]) null);
    }
    return index;
}

From source file:ArrayUtils.java

/**
 * Removes all contents of <code>array2</code> from <code>array1</code>. All
 * instances of <code>array2</code> will also be removed from
 * <code>array1</code>. For primitive types.
 * /*from   w  w  w.j  a  v a2 s  . com*/
 * @param array1
 *            The array to remove elements from
 * @param array2
 *            The array containing the elements to remove; or the element to
 *            remove itself
 * @return <code>array1</code> missing all the contents of
 *         <code>array2</code>
 */
public static Object removeAllP(Object array1, Object array2) {
    if (array1 == null || array2 == null)
        return array1;
    if (!array1.getClass().isArray())
        return null;
    if (!array2.getClass().isArray())
        array2 = new Object[] { array2 };
    else
        array2 = addP(array2, array2);
    java.util.BitSet remove = new java.util.BitSet();
    int len1 = Array.getLength(array1);
    int len2 = Array.getLength(array2);
    int i, j;
    for (i = 0; i < len1; i++) {
        for (j = 0; j < len2; j++) {
            if (equals(Array.get(array1, i), Array.get(array2, j))) {
                remove.set(i);
                break;
            }
        }
    }
    Object ret = Array.newInstance(array1.getClass().getComponentType(), len1 - remove.cardinality());
    // This copying section might be replaced by a more efficient version
    // using System.arraycopy()--this would be much faster than reflection,
    // especially for large arrays needing only a few elements removed
    for (i = 0, j = 0; i < len1; i++) {
        if (!remove.get(i)) {
            put(ret, Array.get(array1, i), j);
            j++;
        }
    }
    return ret;
}