UnwarrantedOptimismException.java revision 1571:fd97b9047199
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25package jdk.nashorn.internal.runtime; 26 27import java.io.NotSerializableException; 28import java.io.ObjectInputStream; 29import java.io.ObjectOutputStream; 30import jdk.nashorn.internal.codegen.types.Type; 31 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") 40public 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 */ 134 public Type getReturnType() { 135 return returnType; 136 } 137 138 /** 139 * Does this exception refer to an invalid program point? This might be OK if 140 * we throw it, e.g. from a parameter guard 141 * @return true if invalid program point specified 142 */ 143 public boolean hasInvalidProgramPoint() { 144 return programPoint == INVALID_PROGRAM_POINT; 145 } 146 147 /** 148 * Get the program point 149 * @return the program point 150 */ 151 public int getProgramPoint() { 152 return programPoint; 153 } 154 155 /** 156 * Check if we ended up with a primitive return value (even though it may be 157 * too wide for what we tried to do, e.g. double instead of int) 158 * @return true if return value is primitive 159 */ 160 public boolean hasPrimitiveReturnValue() { 161 return returnValue instanceof Number || returnValue instanceof Boolean; 162 } 163 164 @Override 165 public String getMessage() { 166 return "UNWARRANTED OPTIMISM: [returnValue=" + 167 returnValue + 168 " (class=" + 169 (returnValue == null ? "null" : returnValue.getClass().getSimpleName()) + 170 (hasInvalidProgramPoint() ? 171 " <invalid program point>" : 172 (" @ program point #" + programPoint)) + 173 ")]"; 174 } 175 176 177 private void writeObject(final ObjectOutputStream out) throws NotSerializableException { 178 throw new NotSerializableException(getClass().getName()); 179 } 180 181 private void readObject(final ObjectInputStream in) throws NotSerializableException { 182 throw new NotSerializableException(getClass().getName()); 183 } 184} 185