@ SuppressWarnings ( "unchecked" ) public static void setValue ( Object object , Field field , Object value ) throws IllegalArgumentException , IllegalAccessException { if ( value == null ) { //if (Number.class.isAssignableFrom(field.getType())) { //value = java.math.BigDecimal.ZERO; //} else return; return ; } try { field . setAccessible ( true ) ; field . set ( object , value ) ; } catch ( IllegalArgumentException x ) { if ( value instanceof Number ) { // size of "value" bigger than that of "field"? try { Number number = ( Number ) value ; Class < ? > ftype = field . getType ( ) ; if ( Enum . class . isAssignableFrom ( ftype ) ) { field . set ( object , ftype . getEnumConstants ( ) [ number . intValue ( ) ] ) ; } else if ( BigDecimal . class . isAssignableFrom ( ftype ) ) { field . set ( object , BigDecimal . valueOf ( number . doubleValue ( ) ) ) ; } else if ( BigInteger . class . isAssignableFrom ( ftype ) ) { field . set ( object , BigInteger . valueOf ( number . longValue ( ) ) ) ; } else if ( Double . TYPE == ftype || Double . class . isAssignableFrom ( ftype ) ) { field . set ( object , number . doubleValue ( ) ) ; } else if ( Float . TYPE == ftype || Float . class . isAssignableFrom ( ftype ) ) { field . set ( object , number . floatValue ( ) ) ; } else if ( Long . TYPE == ftype || Long . class . isAssignableFrom ( ftype ) ) { field . set ( object , number . longValue ( ) ) ; } else if ( Integer . TYPE == ftype || Integer . class . isAssignableFrom ( ftype ) ) { field . set ( object , number . intValue ( ) ) ; } else if ( Short . TYPE == ftype || Short . class . isAssignableFrom ( ftype ) ) { field . set ( object , number . shortValue ( ) ) ; } else { field . set ( object , number . byteValue ( ) ) ; } } catch ( Throwable t ) { throw new IllegalArgumentException ( t ) ; } } else if ( value instanceof java . sql . Timestamp ) { try { field . set ( object , new java . sql . Date ( ( ( java . sql . Timestamp ) value ) . getTime ( ) ) ) ; } catch ( Throwable t ) { throw new IllegalArgumentException ( t ) ; } } else if ( value instanceof String ) { field . set ( object , new ValueOf ( field . getType ( ) , field . getAnnotation ( typeinfo . class ) ) . invoke ( ( String ) value ) ) ; } else { throw x ; } } }
Assigns a value to the field in an object converting value type as necessary .