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: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);//from  ww w  . j a v  a  2  s  .  c om
    }

    return builder;
}

From source file:javadz.beanutils.PropertyUtilsBean.java

/**
 * Return the value of the specified indexed property of the specified
 * bean, with no type conversions.  In addition to supporting the JavaBeans
 * specification, this method has been extended to support
 * <code>List</code> objects as well.
 *
 * @param bean Bean whose property is to be extracted
 * @param name Simple property name of the property value to be extracted
 * @param index Index of the property value to be extracted
 * @return the indexed property value/*from w  w w  . j  a v a  2  s. c  om*/
 *
 * @exception IndexOutOfBoundsException if the specified index
 *  is outside the valid range for the underlying property
 * @exception IllegalAccessException if the caller does not have
 *  access to the property accessor method
 * @exception IllegalArgumentException if <code>bean</code> or
 *  <code>name</code> is null
 * @exception InvocationTargetException if the property accessor method
 *  throws an exception
 * @exception NoSuchMethodException if an accessor method for this
 *  propety cannot be found
 */
public Object getIndexedProperty(Object bean, String name, int index)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

    if (bean == null) {
        throw new IllegalArgumentException("No bean specified");
    }
    if (name == null || name.length() == 0) {
        if (bean.getClass().isArray()) {
            return Array.get(bean, index);
        } else if (bean instanceof List) {
            return ((List) bean).get(index);
        }
    }
    if (name == null) {
        throw new IllegalArgumentException("No name specified for bean class '" + bean.getClass() + "'");
    }

    // Handle DynaBean instances specially
    if (bean instanceof DynaBean) {
        DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name);
        if (descriptor == null) {
            throw new NoSuchMethodException(
                    "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
        }
        return (((DynaBean) bean).get(name, index));
    }

    // Retrieve the property descriptor for the specified property
    PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
    if (descriptor == null) {
        throw new NoSuchMethodException(
                "Unknown property '" + name + "' on bean class '" + bean.getClass() + "'");
    }

    // Call the indexed getter method if there is one
    if (descriptor instanceof IndexedPropertyDescriptor) {
        Method readMethod = ((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod();
        readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
        if (readMethod != null) {
            Object[] subscript = new Object[1];
            subscript[0] = new Integer(index);
            try {
                return (invokeMethod(readMethod, bean, subscript));
            } catch (InvocationTargetException e) {
                if (e.getTargetException() instanceof IndexOutOfBoundsException) {
                    throw (IndexOutOfBoundsException) e.getTargetException();
                } else {
                    throw e;
                }
            }
        }
    }

    // Otherwise, the underlying property must be an array
    Method readMethod = getReadMethod(bean.getClass(), descriptor);
    if (readMethod == null) {
        throw new NoSuchMethodException(
                "Property '" + name + "' has no " + "getter method on bean class '" + bean.getClass() + "'");
    }

    // Call the property getter and return the value
    Object value = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY);
    if (!value.getClass().isArray()) {
        if (!(value instanceof java.util.List)) {
            throw new IllegalArgumentException(
                    "Property '" + name + "' is not indexed on bean class '" + bean.getClass() + "'");
        } else {
            //get the List's value
            return ((java.util.List) value).get(index);
        }
    } else {
        //get the array's value
        try {
            return (Array.get(value, index));
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new ArrayIndexOutOfBoundsException(
                    "Index: " + index + ", Size: " + Array.getLength(value) + " for property '" + name + "'");
        }
    }

}

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;
                }/*from w ww  .  j a va2 s. co  m*/
            } else if (value.equals(itemValue)) {
                return true;
            }
        }
    }
    return false;
}

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;
    }/*  w ww  .  j  ava  2 s . c o  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.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  w  w  w .  ja  va  2 s  . 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: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  v a  2  s.  c o m
        }
    }
    return false;

}

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  w w  w.j  av  a  2  s .c  o m
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.eclipse.dataset.AbstractDataset.java

/**
 * Get dataset type from an object. The following are supported: Java Number objects, Apache common math Complex
 * objects, Java arrays and lists/* w  w w.ja v  a2  s.c  om*/
 * 
 * @param obj
 * @return dataset type
 */
public static int getDTypeFromObject(Object obj) {
    int dtype = -1;

    if (obj == null) {
        return dtype;
    }

    if (obj instanceof List<?>) {
        List<?> jl = (List<?>) obj;
        int l = jl.size();
        for (int i = 0; i < l; i++) {
            int ldtype = getDTypeFromObject(jl.get(i));
            if (ldtype > dtype) {
                dtype = ldtype;
            }
        }
    } else if (obj.getClass().isArray()) {
        Class<?> ca = obj.getClass().getComponentType();
        if (isComponentSupported(ca)) {
            return getDTypeFromClass(ca);
        }
        int l = Array.getLength(obj);
        for (int i = 0; i < l; i++) {
            Object lo = Array.get(obj, i);
            int ldtype = getDTypeFromObject(lo);
            if (ldtype > dtype) {
                dtype = ldtype;
            }
        }
    } else if (obj instanceof Dataset) {
        return ((Dataset) obj).getDtype();
    } else if (obj instanceof ILazyDataset) {
        dtype = getDTypeFromClass(((ILazyDataset) obj).elementClass(),
                ((ILazyDataset) obj).getElementsPerItem());
    } else {
        dtype = getDTypeFromClass(obj.getClass());
    }
    return dtype;
}

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. j a  v a2 s  .c om
    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:org.apache.struts.action.DynaActionForm.java

/**
 * <p>Render a String representation of this object.</p>
 *
 * @return A string representation of this object.
 *//* www  .  j a  v  a 2s  .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());
}