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.*;
  26 
  27 /**
  28  * Indicates a condition in JVMCI related code that should never occur during normal operation.
  29  */
  30 public class JVMCIError extends Error {
  31 
  32     private static final long serialVersionUID = 531632331813456233L;
  33     private final ArrayList<String> context = new ArrayList<>();
  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     /**
 104      * This constructor creates a {@link JVMCIError} and adds all the
 105      * {@linkplain #addContext(String) context} of another {@link JVMCIError}.
 106      *
 107      * @param e the original {@link JVMCIError}
 108      */
 109     public JVMCIError(JVMCIError e) {
 110         super(e);
 111         context.addAll(e.context);
 112     }
 113 
 114     @Override
 115     public String toString() {
 116         StringBuilder str = new StringBuilder();
 117         str.append(super.toString());
 118         for (String s : context) {
 119             str.append("\n\tat ").append(s);
 120         }
 121         return str.toString();
 122     }
 123 
 124     private static String format(String msg, Object... args) {
 125         if (args != null) {
 126             // expand Iterable parameters into a list representation
 127             for (int i = 0; i < args.length; i++) {
 128                 if (args[i] instanceof Iterable<?>) {
 129                     ArrayList<Object> list = new ArrayList<>();
 130                     for (Object o : (Iterable<?>) args[i]) {
 131                         list.add(o);
 132                     }
 133                     args[i] = list.toString();
 134                 }
 135             }
 136         }
 137         return String.format(Locale.ENGLISH, msg, args);
 138     }
 139 
 140     public JVMCIError addContext(String newContext) {
 141         this.context.add(newContext);
 142         return this;
 143     }
 144 
 145     public JVMCIError addContext(String name, Object obj) {
 146         return addContext(format("%s: %s", name, obj));
 147     }
 148 }