< prev index next >

src/jdk/nashorn/internal/runtime/UnwarrantedOptimismException.java

Print this page

        

*** 47,66 **** private Object returnValue; private final int programPoint; private final Type returnType; /** ! * Constructor * @param returnValue actual return value from the too narrow operation * @param programPoint program point where unwarranted optimism was detected */ public UnwarrantedOptimismException(final Object returnValue, final int programPoint) { this(returnValue, programPoint, getReturnType(returnValue)); } /** ! * Check if a program point is valid * @param programPoint the program point * @return true if valid */ public static boolean isValid(final int programPoint) { assert programPoint >= INVALID_PROGRAM_POINT; --- 47,69 ---- private Object returnValue; private final int programPoint; private final Type returnType; /** ! * Constructor without explicit return type. The return type is determined statically from the class of ! * the return value, and only canonical internal number representations are recognized. Use ! * {@link #createNarrowest} if you want to handle float and long values as numbers instead of objects. ! * * @param returnValue actual return value from the too narrow operation * @param programPoint program point where unwarranted optimism was detected */ public UnwarrantedOptimismException(final Object returnValue, final int programPoint) { this(returnValue, programPoint, getReturnType(returnValue)); } /** ! * Check if a program point is valid. * @param programPoint the program point * @return true if valid */ public static boolean isValid(final int programPoint) { assert programPoint >= INVALID_PROGRAM_POINT;
*** 68,79 **** } private static Type getReturnType(final Object v) { if (v instanceof Double) { return Type.NUMBER; - } else if (v instanceof Long) { - return Type.LONG; } assert !(v instanceof Integer) : v + " is an int"; // Can't have an unwarranted optimism exception with int return Type.OBJECT; } --- 71,80 ----
*** 95,104 **** --- 96,121 ---- this.programPoint = programPoint; this.returnType = returnType; } /** + * Create an {@code UnwarrantedOptimismException} with the given return value and program point, narrowing + * the type to {@code number} if the value is a float or a long that can be represented as double. + * + * @param returnValue the return value + * @param programPoint the program point + * @return the exception + */ + public static UnwarrantedOptimismException createNarrowest(final Object returnValue, final int programPoint) { + if (returnValue instanceof Float + || (returnValue instanceof Long && JSType.isRepresentableAsDouble((Long) returnValue))) { + return new UnwarrantedOptimismException(((Number) returnValue).doubleValue(), programPoint, Type.NUMBER); + } + return new UnwarrantedOptimismException(returnValue, programPoint); + } + + /** * Get the return value. This is a destructive readout, after the method is invoked the return value is null'd out. * @return return value */ public Object getReturnValueDestructive() { final Object retval = returnValue;
< prev index next >