32 /** 33 * This exception is thrown from an optimistic operation, e.g. an integer add, 34 * that was to optimistic for what really took place. Typically things like 35 * trying to get an array element that we want to be an int, and it was a double, 36 * and an int add that actually overflows and needs a double for the representation 37 */ 38 39 @SuppressWarnings("serial") 40 public final class UnwarrantedOptimismException extends RuntimeException { 41 /** Denotes an invalid program point */ 42 public static final int INVALID_PROGRAM_POINT = -1; 43 44 /** The value for the first ordinary program point */ 45 public static final int FIRST_PROGRAM_POINT = 1; 46 47 private Object returnValue; 48 private final int programPoint; 49 private final Type returnType; 50 51 /** 52 * Constructor 53 * @param returnValue actual return value from the too narrow operation 54 * @param programPoint program point where unwarranted optimism was detected 55 */ 56 public UnwarrantedOptimismException(final Object returnValue, final int programPoint) { 57 this(returnValue, programPoint, getReturnType(returnValue)); 58 } 59 60 /** 61 * Check if a program point is valid 62 * @param programPoint the program point 63 * @return true if valid 64 */ 65 public static boolean isValid(final int programPoint) { 66 assert programPoint >= INVALID_PROGRAM_POINT; 67 return programPoint != INVALID_PROGRAM_POINT; 68 } 69 70 private static Type getReturnType(final Object v) { 71 if (v instanceof Double) { 72 return Type.NUMBER; 73 } else if (v instanceof Long) { 74 return Type.LONG; 75 } 76 assert !(v instanceof Integer) : v + " is an int"; // Can't have an unwarranted optimism exception with int 77 return Type.OBJECT; 78 } 79 80 /** 81 * Constructor with explicit return value type. 82 * @param returnValue actual return value from the too narrow operation 83 * @param programPoint program point where unwarranted optimism was detected 84 * @param returnType type of the returned value. Used to disambiguate the return type. E.g. an {@code ObjectArrayData} 85 * might return a {@link Double} for a particular element getter, but still throw this exception even if the call 86 * site can accept a double, since the array's type is actually {@code Type#OBJECT}. In this case, it must 87 * explicitly use this constructor to indicate its values are to be considered {@code Type#OBJECT} and not 88 * {@code Type#NUMBER}. 89 */ 90 public UnwarrantedOptimismException(final Object returnValue, final int programPoint, final Type returnType) { 91 super("", null, false, Context.DEBUG); 92 assert returnType != Type.OBJECT || returnValue == null || !Type.typeFor(returnValue.getClass()).isNumeric(); 93 assert returnType != Type.INT; 94 this.returnValue = returnValue; 95 this.programPoint = programPoint; 96 this.returnType = returnType; 97 } 98 99 /** 100 * Get the return value. This is a destructive readout, after the method is invoked the return value is null'd out. 101 * @return return value 102 */ 103 public Object getReturnValueDestructive() { 104 final Object retval = returnValue; 105 returnValue = null; 106 return retval; 107 } 108 109 Object getReturnValueNonDestructive() { 110 return returnValue; 111 } 112 113 /** 114 * Get the return type 115 * @return return type 116 */ | 32 /** 33 * This exception is thrown from an optimistic operation, e.g. an integer add, 34 * that was to optimistic for what really took place. Typically things like 35 * trying to get an array element that we want to be an int, and it was a double, 36 * and an int add that actually overflows and needs a double for the representation 37 */ 38 39 @SuppressWarnings("serial") 40 public final class UnwarrantedOptimismException extends RuntimeException { 41 /** Denotes an invalid program point */ 42 public static final int INVALID_PROGRAM_POINT = -1; 43 44 /** The value for the first ordinary program point */ 45 public static final int FIRST_PROGRAM_POINT = 1; 46 47 private Object returnValue; 48 private final int programPoint; 49 private final Type returnType; 50 51 /** 52 * Constructor without explicit return type. The return type is determined statically from the class of 53 * the return value, and only canonical internal number representations are recognized. Use 54 * {@link #createNarrowest} if you want to handle float and long values as numbers instead of objects. 55 * 56 * @param returnValue actual return value from the too narrow operation 57 * @param programPoint program point where unwarranted optimism was detected 58 */ 59 public UnwarrantedOptimismException(final Object returnValue, final int programPoint) { 60 this(returnValue, programPoint, getReturnType(returnValue)); 61 } 62 63 /** 64 * Check if a program point is valid. 65 * @param programPoint the program point 66 * @return true if valid 67 */ 68 public static boolean isValid(final int programPoint) { 69 assert programPoint >= INVALID_PROGRAM_POINT; 70 return programPoint != INVALID_PROGRAM_POINT; 71 } 72 73 private static Type getReturnType(final Object v) { 74 if (v instanceof Double) { 75 return Type.NUMBER; 76 } 77 assert !(v instanceof Integer) : v + " is an int"; // Can't have an unwarranted optimism exception with int 78 return Type.OBJECT; 79 } 80 81 /** 82 * Constructor with explicit return value type. 83 * @param returnValue actual return value from the too narrow operation 84 * @param programPoint program point where unwarranted optimism was detected 85 * @param returnType type of the returned value. Used to disambiguate the return type. E.g. an {@code ObjectArrayData} 86 * might return a {@link Double} for a particular element getter, but still throw this exception even if the call 87 * site can accept a double, since the array's type is actually {@code Type#OBJECT}. In this case, it must 88 * explicitly use this constructor to indicate its values are to be considered {@code Type#OBJECT} and not 89 * {@code Type#NUMBER}. 90 */ 91 public UnwarrantedOptimismException(final Object returnValue, final int programPoint, final Type returnType) { 92 super("", null, false, Context.DEBUG); 93 assert returnType != Type.OBJECT || returnValue == null || !Type.typeFor(returnValue.getClass()).isNumeric(); 94 assert returnType != Type.INT; 95 this.returnValue = returnValue; 96 this.programPoint = programPoint; 97 this.returnType = returnType; 98 } 99 100 /** 101 * Create an {@code UnwarrantedOptimismException} with the given return value and program point, narrowing 102 * the type to {@code number} if the value is a float or a long that can be represented as double. 103 * 104 * @param returnValue the return value 105 * @param programPoint the program point 106 * @return the exception 107 */ 108 public static UnwarrantedOptimismException createNarrowest(final Object returnValue, final int programPoint) { 109 if (returnValue instanceof Float 110 || (returnValue instanceof Long && JSType.isRepresentableAsDouble((Long) returnValue))) { 111 return new UnwarrantedOptimismException(((Number) returnValue).doubleValue(), programPoint, Type.NUMBER); 112 } 113 return new UnwarrantedOptimismException(returnValue, programPoint); 114 } 115 116 /** 117 * Get the return value. This is a destructive readout, after the method is invoked the return value is null'd out. 118 * @return return value 119 */ 120 public Object getReturnValueDestructive() { 121 final Object retval = returnValue; 122 returnValue = null; 123 return retval; 124 } 125 126 Object getReturnValueNonDestructive() { 127 return returnValue; 128 } 129 130 /** 131 * Get the return type 132 * @return return type 133 */ |