Java Code Examples for java.lang.reflect.Array#setShort()

The following examples show how to use java.lang.reflect.Array#setShort() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: titan1withtp3.1   File: SerializerTest.java    License: Apache License 2.0 6 votes vote down vote up
private static Factory getArrayFactory(final Class ct, final Factory f) {
    return new Factory() {
        @Override
        public Object newInstance() {
            int length = random.nextInt(100);
            Object array = Array.newInstance(ct,length);
            for (int i = 0; i < length; i++) {
                if (ct==boolean.class) Array.setBoolean(array,i, (Boolean) f.newInstance());
                else if (ct==byte.class) Array.setByte(array,i, (Byte) f.newInstance());
                else if (ct==short.class) Array.setShort(array,i, (Short) f.newInstance());
                else if (ct==int.class) Array.setInt(array,i, (Integer) f.newInstance());
                else if (ct==long.class) Array.setLong(array,i, (Long) f.newInstance());
                else if (ct==float.class) Array.setFloat(array,i, (Float) f.newInstance());
                else if (ct==double.class) Array.setDouble(array,i, (Double) f.newInstance());
                else if (ct==char.class) Array.setChar(array,i, (Character) f.newInstance());
                else Array.set(array,i, f.newInstance());
            }
            return array;
        }
    };
}
 
Example 2
Source Project: rice   File: Formatter.java    License: Educational Community License v2.0 6 votes vote down vote up
/**
 * Sets the value of an element of an array of primitives at the supplied index.
 *
 * @param array An array.
 * @param type The component type of the array.
 * @param index An array index.
 */
public static void setArrayValue(Object array, Class type, Object value, int index) {
    if (!type.isPrimitive())
        Array.set(array, index, value);
    else if (type.isAssignableFrom(Boolean.TYPE))
        Array.setBoolean(array, index, (Boolean) value);
    else if (type.isAssignableFrom(Character.TYPE))
        Array.setChar(array, index, (Character) value);
    else if (type.isAssignableFrom(Byte.TYPE))
        Array.setByte(array, index, (Byte) value);
    else if (type.isAssignableFrom(Integer.TYPE))
        Array.setInt(array, index, (Integer) value);
    else if (type.isAssignableFrom(Short.TYPE))
        Array.setShort(array, index, (Short) value);
    else if (type.isAssignableFrom(Long.TYPE))
        Array.setLong(array, index, (Long) value);
    else if (type.isAssignableFrom(Float.TYPE))
        Array.setFloat(array, index, (Float) value);
    else if (type.isAssignableFrom(Double.TYPE))
        Array.setDouble(array, index, (Double) value);
}
 
Example 3
Source Project: Bytecoder   File: ArrayTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shortArrayTest() {
    final short[] array = new short[1];
    Array.setShort(array, 0, (short) 42);
    Assert.assertEquals((short) 42, Array.getShort(array, 0));
    Assert.assertEquals(1, Array.getLength(array));
}
 
Example 4
Source Project: groovy   File: MetaClassHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param list          the original list
 * @param parameterType the resulting array type
 * @return the constructed array
 */
public static Object asPrimitiveArray(List list, Class parameterType) {
    Class arrayType = parameterType.getComponentType();
    Object objArray = Array.newInstance(arrayType, list.size());
    for (int i = 0; i < list.size(); i++) {
        Object obj = list.get(i);
        if (arrayType.isPrimitive()) {
            if (obj instanceof Integer) {
                Array.setInt(objArray, i, (Integer) obj);
            } else if (obj instanceof Double) {
                Array.setDouble(objArray, i, (Double) obj);
            } else if (obj instanceof Boolean) {
                Array.setBoolean(objArray, i, (Boolean) obj);
            } else if (obj instanceof Long) {
                Array.setLong(objArray, i, (Long) obj);
            } else if (obj instanceof Float) {
                Array.setFloat(objArray, i, (Float) obj);
            } else if (obj instanceof Character) {
                Array.setChar(objArray, i, (Character) obj);
            } else if (obj instanceof Byte) {
                Array.setByte(objArray, i, (Byte) obj);
            } else if (obj instanceof Short) {
                Array.setShort(objArray, i, (Short) obj);
            }
        } else {
            Array.set(objArray, i, obj);
        }
    }
    return objArray;
}
 
Example 5
Source Project: brooklyn-server   File: Boxing.java    License: Apache License 2.0 5 votes vote down vote up
/** sets the given element in an array to the indicated value;
 * if the type is a primitive type, the appropriate primitive method is used
 * <p>
 * this is needed because arrays do not deal with autoboxing */
