Example usage for java.lang.reflect Array set

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

Introduction

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

Prototype

public static native void set(Object array, int index, Object value)
        throws IllegalArgumentException, ArrayIndexOutOfBoundsException;

Source Link

Document

Sets the value of the indexed component of the specified array object to the specified new value.

Usage

From source file:com.haulmont.cuba.web.app.ui.jmxcontrol.util.AttributeEditor.java

public Object getAttributeValue(boolean allowNull) {
    if (checkBox != null) {
        Boolean value = checkBox.getValue();
        return BooleanUtils.isTrue(value);
    } else if (dateField != null) {
        Date date = dateField.getValue();
        return date;
    } else if (textField != null) {
        String strValue = textField.getValue();
        if (allowNull && StringUtils.isBlank(strValue)) {
            return null;
        } else {/*  w w w  .j a va 2 s  . c  o m*/
            if (strValue == null)
                strValue = "";
            return AttributeHelper.convert(type, strValue);
        }
    } else if (layout != null) {
        if (AttributeHelper.isList(type)) {
            return getValuesFromArrayLayout(layout);
        } else if (AttributeHelper.isArray(type)) {
            Class clazz = AttributeHelper.getArrayType(type);
            if (clazz != null) {
                List<String> strValues = getValuesFromArrayLayout(layout);
                Object array = Array.newInstance(clazz, strValues.size());
                for (int i = 0; i < strValues.size(); i++) {
                    Array.set(array, i, AttributeHelper.convert(clazz.getName(), strValues.get(i)));
                }
                return array;
            }
        }
    }
    return null;
}

From source file:org.ajax4jsf.util.SelectUtils.java

/**
 * Converts UISelectMany submitted value to converted value
 * //from   w w w .j  av a 2s .co  m
 * @author Manfred Geiler
 * @param facesContext
 * @param component
 * @param submittedValue
 * @return
 * @throws ConverterException
 */
