--- old/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/UnwarrantedOptimismException.java 2015-12-10 13:51:24.548614808 +0100 +++ new/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/UnwarrantedOptimismException.java 2015-12-10 13:51:24.376614809 +0100 @@ -49,7 +49,10 @@ private final Type returnType; /** - * Constructor + * 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 */ @@ -58,7 +61,7 @@ } /** - * Check if a program point is valid + * Check if a program point is valid. * @param programPoint the program point * @return true if valid */ @@ -70,8 +73,6 @@ 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; @@ -97,6 +98,22 @@ } /** + * 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 */