public static void setInArray(Object target, int index, Object value, Class<?> type) {
    if (PRIMITIVE_TO_BOXED.containsKey(type)) {
        if (type.equals(Integer.TYPE))
            Array.setInt(target, index, (Integer)value);
        else if (type.equals(Long.TYPE))
            Array.setLong(target, index, (Long)value);
        else if (type.equals(Double.TYPE))
            Array.setDouble(target, index, (Double)value);
        else if (type.equals(Float.TYPE))
            Array.setFloat(target, index, (Float)value);
        else if (type.equals(Boolean.TYPE))
            Array.setBoolean(target, index, (Boolean)value);
        else if (type.equals(Character.TYPE))
            Array.setChar(target, index, (Character)value);
        else if (type.equals(Byte.TYPE))
            Array.setByte(target, index, (Byte)value);
        else if (type.equals(Short.TYPE))
            Array.setShort(target, index, (Short)value);
        
        else if (type.equals(Void.TYPE))
            Array.set(target, index, value);
        
        else 
            // should not happen!
            throw new IllegalStateException("Unsupported primitive: "+type);
        
        return;
    }
    
    Array.set(target, index, value);
}
 
Example 6
/**
    * Set an element of an array to the appropriate dummy value type
    * @param array
    * @param i
    * @param expectedLeafType
    */
public static void setArrayMatchingDummyValue(Object array, int i, Class<?> expectedLeafType)
{
	if (boolean.class.equals(expectedLeafType)) {
		Array.setBoolean(array, i, false);
	}
	else if (byte.class.equals(expectedLeafType)) {
		Array.setByte(array, i, DUMMY_BYTE);
	}
	else if (char.class.equals(expectedLeafType)) {
		Array.setChar(array, i, DUMMY_CHAR);
	}
	else if (double.class.equals(expectedLeafType)) {
		Array.setDouble(array, i, DUMMY_DOUBLE);
	}
	else if (float.class.equals(expectedLeafType)) {
		Array.setFloat(array, i, DUMMY_FLOAT);
	}
	else if (int.class.equals(expectedLeafType)) {
		Array.setInt(array, i, DUMMY_INTEGER);
	}
	else if (long.class.equals(expectedLeafType)) {
		Array.setLong(array, i, DUMMY_LONG);
	}
	else if (short.class.equals(expectedLeafType)) {
		Array.setShort(array, i, DUMMY_SHORT);
	}
	else {
		Array.set(array, i, null);
	}
}
 
Example 7
Source Project: sofa-acts   File: BaseConstructor.java    License: Apache License 2.0 4 votes vote down vote up
protected Object constructArrayStep2(SequenceNode node, Object array) {
    final Class<?> componentType = node.getType().getComponentType();

    int index = 0;
    for (Node child : node.getValue()) {
        // Handle multi-dimensional arrays...
        if (child.getType() == Object.class) {
            child.setType(componentType);
        }

        final Object value = constructObject(child);

        if (componentType.isPrimitive()) {
            // Null values are disallowed for primitives
            if (value == null) {
                throw new NullPointerException("Unable to construct element value for " + child);
            }

            // Primitive arrays require quite a lot of work.
            if (byte.class.equals(componentType)) {
                Array.setByte(array, index, ((Number) value).byteValue());

            } else if (short.class.equals(componentType)) {
                Array.setShort(array, index, ((Number) value).shortValue());

            } else if (int.class.equals(componentType)) {
                Array.setInt(array, index, ((Number) value).intValue());

            } else if (long.class.equals(componentType)) {
                Array.setLong(array, index, ((Number) value).longValue());

            } else if (float.class.equals(componentType)) {
                Array.setFloat(array, index, ((Number) value).floatValue());

            } else if (double.class.equals(componentType)) {
                Array.setDouble(array, index, ((Number) value).doubleValue());

            } else if (char.class.equals(componentType)) {
                Array.setChar(array, index, ((Character) value).charValue());

            } else if (boolean.class.equals(componentType)) {
                Array.setBoolean(array, index, ((Boolean) value).booleanValue());

            } else {
                throw new YAMLException("unexpected primitive type");
            }

        } else {
            // Non-primitive arrays can simply be assigned:
            Array.set(array, index, value);
        }

        ++index;
    }
    return array;
}
 
