( Class toType, Class fromType, Primitive fromValue, boolean checkOnly, int operation )
| 997 | fromValue must be non-null (Primitive.NULL is of course valid). |
| 998 | */ |
| 999 | static Primitive castPrimitive( |
| 1000 | Class toType, Class fromType, Primitive fromValue, |
| 1001 | boolean checkOnly, int operation ) |
| 1002 | throws UtilEvalError |
| 1003 | { |
| 1004 | /* |
| 1005 | Lots of preconditions checked here... |
| 1006 | Once things are running smoothly we might comment these out |
| 1007 | (That's what assertions are for). |
| 1008 | */ |
| 1009 | if ( checkOnly && fromValue != null ) |
| 1010 | throw new InterpreterError("bad cast param 1"); |
| 1011 | if ( !checkOnly && fromValue == null ) |
| 1012 | throw new InterpreterError("bad cast param 2"); |
| 1013 | if ( fromType != null && !fromType.isPrimitive() ) |
| 1014 | throw new InterpreterError("bad fromType:" +fromType); |
| 1015 | if ( fromValue == Primitive.NULL && fromType != null ) |
| 1016 | throw new InterpreterError("inconsistent args 1"); |
| 1017 | if ( fromValue == Primitive.VOID && fromType != Void.TYPE ) |
| 1018 | throw new InterpreterError("inconsistent args 2"); |
| 1019 | |
| 1020 | // can't cast void to anything |
| 1021 | if ( fromType == Void.TYPE ) |
| 1022 | if ( checkOnly ) |
| 1023 | return Types.INVALID_CAST; |
| 1024 | else |
| 1025 | throw Types.castError( Reflect.normalizeClassName(toType), |
| 1026 | "void value", operation ); |
| 1027 | |
| 1028 | // unwrap Primitive fromValue to its wrapper value, etc. |
| 1029 | Object value = null; |
| 1030 | if ( fromValue != null ) |
| 1031 | value = fromValue.getValue(); |
| 1032 | |
| 1033 | if ( toType.isPrimitive() ) |
| 1034 | { |
| 1035 | // Trying to cast null to primitive type? |
| 1036 | if ( fromType == null ) |
| 1037 | if ( checkOnly ) |
| 1038 | return Types.INVALID_CAST; |
| 1039 | else |
| 1040 | throw Types.castError( |
| 1041 | "primitive type:" + toType, "Null value", operation ); |
| 1042 | |
| 1043 | // fall through |
| 1044 | } else |
| 1045 | { |
| 1046 | // Trying to cast primitive to an object type |
| 1047 | // Primitive.NULL can be cast to any object type |
| 1048 | if ( fromType == null ) |
| 1049 | return checkOnly ? Types.VALID_CAST : |
| 1050 | Primitive.NULL; |
| 1051 | |
| 1052 | if ( checkOnly ) |
| 1053 | return Types.INVALID_CAST; |
| 1054 | else |
| 1055 | throw Types.castError( |
| 1056 | "object type:" + toType, "primitive value", operation); |
no test coverage detected