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

The following examples show how to use java.lang.reflect.Field#setShort() . 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 check out the related API usage on the sidebar.
Example 1
Source File: ReflectUtils.java    From zkdoctor with Apache License 2.0 6 votes vote down vote up
/**
 * 设置指定类的field值
 *
 * @param clazz 类
 * @param field field
 * @param value 值
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
public static void setField(Class<?> clazz, Field field, Object value)
        throws IllegalArgumentException, IllegalAccessException {
    Class<?> fieldType = field.getType();
    if (int.class.equals(fieldType)) {
        field.setInt(clazz, Integer.parseInt(String.valueOf(value)));
    } else if (boolean.class.equals(fieldType)) {
        field.setBoolean(clazz, Boolean.parseBoolean(String.valueOf(value)));
    } else if (byte.class.equals(fieldType)) {
        field.setByte(clazz, Byte.parseByte(String.valueOf(value)));
    } else if (double.class.equals(fieldType)) {
        field.setDouble(clazz, Double.parseDouble(String.valueOf(value)));
    } else if (float.class.equals(fieldType)) {
        field.setFloat(clazz, Float.parseFloat(String.valueOf(value)));
    } else if (long.class.equals(fieldType)) {
        field.setLong(clazz, Long.parseLong(String.valueOf(value)));
    } else if (short.class.equals(fieldType)) {
        field.setShort(clazz, Short.parseShort(String.valueOf(value)));
    } else if (char.class.equals(fieldType) && value instanceof Character) {
        field.setChar(clazz, (Character) value);
    } else {
        field.set(clazz, value);
    }
}
 
Example 2
Source File: FieldUtil.java    From android-lite-orm with Apache License 2.0 6 votes vote down vote up
public static void setNumber(Object o, Field field, long n) throws IllegalAccessException {
    field.setAccessible(true);
    Class claxx = field.getType();
    if (claxx == long.class) {
        field.setLong(o, n);
    } else if (claxx == int.class) {
        field.setInt(o, (int) n);
    } else if (claxx == short.class) {
        field.setShort(o, (short) n);
    } else if (claxx == byte.class) {
        field.setByte(o, (byte) n);
    } else if (claxx == Long.class) {
        field.set(o, new Long(n));
    } else if (claxx == Integer.class) {
        field.set(o, new Integer((int) n));
    } else if (claxx == Short.class) {
        field.set(o, new Short((short) n));
    } else if (claxx == Byte.class) {
        field.set(o, new Byte((byte) n));
    } else {
        throw new RuntimeException("field is not a number class");
    }
}
 
Example 3
Source File: TestInstanceCloneUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void setVal(Field f, int i) {
    try {
        if (f.getType() == int.class) {
            f.setInt(this, i);
            return;
        } else if (f.getType() == short.class) {
            f.setShort(this, (short)i);
            return;
        } else if (f.getType() == byte.class) {
            f.setByte(this, (byte)i);
            return;
        } else if (f.getType() == long.class) {
            f.setLong(this, i);
            return;
        }
    } catch(IllegalAccessException iae) {
        throw new RuntimeException("Getting fields failed");
    }
    throw new RuntimeException("unexpected field type");
}
 
Example 4
Source File: Equals.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 5
Source File: PGUtils.java    From ParcelableGenerator with MIT License 5 votes vote down vote up
private static void readValue(Parcel source, Field field, Object target) {
    try {
        if (!checkSerializable(field)) {
            return;
        }
        field.setAccessible(true);
        if (field.getType().equals(int.class)) {
            field.setInt(target, source.readInt());
        } else if (field.getType().equals(double.class)) {
            field.setDouble(target, source.readDouble());
        } else if (field.getType().equals(float.class)) {
            field.setFloat(target, source.readFloat());
        } else if (field.getType().equals(long.class)) {
            field.setLong(target, source.readLong());
        } else if (field.getType().equals(boolean.class)) {
            field.setBoolean(target, source.readInt() != 0);
        } else if (field.getType().equals(char.class)) {
            field.setChar(target, (char) source.readInt());
        } else if (field.getType().equals(byte.class)) {
            field.setByte(target, source.readByte());
        } else if (field.getType().equals(short.class)) {
            field.setShort(target, (short) source.readInt());
        } else {
            field.set(target,
                    source.readValue(target.getClass().getClassLoader()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: Equals.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 7
Source File: Equals.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 8
Source File: ConfigCommandUtils.java    From Valkyrien-Skies with Apache License 2.0 5 votes vote down vote up
/**
 * Sets a field from a string. It only supports fields where {@link
 * ConfigCommandUtils#isSupportedType(Class)} for {@link Field#getType()}
 *
 * @param string The string to set the field's value to
 * @param field  The field to set
 * @param object The object upon which to set the field
 */