Example 8
Source Project: onedev   File: BaseConstructor.java    License: MIT License 4 votes vote down vote up
protected Object constructArrayStep2(SequenceNode node, Object array) {
    final Class<?> componentType = node.getType().getComponentType();

    int index = 0;
    for (Node child : node.getValue()) {
        // Handle multi-dimensional arrays...
        if (child.getType() == Object.class) {
            child.setType(componentType);
        }

        final Object value = constructObject(child);

        if (componentType.isPrimitive()) {
            // Null values are disallowed for primitives
            if (value == null) {
                throw new NullPointerException(
                        "Unable to construct element value for " + child);
            }

            // Primitive arrays require quite a lot of work.
            if (byte.class.equals(componentType)) {
                Array.setByte(array, index, ((Number) value).byteValue());

            } else if (short.class.equals(componentType)) {
                Array.setShort(array, index, ((Number) value).shortValue());

            } else if (int.class.equals(componentType)) {
                Array.setInt(array, index, ((Number) value).intValue());

            } else if (long.class.equals(componentType)) {
                Array.setLong(array, index, ((Number) value).longValue());

            } else if (float.class.equals(componentType)) {
                Array.setFloat(array, index, ((Number) value).floatValue());

            } else if (double.class.equals(componentType)) {
                Array.setDouble(array, index, ((Number) value).doubleValue());

            } else if (char.class.equals(componentType)) {
                Array.setChar(array, index, ((Character) value).charValue());

            } else if (boolean.class.equals(componentType)) {
                Array.setBoolean(array, index, ((Boolean) value).booleanValue());

            } else {
                throw new YAMLException("unexpected primitive type");
            }

        } else {
            // Non-primitive arrays can simply be assigned:
            Array.set(array, index, value);
        }

        ++index;
    }
    return array;
}
 
Example 9
protected Object constructArrayStep2(SequenceNode node, Object array) {
    final Class<?> componentType = node.getType().getComponentType();

    int index = 0;
    for (Node child : node.getValue()) {
        // Handle multi-dimensional arrays...
        if ( child.getType() == Object.class ) {
            child.setType(componentType);
        }
        
        final Object value = constructObject(child);

        if (componentType.isPrimitive()) {
            // Null values are disallowed for primitives
            if ( value == null ) {
                throw new NullPointerException ( "Unable to construct element value for " + child );
            }
            
            // Primitive arrays require quite a lot of work.
            if (byte.class.equals(componentType)) {
                Array.setByte(array, index, ((Number) value).byteValue());

            } else if (short.class.equals(componentType)) {
                Array.setShort(array, index, ((Number) value).shortValue());

            } else if (int.class.equals(componentType)) {
                Array.setInt(array, index, ((Number) value).intValue());

            } else if (long.class.equals(componentType)) {
                Array.setLong(array, index, ((Number) value).longValue());

            } else if (float.class.equals(componentType)) {
                Array.setFloat(array, index, ((Number) value).floatValue());

            } else if (double.class.equals(componentType)) {
                Array.setDouble(array, index, ((Number) value).doubleValue());

            } else if (char.class.equals(componentType)) {
                Array.setChar(array, index, ((Character) value).charValue());

            } else if (boolean.class.equals(componentType)) {
                Array.setBoolean(array, index, ((Boolean) value).booleanValue());

            } else {
                throw new YAMLException("unexpected primitive type");
            }

        } else {
            // Non-primitive arrays can simply be assigned:
            Array.set(array, index, value);
        }

        ++index;
    }
    return array;
}
 
Example 10
public static short test(int i, short value) {
    Array.setShort(array, i, value);
    return array[i];
}
 
Example 11
Source Project: snake-yaml   File: BaseConstructor.java    License: Apache License 2.0 4 votes vote down vote up
protected Object constructArrayStep2(SequenceNode node, Object array) {
    final Class<?> componentType = node.getType().getComponentType();

    int index = 0;
    for (Node child : node.getValue()) {
        // Handle multi-dimensional arrays...
        if (child.getType() == Object.class) {
            child.setType(componentType);
        }

        final Object value = constructObject(child);

        if (componentType.isPrimitive()) {
            // Null values are disallowed for primitives
            if (value == null) {
                throw new NullPointerException("Unable to construct element value for " + child);
            }

            // Primitive arrays require quite a lot of work.
            if (byte.class.equals(componentType)) {
                Array.setByte(array, index, ((Number) value).byteValue());

            } else if (short.class.equals(componentType)) {
                Array.setShort(array, index, ((Number) value).shortValue());

            } else if (int.class.equals(componentType)) {
                Array.setInt(array, index, ((Number) value).intValue());

            } else if (long.class.equals(componentType)) {
                Array.setLong(array, index, ((Number) value).longValue());

            } else if (float.class.equals(componentType)) {
                Array.setFloat(array, index, ((Number) value).floatValue());

            } else if (double.class.equals(componentType)) {
                Array.setDouble(array, index, ((Number) value).doubleValue());

            } else if (char.class.equals(componentType)) {
                Array.setChar(array, index, ((Character) value).charValue());

            } else if (boolean.class.equals(componentType)) {
                Array.setBoolean(array, index, ((Boolean) value).booleanValue());

            } else {
                throw new YAMLException("unexpected primitive type");
            }

        } else {
            // Non-primitive arrays can simply be assigned:
            Array.set(array, index, value);
        }

        ++index;
    }
    return array;
}
 
