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     private final ArrayList<String> context = new ArrayList<>();
  35 
  36     public static RuntimeException unimplemented() {
  37         throw new JVMCIError("unimplemented");
  38     }
  39 
  40     public static RuntimeException unimplemented(String msg) {
  41         throw new JVMCIError("unimplemented: %s", msg);
  42     }
  43 
  44     public static RuntimeException shouldNotReachHere() {
  45         throw new JVMCIError("should not reach here");
  46     }
  47 
  48     public static RuntimeException shouldNotReachHere(String msg) {
  49         throw new JVMCIError("should not reach here: %s", msg);
  50     }
  51 
  52     public static RuntimeException shouldNotReachHere(Throwable cause) {
  53         throw new JVMCIError(cause);
  54     }
  55 
  56     /**
  57      * Checks a given condition and throws a {@link JVMCIError} if it is false. Guarantees are
  58      * stronger than assertions in that they are always checked. Error messages for guarantee
  59      * violations should clearly indicate the nature of the problem as well as a suggested solution
  60      * if possible.
  61      *
  62      * @param condition the condition to check
  63      * @param msg the message that will be associated with the error, in
  64      *            {@link String#format(String, Object...)} syntax
  65      * @param args arguments to the format string
  66      */
  67     public static void guarantee(boolean condition, String msg, Object... args) {
  68         if (!condition) {
  69             throw new JVMCIError("failed guarantee: " + msg, args);
  70         }
  71     }
  72 
  73     /**
  74      * This constructor creates a {@link JVMCIError} with a given message.
  75      *
  76      * @param msg the message that will be associated with the error
  77      */
  78     public JVMCIError(String msg) {
  79         super(msg);
  80     }
  81 
  82     /**
  83      * This constructor creates a {@link JVMCIError} with a message assembled via
  84      * {@link String#format(String, Object...)}. It always uses the ENGLISH locale in order to
  85      * always generate the same output.
  86      *
  87      * @param msg the message that will be associated with the error, in String.format syntax
  88      * @param args parameters to String.format - parameters that implement {@link Iterable} will be
  89      *            expanded into a [x, x, ...] representation.
  90      */
  91     public JVMCIError(String msg, Object... args) {
  92         super(format(msg, args));
  93     }
  94 
  95     /**
  96      * This constructor creates a {@link JVMCIError} for a given causing Throwable instance.
  97      *
  98      * @param cause the original exception that contains additional information on this error
  99      */
 100     public JVMCIError(Throwable cause) {
 101         super(cause);
 102     }
 103 
 104     /**
 105      * This constructor creates a {@link JVMCIError} and adds all the
 106      * {@linkplain #addContext(String) context} of another {@link JVMCIError}.
 107      *
 108      * @param e the original {@link JVMCIError}
 109      */
 110     public JVMCIError(JVMCIError e) {
 111         super(e);
 112         context.addAll(e.context);
 113     }
 114 
 115     @Override
 116     public String toString() {
 117         StringBuilder str = new StringBuilder();
 118         str.append(super.toString());
 119         for (String s : context) {
 120             str.append("\n\tat ").append(s);
 121         }
 122         return str.toString();
 123     }
 124 
 125     private static String format(String msg, Object... args) {
 126         if (args != null) {
 127             // expand Iterable parameters into a list representation
 128             for (int i = 0; i < args.length; i++) {
 129                 if (args[i] instanceof Iterable<?>) {
 130                     ArrayList<Object> list = new ArrayList<>();
 131                     for (Object o : (Iterable<?>) args[i]) {
 132                         list.add(o);
 133                     }
 134                     args[i] = list.toString();
 135                 }
 136             }
 137         }
 138         return String.format(Locale.ENGLISH, msg, args);
 139     }
 140 
 141     public JVMCIError addContext(String newContext) {
 142         this.context.add(newContext);
 143         return this;
 144     }
 145 
 146     public JVMCIError addContext(String name, Object obj) {
 147         return addContext(format("%s: %s", name, obj));
 148     }
 149 }