Cast a primitive value represented by its java.lang wrapper type to the specified java.lang wrapper type. e.g. Byte(5) to Integer(5) or Integer(5) to Byte(5) @param toType is the java TYPE type @param value is the value in java.lang wrapper. value may not be null.
( Class toType, Object value )
| 1099 | value may not be null. |
| 1100 | */ |
| 1101 | static Object castWrapper( |
| 1102 | Class toType, Object value ) |
| 1103 | { |
| 1104 | if ( !toType.isPrimitive() ) |
| 1105 | throw new InterpreterError("invalid type in castWrapper: "+toType); |
| 1106 | if ( value == null ) |
| 1107 | throw new InterpreterError("null value in castWrapper, guard"); |
| 1108 | if ( value instanceof Boolean ) |
| 1109 | { |
| 1110 | if ( toType != Boolean.TYPE ) |
| 1111 | throw new InterpreterError("bad wrapper cast of boolean"); |
| 1112 | else |
| 1113 | return value; |
| 1114 | } |
| 1115 | |
| 1116 | // first promote char to Number type to avoid duplicating code |
| 1117 | if ( value instanceof Character ) |
| 1118 | value = new Integer(((Character)value).charValue()); |
| 1119 | |
| 1120 | if ( !(value instanceof Number) ) |
| 1121 | throw new InterpreterError("bad type in cast"); |
| 1122 | |
| 1123 | Number number = (Number)value; |
| 1124 | |
| 1125 | if (toType == Byte.TYPE) |
| 1126 | return new Byte(number.byteValue()); |
| 1127 | if (toType == Short.TYPE) |
| 1128 | return new Short(number.shortValue()); |
| 1129 | if (toType == Character.TYPE) |
| 1130 | return new Character((char)number.intValue()); |
| 1131 | if (toType == Integer.TYPE) |
| 1132 | return new Integer(number.intValue()); |
| 1133 | if (toType == Long.TYPE) |
| 1134 | return new Long(number.longValue()); |
| 1135 | if (toType == Float.TYPE) |
| 1136 | return new Float(number.floatValue()); |
| 1137 | if (toType == Double.TYPE) |
| 1138 | return new Double(number.doubleValue()); |
| 1139 | |
| 1140 | throw new InterpreterError("error in wrapper cast"); |
| 1141 | } |
| 1142 | |
| 1143 | } |
no test coverage detected