public static Object getConvertedUISelectManyValue(FacesContext facesContext, UISelectMany component,
        String[] submittedValue) throws ConverterException {
    // Attention!
    // This code is duplicated in jsfapi component package.
    // If you change something here please do the same in the other class!

    if (submittedValue == null)
        throw new NullPointerException("submittedValue");

    ELContext elContext = facesContext.getELContext();
    ValueExpression vb = component.getValueExpression("value");
    Class<?> valueType = null;
    Class<?> arrayComponentType = null;
    if (vb != null) {
        valueType = vb.getType(elContext);
        if (valueType != null && valueType.isArray()) {
            arrayComponentType = valueType.getComponentType();
        }
    }

    Converter converter = component.getConverter();
    if (converter == null) {
        if (valueType == null) {
            // No converter, and no idea of expected type
            // --> return the submitted String array
            return submittedValue;
        }

        if (List.class.isAssignableFrom(valueType)) {
            // expected type is a List
            // --> according to javadoc of UISelectMany we assume that the
            // element type
            // is java.lang.String, and copy the String array to a new List
            List<String> lst = Arrays.asList(submittedValue);
            return lst;
        }

        if (arrayComponentType == null) {
            throw new IllegalArgumentException(Messages.getMessage(Messages.VALUE_BINDING_TYPE_ERROR));
        }

        if (String.class.equals(arrayComponentType))
            return submittedValue; // No conversion needed for String type
        if (Object.class.equals(arrayComponentType))
            return submittedValue; // No conversion for Object class

        try {
            converter = facesContext.getApplication().createConverter(arrayComponentType);
        } catch (FacesException e) {
            log.error(Messages.getMessage(Messages.NO_CONVERTER_FOUND_ERROR, arrayComponentType.getName()), e);
            return submittedValue;
        }
    }

    // Now, we have a converter...
    if (valueType == null) {
        // ...but have no idea of expected type
        // --> so let's convert it to an Object array
        int len = submittedValue.length;
        Object[] convertedValues = (Object[]) Array
                .newInstance(arrayComponentType == null ? Object.class : arrayComponentType, len);
        for (int i = 0; i < len; i++) {
            convertedValues[i] = converter.getAsObject(facesContext, component, submittedValue[i]);
        }
        return convertedValues;
    }

    if (List.class.isAssignableFrom(valueType)) {
        // Curious case: According to specs we should assume, that the
        // element type
        // of this List is java.lang.String. But there is a Converter set
        // for this
        // component. Because the user must know what he is doing, we will
        // convert the values.
        int len = submittedValue.length;
        List<Object> lst = new ArrayList<Object>(len);
        for (int i = 0; i < len; i++) {
            lst.add(converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return lst;
    }

    if (arrayComponentType == null) {
        throw new IllegalArgumentException(Messages.getMessage(Messages.VALUE_BINDING_TYPE_ERROR));
    }

    if (arrayComponentType.isPrimitive()) {
        // primitive array
        int len = submittedValue.length;
        Object convertedValues = Array.newInstance(arrayComponentType, len);
        for (int i = 0; i < len; i++) {
            Array.set(convertedValues, i, converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return convertedValues;
    } else {
        // Object array
        int len = submittedValue.length;
        ArrayList<Object> convertedValues = new ArrayList<Object>(len);
        for (int i = 0; i < len; i++) {
            convertedValues.add(i, converter.getAsObject(facesContext, component, submittedValue[i]));
        }
        return convertedValues.toArray((Object[]) Array.newInstance(arrayComponentType, len));
    }
}

From source file:org.yestech.jmlnitrate.transformation.inbound.BaseInboundTransformation.java

/**
 *  Retrieves the Method Array to execute from a method arguments arrays
 *  describing the Methods. <br>/*w  w  w .j  a va  2s.  c  om*/
 *  Method Array Format:
 *  <ol>
 *    <li> **Method Name - String <b>Mandatory</b>
 *    <li> **Static method - Boolean <b>Mandatory</b>
 *    <li> Method Argument Type - String Optional [0..*]
 *  </ol>
 *  <br>
 *  Method Argument Array Format:
 *  <ol>
 *    <li>
 *  </ol>
 *
 *
 * @param  method Array describing the method to create
 * @param  arguments Array of arguments needed for the Method
 * @return  an Object Array in the valid format for the {@link
 *      org.yestech.jmlnitrate.core.Kernel#createKernelProcess(String, String,
 *      Object[], Object[])}
 * @throws  Exception if error happens
 */
protected Object[] getMethod(Object[] method, Object[] arguments) throws Exception {
    Object[] executeMethod = new Object[4];
    if (method == null || method.length < 2 || (arguments != null && arguments.length < 1)) {
        logger.error("arguments must not be null....");
        throw new NullPointerException("arguments must not be null....");
    }
    String methodName = (String) method[0];

    Boolean staticMethod = Boolean.valueOf((String) method[1]);

    //check to see if method is empty or not.  short circuit if it is
    if (method.length == 2) {
        executeMethod[0] = methodName;
        executeMethod[1] = null;
        executeMethod[2] = null;
        executeMethod[3] = staticMethod;
    } else {
        //get the class
        int typeSize = method.length;
        Class[] methodTypes = new Class[typeSize - 2];
        for (int t = 2, mt = 0; t < typeSize; t++, mt++) {
            methodTypes[mt] = getClass((String) method[t]);
        }

        //get the Method Objects
        int argSize = arguments.length;
        Object[] methodArgs = new Object[argSize];
        boolean isArray = false;

        for (int i = 0; i < argSize; i++) {
            //check to make sure arguments holds an Object Array
            Object[] parsedArgValues = (Object[]) arguments[i];
            if (parsedArgValues.length == 0) {
                methodArgs[i] = null;
            } else {
                //find out if an array is needed?
                isArray = parsedArgValues[0].getClass().isArray();
                if (isArray) {
                    int asize = parsedArgValues.length;
                    Object array = Array.newInstance(methodTypes[i], asize);
                    methodTypes[i] = array.getClass();
                    for (int a = 0; a < asize; a++) {
                        Object value = null;
                        Object[] aparams = (Object[]) parsedArgValues[a];
                        int apsize = aparams.length;
                        //check to see if ctor needed
                        if (apsize > 1) {
                            //ctor needed
                            Object[] actorArgs = { aparams[0] };
                            Class[] actorTypes = { getClass((String) aparams[2]) };
                            Class actorClass = getClass((String) aparams[1]);
                            Constructor actor = Clazz.getConstructor(actorClass, actorTypes);

                            value = actor.newInstance(actorArgs);
                        } else {
                            //this is the value doesn't need a ctor
                            value = aparams[0];
                        }
                        Array.set(array, a, value);
                    }
                    methodArgs[i] = array;
                } else {
                    int psize = parsedArgValues.length;

                    //check to see if ctor needed
                    if (psize > 1) {
                        //ctor needed
                        Object[] ctorArgs = { parsedArgValues[0] };
                        Class[] ctorTypes = { getClass((String) parsedArgValues[2]) };
                        Class ctorClass = getClass((String) parsedArgValues[1]);
                        Constructor ctor = Clazz.getConstructor(ctorClass, ctorTypes);
                        methodArgs[i] = ctor.newInstance(ctorArgs);
                    } else {
                        //this is the value doesn't need a ctor
                        Object pvalue = parsedArgValues[0];
                        methodArgs[i] = pvalue;
                    }
                }
            }
        }

        executeMethod[0] = methodName;
        executeMethod[1] = methodTypes;
        executeMethod[2] = methodArgs;
        executeMethod[3] = staticMethod;
    }

    return executeMethod;
}

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

public static Object convertUISelectManyToObject(FacesContext context, UISelectMany component, Object value) {
    if (!(value instanceof String[]))
        throw new ClassCastException("Selected value must be a String[] type.");

    String[] submittedValue = (String[]) value;

    ValueBinding vb = component.getValueBinding("value");
    Class valueType = null;/*from w w w.j  a  va2  s  .com*/
    Class arrayComponentType = null;
    if (vb != null) {
        valueType = vb.getType(context);
        if (valueType != null && valueType.isArray()) {
            arrayComponentType = valueType.getComponentType();
        }
    }

    Converter converter = component.getConverter();
    if (converter == null) {
        if (valueType == null) {
            // No converter, and no idea of expected type
            // --> return the submitted String array
            return submittedValue;
        }

        if (List.class.isAssignableFrom(valueType)) {
            // expected type is a List
            // --> according to javadoc of UISelectMany we assume that the element type
            //     is java.lang.String, and copy the String array to a new List
            int len = submittedValue.length;
            List lst = new ArrayList(len);
            for (int i = 0; i < len; i++) {
                lst.add(submittedValue[i]);
            }
            return lst;
        }

        if (arrayComponentType == null) {
            throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array");
        }

        if (String.class.equals(arrayComponentType))
            return submittedValue; //No conversion needed for String type
        if (Object.class.equals(arrayComponentType))
            return submittedValue; //No conversion for Object class

        try {
            converter = context.getApplication().createConverter(arrayComponentType);
        } catch (FacesException e) {
            log.error("No Converter for type " + arrayComponentType.getName() + " found");
            return submittedValue;
        }
    }

    // Now, we have a converter...
    if (valueType == null) {
        // ...but have no idea of expected type
        // --> so let's convert it to an Object array
        int len = submittedValue.length;
        Object[] convertedValues = new Object[len];
        for (int i = 0; i < len; i++) {
            convertedValues[i] = converter.getAsObject(context, component, submittedValue[i]);
        }
        return convertedValues;
    }

    if (List.class.isAssignableFrom(valueType)) {
        // Curious case: According to specs we should assume, that the element type
        // of this List is java.lang.String. But there is a Converter set for this
        // component. Because the user must know what he is doing, we will convert the values.
        int len = submittedValue.length;
        List lst = new ArrayList(len);
        for (int i = 0; i < len; i++) {
            lst.add(converter.getAsObject(context, component, submittedValue[i]));
        }
        return lst;
    }

    if (arrayComponentType == null) {
        throw new IllegalArgumentException("ValueBinding for UISelectMany must be of type List or Array");
    }

    if (arrayComponentType.isPrimitive()) {
        //primitive array
        int len = submittedValue.length;
        Object convertedValues = Array.newInstance(arrayComponentType, len);
        for (int i = 0; i < len; i++) {
            Array.set(convertedValues, i, converter.getAsObject(context, component, submittedValue[i]));
        }
        return convertedValues;
    } else {
        //Object array
        int len = submittedValue.length;
        Object[] convertedValues = new Object[len];
        for (int i = 0; i < len; i++) {
            convertedValues[i] = converter.getAsObject(context, component, submittedValue[i]);
        }
        return convertedValues;
    }
}

From source file:org.apache.myfaces.el.PropertyResolverImpl.java

public void setValue(Object base, int index, Object newValue)
        throws EvaluationException, PropertyNotFoundException {
    try {/*from  w  w  w  . j a  va  2 s  .c  o m*/
        if (base == null) {
            throw new PropertyNotFoundException("Null bean, index: " + index);
        }

        try {
            if (base.getClass().isArray()) {
                Array.set(base, index, newValue);

                return;
            }
            if (base instanceof List) {
                // REVISIT: should we try to grow the list, if growable type
                //          (e.g., ArrayList, etc.), and if not large
                //          enough?
                ((List) base).set(index, newValue);

                return;
            }
        } catch (IndexOutOfBoundsException e) {
            throw new PropertyNotFoundException(
                    "Base with class : " + base.getClass().getName() + ", index " + index, e);
        }

        throw new EvaluationException("Bean must be array or List. Base with class: "
                + base.getClass().getName() + ", index " + index);
    } catch (PropertyNotFoundException e) {
        throw e;
    } catch (RuntimeException e) {
        throw new EvaluationException(
                "Exception setting value of index " + index + " of bean " + base.getClass().getName(), e);
    }
}

From source file:org.eclipse.e4.emf.internal.xpath.helper.ValueUtils.java

@SuppressWarnings("unchecked")
public static void setValue(Object collection, int index, Object value) {
    collection = getValue(collection);/*from  w w  w. j  ava2  s  .  co m*/
    if (collection != null) {
        if (collection.getClass().isArray()) {
            Array.set(collection, index, convert(value, collection.getClass().getComponentType()));
        } else if (collection instanceof List) {
            ((List<Object>) collection).set(index, value);
        } else if (collection instanceof Collection) {
            throw new UnsupportedOperationException(
                    "Cannot set value of an element of a " + collection.getClass().getName());
        }
    }
}

From source file:org.sc.probro.servlets.SkeletonServlet.java

private <T> T decode(String[] undecoded, Class<T> type) throws BrokerException {

    try {//w  ww.j av a2 s  .c o  m
        String[] decoded = new String[undecoded.length];
        for (int i = 0; i < undecoded.length; i++) {
            decoded[i] = URLDecoder.decode(undecoded[i], "UTF-8");
        }

        if (type.isArray()) {
            Class inner = type.getComponentType();
            int len = undecoded.length;
            Object arrayValue = Array.newInstance(inner, len);
            for (int i = 0; i < len; i++) {
                Array.set(arrayValue, i, decode(new String[] { undecoded[i] }, inner));
            }
            return (T) arrayValue;

        } else if (DBObject.isSubclass(type, String.class)) {
            return (T) decoded[0];

        } else if (DBObject.isSubclass(type, Integer.class)) {
            int parsed = Integer.parseInt(decoded[0]);
            return (T) (new Integer(parsed));

        } else if (DBObject.isSubclass(type, Double.class)) {
            int parsed = Integer.parseInt(decoded[0]);
            return (T) (new Integer(parsed));

        } else if (DBObject.isSubclass(type, Boolean.class)) {
            String lower = decoded[0].toLowerCase();
            if (!lower.equals("true") && !lower.equals("1")) {
                return (T) (Boolean.FALSE);
            } else {
                return (T) (Boolean.TRUE);
            }

        } else if (DBObject.isSubclass(type, JSONObject.class)) {
            return (T) (new JSONObject(decoded[0]));

        } else {
            throw new IllegalArgumentException(type.getCanonicalName());
        }

    } catch (UnsupportedEncodingException e) {
        throw new BrokerException(e);
    } catch (JSONException e) {
        throw new BrokerException(e);
    }

}

From source file:jp.terasoluna.fw.web.struts.form.ValidatorActionFormEx.java

/**
 * wCfbNXuv?peBl??B/*from w ww .  ja  v a 2 s . c  o  m*/
 *
 * @param name ??CfbNXtv?peB
 * @param index ??CfbNXu
 * @param value ?v?peBl
 */
@SuppressWarnings("unchecked")
public void setIndexedValue(String name, int index, Object value) {
    if (log.isDebugEnabled()) {
        log.debug("set(" + name + ", " + index + ", " + value + ") called.");
    }

    Object prop = null;
    try {
        prop = BeanUtil.getBeanProperty(this, name);
    } catch (PropertyAccessException e) {
        // DynaValidatorActionFormExdl??A
        // O????s?B
    }
    if (prop == null) {
        throw new NullPointerException("No indexed value for '" + name + "[" + index + "]'");
    } else if (prop.getClass().isArray()) {
        if (index < Array.getLength(prop)) {
            Array.set(prop, index, value);
        } else {
            // CfbNX`FbN
            ActionFormUtil.checkIndexLength(index);
            // ?VKz??
            Object newArray = Array.newInstance(prop.getClass().getComponentType(), index + 1);
            // zR|?[lgRs?[
            System.arraycopy(prop, 0, newArray, 0, Array.getLength(prop));
            // R|?[lgZbg
            Array.set(newArray, index, value);
            // Q?Rs?[
            prop = newArray;
        }
        try {
            BeanUtil.setBeanProperty(this, name, prop);
        } catch (PropertyAccessException e) {
            throw new IllegalArgumentException("Cannot set property for '" + name + "[" + index + "]'");
        }
    } else if (prop instanceof List) {
        if (index < ((List) prop).size()) {
            ((List) prop).set(index, value);
        } else {
            // CfbNX`FbN
            ActionFormUtil.checkIndexLength(index);
            Object[] oldValues = ((List) prop).toArray();
            Object[] newValues = (Object[]) Array.newInstance(oldValues.getClass().getComponentType(),
                    index + 1);
            System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
            newValues[index] = value;
            ((List) prop).clear();
            ((List) prop).addAll(Arrays.asList(newValues));
        }
        try {
            BeanUtil.setBeanProperty(this, name, prop);
        } catch (PropertyAccessException e) {
            throw new IllegalArgumentException("Cannot set property for '" + name + "[" + index + "]'");
        }
    } else {
        throw new IllegalArgumentException("Non-indexed property for '" + name + "[" + index + "]'");
    }

}

From source file:eu.sathra.io.IO.java

private Object getValue(JSONArray array, Class<?> clazz) throws Exception {

    Object parsedArray = Array.newInstance(clazz, array.length());

    for (int c = 0; c < array.length(); ++c) {
        if ((clazz.equals(String.class) || clazz.isPrimitive()) && !clazz.equals(float.class)) {
            Array.set(parsedArray, c, array.get(c));
        } else if (clazz.equals(float.class)) {
            Array.set(parsedArray, c, (float) array.getDouble(c));
        } else if (clazz.isArray()) {
            // nested array
            Array.set(parsedArray, c, getValue(array.getJSONArray(c), float.class)); // TODO
        } else {//from   www . ja  va2 s.  c om
            Array.set(parsedArray, c, load(array.getJSONObject(c), clazz));
        }
    }

    return parsedArray;
}

From source file:org.mypsycho.beans.DefaultInvoker.java

public void setIndexed(Object bean, int index, Object value) throws IllegalArgumentException {
    if (bean.getClass().isArray()) {
        // Modify the specified value in the array
        Array.set(bean, index, value);
    } else if (bean instanceof List) {
        // Modify the specified value in the List
        @SuppressWarnings("unchecked")
        List<Object> list = (List<Object>) bean;
        list.set(index, value);//from w  w w .j a v a 2 s .co  m
    } else {
        throw new IllegalArgumentException("Class '" + bean.getClass() + "' is not indexed");
    }
}