< prev index next >
src/jdk/nashorn/internal/runtime/UnwarrantedOptimismException.java
Print this page
@@ -47,20 +47,23 @@
private Object returnValue;
private final int programPoint;
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
*/
public UnwarrantedOptimismException(final Object returnValue, final int programPoint) {
this(returnValue, programPoint, getReturnType(returnValue));
}
/**
- * Check if a program point is valid
+ * 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,12 +71,10 @@
}
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;
}
@@ -95,10 +96,26 @@
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 >