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

/**
 * Get all child nodes of parent that implement/subclass the given type.
 * /*w w  w  .jav  a  2s  . com*/
 * @param parent
 *            Parent to search
 * @param nodeType
 *            Type of child to search for
 * @return Array of children of parent that conform to the given nodeType
 */
public static <N extends Node> N[] getChildrenImplementing(Node parent, Class<N> nodeType) {
    if (parent == null) {
        return null;
    }
    NodeList children = parent.getChildNodes();
    if (children == null) {
        return null;
    }
    int count = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (nodeType.isAssignableFrom(node.getClass())) {
            count++;
        }
    }
    @SuppressWarnings("unchecked")
    N[] array = (N[]) Array.newInstance(nodeType, count);
    for (int i = 0, pos = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (nodeType.isAssignableFrom(node.getClass())) {
            @SuppressWarnings("unchecked")
            N n = (N) node;
            Array.set(array, pos++, n);
        }
    }
    return array;
}

From source file:Main.java

/**
 * @return an Object array for the given object.
 *
 * @param obj  Object to convert to an array.  Converts primitive
 *             arrays to Object arrays consisting of their wrapper
 *             classes.  If the object is not an array (object or primitve)
 *             then a new array of the given type is created and the
 *             object is set as the sole element.
 *//*from www  . j  a  va  2 s  . co m*/
public static Object[] toArray(final Object obj) {
    // if the object is an array, the cast and return it.
    if (obj instanceof Object[]) {
        return (Object[]) obj;
    }

    // if the object is an array of primitives then wrap the array
    Class type = obj.getClass();
    Object array;
    if (type.isArray()) {
        int length = Array.getLength(obj);
        Class componentType = type.getComponentType();
        array = Array.newInstance(componentType, length);
        for (int i = 0; i < length; i++) {
            Array.set(array, i, Array.get(obj, i));
        }
    } else {
        array = Array.newInstance(type, 1);
        Array.set(array, 0, obj);
    }

    return (Object[]) array;
}

From source file:ArrayListWrapper.java

public Object set(int index, Object element) {
    Object old = get(index);
    Array.set(array, index, element);
    return old;
}

From source file:edu.scripps.fl.pubchem.app.util.GroupingIterator.java

public boolean hasNext() {
    array = Array.newInstance(clazz, groupSize);
    int ii = 0;/*from  w ww  .ja  v a  2 s  .c  o m*/
    for (ii = 0; ii < groupSize; ii++)
        if (iterator.hasNext()) {
            Object obj = iterator.next();
            obj = ConvertUtils.convert(obj, clazz);
            Array.set(array, ii, obj);
        } else
            break;
    if (ii < groupSize) {
        Object dest = Array.newInstance(clazz, ii);
        System.arraycopy(array, 0, dest, 0, ii);
        array = dest;
    }
    return ii > 0;
}

From source file:com.sinosoft.one.data.jade.rowmapper.ArrayRowMapper.java

public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
    int columnSize = rs.getMetaData().getColumnCount();
    Object array = Array.newInstance(componentType, columnSize);
    for (int i = 0; i < columnSize; i++) {
        Object value = JdbcUtils.getResultSetValue(rs, (i + 1), componentType);
        Array.set(array, i, value);
    }// w  w  w  .j ava2s .  co  m
    return array;
}

From source file:com.gzj.tulip.jade.rowmapper.ArrayRowMapper.java

@Override
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
    int columnSize = rs.getMetaData().getColumnCount();
    Object array = Array.newInstance(componentType, columnSize);
    for (int i = 0; i < columnSize; i++) {
        Object value = JdbcUtils.getResultSetValue(rs, (i + 1), componentType);
        Array.set(array, i, value);
    }//from  www  .  j a v a 2  s . c  om
    return array;
}

From source file:com.base.dao.sql.ReflectionUtils.java

public static Object convertValue(Object value, Class toType) {
    Object result = null;/*w w  w . j a  va2 s . c  o  m*/
    if (value != null) {
        if (value.getClass().isArray() && toType.isArray()) {
            Class componentType = toType.getComponentType();
            result = Array.newInstance(componentType, Array.getLength(value));
            for (int i = 0, icount = Array.getLength(value); i < icount; i++) {
                Array.set(result, i, convertValue(Array.get(value, i), componentType));
            }
        } else {
            if ((toType == Integer.class) || (toType == Integer.TYPE))
                result = Integer.valueOf((int) longValue(value));
            if ((toType == Double.class) || (toType == Double.TYPE))
                result = new Double(doubleValue(value));
            if ((toType == Boolean.class) || (toType == Boolean.TYPE))
                result = booleanValue(value) ? Boolean.TRUE : Boolean.FALSE;
            if ((toType == Byte.class) || (toType == Byte.TYPE))
                result = Byte.valueOf((byte) longValue(value));
            if ((toType == Character.class) || (toType == Character.TYPE))
                result = new Character((char) longValue(value));
            if ((toType == Short.class) || (toType == Short.TYPE))
                result = Short.valueOf((short) longValue(value));
            if ((toType == Long.class) || (toType == Long.TYPE))
                result = Long.valueOf(longValue(value));
            if ((toType == Float.class) || (toType == Float.TYPE))
                result = new Float(doubleValue(value));
            if (toType == BigInteger.class)
                result = bigIntValue(value);
            if (toType == BigDecimal.class)
                result = bigDecValue(value);
            if (toType == String.class)
                result = stringValue(value);
            if (toType == Date.class) {
                result = DateUtils.toDate(stringValue(value));
            }
            if (Enum.class.isAssignableFrom(toType))
                result = enumValue((Class<Enum>) toType, value);
        }
    } else {
        if (toType.isPrimitive()) {
            result = primitiveDefaults.get(toType);
        }
    }
    return result;
}

