Mercurial > hg > truffle
comparison graal/com.oracle.jvmci.common/src/com/oracle/jvmci/common/JVMCIError.java @ 21543:93c50cefb9e8
moved GraalInternalError to com.oracle.jvmci.common and renamed it to JVMCIError (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Mon, 25 May 2015 23:30:34 +0200 |
parents | graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/GraalInternalError.java@7976223c77b5 |
children | f5b549811bac |
comparison
equal
deleted
inserted
replaced
21542:543957c1c6a6 | 21543:93c50cefb9e8 |
---|---|
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 com.oracle.jvmci.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 | |
57 * are 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 message assembled via | |
74 * {@link String#format(String, Object...)}. It always uses the ENGLISH locale in order to | |
75 * always generate the same output. | |
76 * | |
77 * @param msg the message that will be associated with the error, in String.format syntax | |
78 * @param args parameters to String.format - parameters that implement {@link Iterable} will be | |
79 * expanded into a [x, x, ...] representation. | |
80 */ | |
81 public JVMCIError(String msg, Object... args) { | |
82 super(format(msg, args)); | |
83 } | |
84 | |
85 /** | |
86 * This constructor creates a {@link JVMCIError} for a given causing Throwable instance. | |
87 * | |
88 * @param cause the original exception that contains additional information on this error | |
89 */ | |
90 public JVMCIError(Throwable cause) { | |
91 super(cause); | |
92 } | |
93 | |
94 /** | |
95 * This constructor creates a {@link JVMCIError} and adds all the | |
96 * {@linkplain #addContext(String) context} of another {@link JVMCIError}. | |
97 * | |
98 * @param e the original {@link JVMCIError} | |
99 */ | |
100 public JVMCIError(JVMCIError e) { | |
101 super(e); | |
102 context.addAll(e.context); | |
103 } | |
104 | |
105 @Override | |
106 public String toString() { | |
107 StringBuilder str = new StringBuilder(); | |
108 str.append(super.toString()); | |
109 for (String s : context) { | |
110 str.append("\n\tat ").append(s); | |
111 } | |
112 return str.toString(); | |
113 } | |
114 | |
115 private static String format(String msg, Object... args) { | |
116 if (args != null) { | |
117 // expand Iterable parameters into a list representation | |
118 for (int i = 0; i < args.length; i++) { | |
119 if (args[i] instanceof Iterable<?>) { | |
120 ArrayList<Object> list = new ArrayList<>(); | |
121 for (Object o : (Iterable<?>) args[i]) { | |
122 list.add(o); | |
123 } | |
124 args[i] = list.toString(); | |
125 } | |
126 } | |
127 } | |
128 return String.format(Locale.ENGLISH, msg, args); | |
129 } | |
130 | |
131 public JVMCIError addContext(String newContext) { | |
132 this.context.add(newContext); | |
133 return this; | |
134 } | |
135 | |
136 public JVMCIError addContext(String name, Object obj) { | |
137 return addContext(format("%s: %s", name, obj)); | |
138 } | |
139 } |