Example 12
@Override
protected void setArray(Object array, int pos, Object value) {
    Array.setShort(array, pos, (Short) value);
}
 
Example 13
protected Object constructArrayStep2(SequenceNode node, Object array) {
    final Class<?> componentType = node.getType().getComponentType();

    int index = 0;
    for (Node child : node.getValue()) {
        // Handle multi-dimensional arrays...
        if (child.getType() == Object.class) {
            child.setType(componentType);
        }

        final Object value = constructObject(child);

        if (componentType.isPrimitive()) {
            // Null values are disallowed for primitives
            if (value == null) {
                throw new NullPointerException("Unable to construct element value for " + child);
            }

            // Primitive arrays require quite a lot of work.
            if (byte.class.equals(componentType)) {
                Array.setByte(array, index, ((Number) value).byteValue());

            } else if (short.class.equals(componentType)) {
                Array.setShort(array, index, ((Number) value).shortValue());

            } else if (int.class.equals(componentType)) {
                Array.setInt(array, index, ((Number) value).intValue());

            } else if (long.class.equals(componentType)) {
                Array.setLong(array, index, ((Number) value).longValue());

            } else if (float.class.equals(componentType)) {
                Array.setFloat(array, index, ((Number) value).floatValue());

            } else if (double.class.equals(componentType)) {
                Array.setDouble(array, index, ((Number) value).doubleValue());

            } else if (char.class.equals(componentType)) {
                Array.setChar(array, index, ((Character) value).charValue());

            } else if (boolean.class.equals(componentType)) {
                Array.setBoolean(array, index, ((Boolean) value).booleanValue());

            } else {
                throw new YAMLException("unexpected primitive type");
            }

        } else {
            // Non-primitive arrays can simply be assigned:
            Array.set(array, index, value);
        }

        ++index;
    }
    return array;
}
 
Example 14
@Override
protected void setArray(Object array, int pos, Object value) {
    Array.setShort(array,pos,(Short)value);
}
 
Example 15
Source Project: beanshell   File: BshArray.java    License: Apache License 2.0 4 votes vote down vote up
/** Copy and cast the elements of from arrays to type in to array.
 * Recursively traverse dimensions to populate the elements at to array.
 * @param toType the element type to cast to
 * @param to the destination array
 * @param from the list of origin arrays */
private static void copy(Class<?> toType, Object to, Object... from) {
    int f = 0, fi = 0,
        length = Array.getLength(from[0]),
        total = from.length > 1 ? Array.getLength(to) : length;
    if ( Types.arrayDimensions(to.getClass()) == 1 ) {
        for ( int i = 0; i < total; i++ ) {
            Object value = Array.get(from[f], fi++);
            try {
                value = Primitive.unwrap(
                        Types.castObject(value, toType, Types.CAST));
            } catch (UtilEvalError e) { /* ignore cast errors */ }
            if ( Byte.TYPE == toType )
                Array.setByte(to, i, (byte) value);
            else if ( Short.TYPE == toType )
                Array.setShort(to, i, (short) value);
            else if ( Integer.TYPE == toType )
                Array.setInt(to, i, (int) value);
            else if ( Long.TYPE == toType )
                Array.setLong(to, i, (long) value);
            else if ( Float.TYPE == toType )
                Array.setFloat(to, i, (float) value);
            else if ( Double.TYPE == toType )
                Array.setDouble(to, i, (double) value);
            else if ( Character.TYPE == toType )
                Array.setChar(to, i, (char) value);
            else if ( Boolean.TYPE == toType )
                Array.setBoolean(to, i, (boolean) value);
            else
                Array.set(to, i, value);

            // concatenate multiple from arrays
            if ( length < total && fi == length && f+1 < from.length ) {
                length = Array.getLength(from[++f]);
                fi = 0;
            }
        }
    } else for ( int i = 0; i < total; i++ ) {
        // concatenate multiple from arrays
        if ( length < total && fi == length && f+1 < from.length ) {
            length = Array.getLength(from[++f]);
            fi = 0;
        }

        Object frm = Array.get(from[f], fi++);

        // null dimension example: new Integer[2][]
        if ( null == frm ) {
            Array.set(to, i, null);
            continue;
        }

        Object tto = Array.get(to, i);

        // mixed array lengths in multiple dimensions ex: {{1,2}, {3}}
        if ( Array.getLength(frm) != Array.getLength(tto) )
            Array.set(to, i,
                tto = Array.newInstance(toType, dimensions(frm)));

        // recurse copy for next array dimension
        copy(toType, tto, frm);
    }
}