1 /* 2 * Copyright (c) 2011, 2014, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package jdk.vm.ci.common; 24 25 import java.util.ArrayList; 26 import java.util.Locale; 27 28 /** 29 * Indicates a condition in JVMCI related code that should never occur during normal operation. 30 */ 31 public class JVMCIError extends Error { 32 33 private static final long serialVersionUID = 531632331813456233L; 34 35 public static RuntimeException unimplemented() { 36 throw new JVMCIError("unimplemented"); 37 } 38 39 public static RuntimeException unimplemented(String msg) { 40 throw new JVMCIError("unimplemented: %s", msg); 41 } 42 43 public static RuntimeException shouldNotReachHere() { 44 throw new JVMCIError("should not reach here"); 45 } 46 47 public static RuntimeException shouldNotReachHere(String msg) { 48 throw new JVMCIError("should not reach here: %s", msg); 49 } 50 51 public static RuntimeException shouldNotReachHere(Throwable cause) { 52 throw new JVMCIError(cause); 53 } 54 55 /** 56 * Checks a given condition and throws a {@link JVMCIError} if it is false. Guarantees are 57 * stronger than assertions in that they are always checked. Error messages for guarantee 58 * violations should clearly indicate the nature of the problem as well as a suggested solution 59 * if possible. 60 * 61 * @param condition the condition to check 62 * @param msg the message that will be associated with the error, in 63 * {@link String#format(String, Object...)} syntax 64 * @param args arguments to the format string 65 */ 66 public static void guarantee(boolean condition, String msg, Object... args) { 67 if (!condition) { 68 throw new JVMCIError("failed guarantee: " + msg, args); 69 } 70 } 71 72 /** 73 * This constructor creates a {@link JVMCIError} with a given message. 74 * 75 * @param msg the message that will be associated with the error 76 */ 77 public JVMCIError(String msg) { 78 super(msg); 79 } 80 81 /** 82 * This constructor creates a {@link JVMCIError} with a message assembled via 83 * {@link String#format(String, Object...)}. It always uses the ENGLISH locale in order to 84 * always generate the same output. 85 * 86 * @param msg the message that will be associated with the error, in String.format syntax 87 * @param args parameters to String.format - parameters that implement {@link Iterable} will be 88 * expanded into a [x, x, ...] representation. 89 */ 90 public JVMCIError(String msg, Object... args) { 91 super(format(msg, args)); 92 } 93 94 /** 95 * This constructor creates a {@link JVMCIError} for a given causing Throwable instance. 96 * 97 * @param cause the original exception that contains additional information on this error 98 */ 99 public JVMCIError(Throwable cause) { 100 super(cause); 101 } 102 103 private static String format(String msg, Object... args) { 104 if (args != null) { 105 // expand Iterable parameters into a list representation 106 for (int i = 0; i < args.length; i++) { 107 if (args[i] instanceof Iterable<?>) { 108 ArrayList<Object> list = new ArrayList<>(); 109 for (Object o : (Iterable<?>) args[i]) { 110 list.add(o); 111 } 112 args[i] = list.toString(); 113 } 114 } 115 } 116 return String.format(Locale.ENGLISH, msg, args); 117 } 118 }