From source file:org.force66.beantester.valuegens.ArrayValueGenerator.java

@Override
public Object[] makeValues() {
    Object[] objArray = new Object[] { Array.newInstance(arrayType.getComponentType(), 1) };
    ValueGenerator<?> gen = valueGeneratorFactory.forClass(arrayType.getComponentType());
    Array.set(objArray[0], 0, gen.makeValues()[0]);
    return objArray;
}

From source file:org.eclipse.scanning.scisoftpy.python.PythonUtils.java

/**
 * Convert tuples/lists of tuples/lists to Java lists of lists. Also convert complex numbers to Apache Commons
 * version and Jython strings to strings
 * /*from   ww w  .j a va  2  s. com*/
 * @param obj
 * @return converted object
 */
public static Object convertToJava(Object obj) {
    if (obj == null || obj instanceof PyNone)
        return null;

    if (obj instanceof PySequenceList) {
        obj = ((PySequenceList) obj).toArray();
    }

    if (obj instanceof PyArray) {
        obj = ((PyArray) obj).getArray();
    }

    if (obj instanceof List<?>) {
        @SuppressWarnings("unchecked")
        List<Object> jl = (List<Object>) obj;
        int l = jl.size();
        for (int i = 0; i < l; i++) {
            Object lo = jl.get(i);
            if (lo instanceof PyObject) {
                jl.set(i, convertToJava(lo));
            }
        }
        return obj;
    }

    if (obj.getClass().isArray()) {
        int l = Array.getLength(obj);
        for (int i = 0; i < l; i++) {
            Object lo = Array.get(obj, i);
            if (lo instanceof PyObject) {
                Array.set(obj, i, convertToJava(lo));
            }
        }
        return obj;
    }

    if (obj instanceof BigInteger || !(obj instanceof PyObject))
        return obj;

    if (obj instanceof PyComplex) {
        PyComplex z = (PyComplex) obj;
        return new Complex(z.real, z.imag);
    } else if (obj instanceof PyString) {
        return obj.toString();
    }

    return ((PyObject) obj).__tojava__(Object.class);
}

From source file:com.microsoft.tfs.core.internal.wrappers.WrapperUtils.java

/**
 * <p>/*  www  . ja  v  a 2  s . c  o m*/
 * Takes an array of web service objects (for example, an array of
 * {@link _Item}) and returns an array of web service wrapper objects of the
 * given type (for instance, {@link Item}).
 * </p>
 * <p>
 * A constructor for the given wrapper type that accepts one of the given
 * service objects must exist.
 * </p>
 * <p>
 * <code>null</code> values in the web service objects are copied into the
 * returned array.
 * </p>
 *
 * @param wrapperType
 *        the wrapper object class name (not array class) to use (must not
 *        be <code>null</code>)
 * @param webServiceObjects
 *        the objects to wrap (if null, null is returned)
 * @return a new array of wrapper objects, where each wraps one of the given
 *         web service objects
 */
public static Object wrap(final Class wrapperType, final Object[] webServiceObjects) {
    Check.notNull(wrapperType, "wrapperType"); //$NON-NLS-1$

    if (webServiceObjects == null) {
        return null;
    }

    final Object ret = Array.newInstance(wrapperType, webServiceObjects.length);
    Class webServiceObjectType = null;
    Constructor constructor = null;

    if (webServiceObjects.length > 0) {
        try {

            for (int i = 0; i < webServiceObjects.length; i++) {
                if (constructor == null && webServiceObjects[i] != null) {
                    webServiceObjectType = webServiceObjects[i].getClass();
                    constructor = wrapperType.getConstructor(new Class[] { webServiceObjectType });
                }

                /*
                 * Persist null values.
                 */
                Array.set(ret, i,
                        (webServiceObjects[i] != null)
                                ? constructor.newInstance(new Object[] { webServiceObjects[i] })
                                : null);
            }

        } catch (final NoSuchMethodException e) {
            final String message = MessageFormat.format(
                    "Wrapper error: the desired wrapper class {0} does not have a visible constructor that accepts the web service type {1}", //$NON-NLS-1$
                    wrapperType, webServiceObjectType);

            log.error(message, e);
            throw new RuntimeException(message);
        } catch (final Exception e) {
            final String message = MessageFormat.format("Error wrapping {0} in {1}", //$NON-NLS-1$
                    webServiceObjectType, wrapperType);

            log.error(message, e);
            throw new RuntimeException(message, e);
        }
    }

    return ret;
}