public static void setFieldFromString(String string, Field field, @Nullable Object object) {
    if (!isSupportedType(field.getType())) {
        throw new IllegalArgumentException("Unsupported field type");
    }
    try {
        if (field.getType() == int.class) {
            field.setInt(object, Integer.parseInt(string));
        } else if (field.getType() == double.class) {
            field.setDouble(object, Double.parseDouble(string));
        } else if (field.getType() == float.class) {
            field.setFloat(object, Float.parseFloat(string));
        } else if (field.getType() == boolean.class) {
            field.setBoolean(object, Boolean.parseBoolean(string));
        } else if (field.getType() == byte.class) {
            field.setByte(object, Byte.parseByte(string));
        } else if (field.getType() == long.class) {
            field.setLong(object, Long.parseLong(string));
        } else if (field.getType() == short.class) {
            field.setShort(object, Short.parseShort(string));
        } else if (field.getType() == char.class) {
            field.setChar(object, string.charAt(0));
        } else if (field.getType() == String.class) {
            field.set(object, string);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example 9
Source File: Equals.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 10
Source File: Equals.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 11
Source File: Equals.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 12
Source File: Equals.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 13
Source File: VirtSet.java    From radon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handle(VM vm, Object[] operands) throws Exception {
    String ownerName = (String) operands[0];
    String name = (String) operands[1];
    String typeName = (String) operands[2];

    Class clazz = VM.getClazz(ownerName);
    Class type = VM.getClazz(typeName);
    Field field = VM.getField(clazz, name, type);

    if (field == null)
        throw new VMException();

    JWrapper value = vm.pop();

    if (value instanceof JTop)
        value = vm.pop();

    Object ref = vm.pop().asObj();

    if ("int".equals(ownerName))
        field.setInt(ref, value.asInt());
    else if ("long".equals(ownerName))
        field.setLong(ref, value.asLong());
    else if ("float".equals(ownerName))
        field.setFloat(ref, value.asFloat());
    else if ("double".equals(ownerName))
        field.setDouble(ref, value.asDouble());
    else if ("byte".equals(ownerName))
        field.setByte(ref, value.asByte());
    else if ("short".equals(ownerName))
        field.setShort(ref, value.asShort());
    else if ("char".equals(ownerName))
        field.setChar(ref, value.asChar());
    else if ("boolean".equals(ownerName))
        field.setBoolean(ref, value.asBool());
    else
        field.set(ref, value.asObj());
}
 
Example 14
Source File: StaticSet.java    From radon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handle(VM vm, Object[] operands) throws Exception {
    String ownerName = (String) operands[0];
    String name = (String) operands[1];
    String typeName = (String) operands[2];

    Class clazz = VM.getClazz(ownerName);
    Class type = VM.getClazz(typeName);
    Field field = VM.getField(clazz, name, type);

    if (field == null)
        throw new VMException();

    JWrapper value = vm.pop();

    if (value instanceof JTop)
        value = vm.pop();

    if ("int".equals(ownerName))
        field.setInt(null, value.asInt());
    else if ("long".equals(ownerName))
        field.setLong(null, value.asLong());
    else if ("float".equals(ownerName))
        field.setFloat(null, value.asFloat());
    else if ("double".equals(ownerName))
        field.setDouble(null, value.asDouble());
    else if ("byte".equals(ownerName))
        field.setByte(null, value.asByte());
    else if ("short".equals(ownerName))
        field.setShort(null, value.asShort());
    else if ("char".equals(ownerName))
        field.setChar(null, value.asChar());
    else if ("boolean".equals(ownerName))
        field.setBoolean(null, value.asBool());
    else
        field.set(null, value.asObj());
}
 
Example 15
Source File: Equals.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 16
Source File: Equals.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns an InterfaceAddress instance with its fields set the the values
 * specificed.
 */
static InterfaceAddress createInterfaceAddress(
            InetAddress address, InetAddress broadcast, short prefixlength) {
    try {
        Class<InterfaceAddress> IAClass = InterfaceAddress.class;
        InterfaceAddress ia;
        Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
        ctr.setAccessible(true);

        Field addressField = IAClass.getDeclaredField("address");
        addressField.setAccessible(true);

        Field broadcastField = IAClass.getDeclaredField("broadcast");
        broadcastField.setAccessible(true);

        Field maskLengthField = IAClass.getDeclaredField("maskLength");
        maskLengthField.setAccessible(true);

        ia = ctr.newInstance();
        addressField.set(ia, address);
        broadcastField.set(ia, broadcast);
        maskLengthField.setShort(ia, prefixlength);

        return ia;
    } catch (NoSuchFieldException nsfe) {
        nsfe.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException ie) {
        ie.printStackTrace();
    } catch (IllegalAccessException iae) {
        iae.printStackTrace();
    } catch (InvocationTargetException ite) {
        ite.printStackTrace();
    }

    return null;
}
 
Example 17
Source File: GlobalKeyListener.java    From tools-ocr with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void preventEvent(NativeKeyEvent e){
    try {
        Field f = NativeInputEvent.class.getDeclaredField("reserved");
        f.setAccessible(true);
        f.setShort(e, (short) 0x01);
    }
    catch (Exception ex) {
        StaticLog.error(ex);
    }
}
 
Example 18
Source File: ExcelReader.java    From azeroth with Apache License 2.0 4 votes vote down vote up
private void getCellValue(Cell cell, Object o, Field field) throws IllegalAccessException, ParseException {
    LOG.debug("cell:{}, field:{}, type:{}", cell.getCellTypeEnum(), field.getName(), field.getType().getName());
    switch (cell.getCellTypeEnum()) {
        case BLANK:
            break;
        case BOOLEAN:
            field.setBoolean(o, cell.getBooleanCellValue());
            break;
        case ERROR:
            field.setByte(o, cell.getErrorCellValue());
            break;
        case FORMULA:
            field.set(o, cell.getCellFormula());
            break;
        case NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                if (field.getType().getName().equals(Date.class.getName())) {
                    field.set(o, cell.getDateCellValue());
                } else {
                    field.set(o, format.format(cell.getDateCellValue()));
                }
            } else {
                if (field.getType().isAssignableFrom(Integer.class) || field.getType().getName().equals("int")) {
                    field.setInt(o, (int) cell.getNumericCellValue());
                } else if (field.getType().isAssignableFrom(Short.class) || field.getType().getName().equals("short")) {
                    field.setShort(o, (short) cell.getNumericCellValue());
                } else if (field.getType().isAssignableFrom(Float.class) || field.getType().getName().equals("float")) {
                    field.setFloat(o, (float) cell.getNumericCellValue());
                } else if (field.getType().isAssignableFrom(Byte.class) || field.getType().getName().equals("byte")) {
                    field.setByte(o, (byte) cell.getNumericCellValue());
                } else if (field.getType().isAssignableFrom(Double.class) || field.getType().getName().equals("double")) {
                    field.setDouble(o, cell.getNumericCellValue());
                } else if (field.getType().isAssignableFrom(String.class)) {
                    String s = String.valueOf(cell.getNumericCellValue());
                    if (s.contains("E")) {
                        s = s.trim();
                        BigDecimal bigDecimal = new BigDecimal(s);
                        s = bigDecimal.toPlainString();
                    }
                    //防止整数判定为浮点数
                    if (s.endsWith(".0")) { s = s.substring(0, s.indexOf(".0")); }
                    field.set(o, s);
                } else {
                    field.set(o, cell.getNumericCellValue());
                }
            }
            break;
        case STRING:
            if (field.getType().getName().equals(Date.class.getName())) {
                field.set(o, format.parse(cell.getRichStringCellValue().getString()));
            } else {
                field.set(o, cell.getRichStringCellValue().getString());
            }
            break;
        default:
            field.set(o, cell.getStringCellValue());
            break;
    }
}
 
Example 19
Source File: FieldTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
void setField(char primitiveType, Object o, Field f,
        Class expected, Object value) {
    try {
        primitiveType = Character.toUpperCase(primitiveType);
        switch (primitiveType) {
        case 'I': // int
            f.setInt(o, ((Integer) value).intValue());
            break;
        case 'J': // long
            f.setLong(o, ((Long) value).longValue());
            break;
        case 'Z': // boolean
            f.setBoolean(o, ((Boolean) value).booleanValue());
            break;
        case 'S': // short
            f.setShort(o, ((Short) value).shortValue());
            break;
        case 'B': // byte
            f.setByte(o, ((Byte) value).byteValue());
            break;
        case 'C': // char
            f.setChar(o, ((Character) value).charValue());
            break;
        case 'D': // double
            f.setDouble(o, ((Double) value).doubleValue());
            break;
        case 'F': // float
            f.setFloat(o, ((Float) value).floatValue());
            break;
        default:
            f.set(o, value);
        }
        // Since 2011, members are always accessible and throwing is optional
        assertTrue("expected " + expected + " for " + f.getName() + " = " + value,
                expected == null || expected == IllegalAccessException.class);
    } catch (Exception e) {
        if (expected == null) {
            e.printStackTrace();
            fail("unexpected exception " + e + " for field "
                    + f.getName() + ", value " + value);
        } else {
            assertTrue("expected exception "
                    + expected.getName() + " and got " + e
                    + " for field " + f.getName() + ", value " + value,
                    e.getClass().equals(expected));
        }
    }
}
 
Example 20
Source File: SqlHelper.java    From SqliteLookup with Apache License 2.0 4 votes vote down vote up
/**
 * use reflection to parse queryResult's value into model
 * @param queryResult
 * @param model
 */
public static void parseResultSetToModel(ResultSet queryResult,
		Object model) {
	Class<?> clazz = model.getClass();
	Field[] fields = clazz.getDeclaredFields();

	Object fieldVal = null;
	Class<?> fieldType = null;
	try {
		for (Field field : fields) {
			if (field.isAccessible() == false)
				field.setAccessible(true);
			Column column = field.getAnnotation(Column.class);
			if (column == null)
				continue;
			String columnName = column.name();
			fieldVal = queryResult.getValue(columnName);
			fieldType = field.getType();
			if (fieldVal != null) {
				if (fieldType.equals(fieldVal.getClass())) {
					field.set(model, fieldVal);
				} else if (fieldType.equals(short.class)) {
					field.setShort(model,queryResult.getShortValue(columnName));
				} else if (fieldType.equals(Short.class)) {
					field.set(model, (Short) queryResult.getShortValue(columnName));
				} else if (fieldType.equals(int.class)) {
					field.setInt(model,queryResult.getIntValue(columnName));
				} else if (fieldType.equals(Integer.class)) {
					field.set(model, (Integer) queryResult.getIntValue(columnName));
				} else if (fieldType.equals(long.class)) {
					field.setLong(model,
							queryResult.getLongValue(columnName));
				} else if (fieldType.equals(Long.class)) {
					field.set(model, (Long) queryResult
							.getLongValue(columnName));
				} else if (fieldType.equals(float.class)) {
					field.setFloat(model,
							queryResult.getFloatValue(columnName));
				} else if (fieldType.equals(Float.class)) {
					field.set(model, (Float) queryResult
							.getFloatValue(columnName));
				} else if (fieldType.equals(double.class)) {
					field.setDouble(model,
							queryResult.getDoubleValue(columnName));
				} else if (fieldType.equals(Double.class)) {
					field.set(model, (Double) queryResult
							.getDoubleValue(columnName));
				} else if (fieldType.equals(boolean.class)) {
					field.setBoolean(model,
							queryResult.getBooleanValue(columnName));
				} else if (fieldType.equals(Boolean.class)) {
					field.set(model, (Boolean) queryResult
							.getBooleanValue(columnName));
				} else if (fieldType.equals(String.class)) {
					field.set(model,queryResult.getStringValue(columnName));
				} else if(fieldType.equals(Date.class)){
					field.set(model, queryResult.getDateValue(columnName));
				}
			}
		}
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	}
}