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 */ 25 package jdk.nashorn.internal.runtime; 26 27 import java.io.NotSerializableException; 28 import java.io.ObjectInputStream; 29 import java.io.ObjectOutputStream; 30 import 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") 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 */ 117 public Type getReturnType() { 118 return returnType; 119 } 120 121 /** 122 * Does this exception refer to an invalid program point? This might be OK if 123 * we throw it, e.g. from a parameter guard 124 * @return true if invalid program point specified 125 */ 126 public boolean hasInvalidProgramPoint() { 127 return programPoint == INVALID_PROGRAM_POINT; 128 } 129 130 /** 131 * Get the program point 132 * @return the program point 133 */ 134 public int getProgramPoint() { 135 return programPoint; 136 } 137 138 /** 139 * Check if we ended up with a primitive return value (even though it may be 140 * too wide for what we tried to do, e.g. double instead of int) 141 * @return true if return value is primitive 142 */ 143 public boolean hasPrimitiveReturnValue() { 144 return returnValue instanceof Number || returnValue instanceof Boolean; 145 } 146 147 @Override 148 public String getMessage() { 149 return "UNWARRANTED OPTIMISM: [returnValue=" + 150 returnValue + 151 " (class=" + 152 (returnValue == null ? "null" : returnValue.getClass().getSimpleName()) + 153 (hasInvalidProgramPoint() ? 154 " <invalid program point>" : 155 (" @ program point #" + programPoint)) + 156 ")]"; 157 } 158 159 160 private void writeObject(final ObjectOutputStream out) throws NotSerializableException { 161 throw new NotSerializableException(getClass().getName()); 162 } 163 164 private void readObject(final ObjectInputStream in) throws NotSerializableException { 165 throw new NotSerializableException(getClass().getName()); 166 } 167 }