43972
|
1 |
/*
|
59095
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.debug;
|
|
26 |
|
|
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.Locale;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Indicates a condition that should never occur during normal operation.
|
|
32 |
*/
|
|
33 |
public class GraalError extends Error {
|
|
34 |
|
|
35 |
private static final long serialVersionUID = 531632331813456233L;
|
|
36 |
private final ArrayList<String> context = new ArrayList<>();
|
|
37 |
|
|
38 |
public static RuntimeException unimplemented() {
|
|
39 |
throw new GraalError("unimplemented");
|
|
40 |
}
|
|
41 |
|
|
42 |
public static RuntimeException unimplemented(String msg) {
|
|
43 |
throw new GraalError("unimplemented: %s", msg);
|
|
44 |
}
|
|
45 |
|
|
46 |
public static RuntimeException shouldNotReachHere() {
|
|
47 |
throw new GraalError("should not reach here");
|
|
48 |
}
|
|
49 |
|
|
50 |
public static RuntimeException shouldNotReachHere(String msg) {
|
|
51 |
throw new GraalError("should not reach here: %s", msg);
|
|
52 |
}
|
|
53 |
|
|
54 |
public static RuntimeException shouldNotReachHere(Throwable cause) {
|
|
55 |
throw new GraalError(cause);
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Checks a given condition and throws a {@link GraalError} if it is false. Guarantees are
|
|
60 |
* stronger than assertions in that they are always checked. Error messages for guarantee
|
|
61 |
* violations should clearly indicate the nature of the problem as well as a suggested solution
|
|
62 |
* if possible.
|
|
63 |
*
|
|
64 |
* @param condition the condition to check
|
46393
|
65 |
* @param msg the message that will be associated with the error
|
|
66 |
*/
|
|
67 |
public static void guarantee(boolean condition, String msg) {
|
|
68 |
if (!condition) {
|
|
69 |
throw new GraalError("failed guarantee: " + msg);
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Checks a given condition and throws a {@link GraalError} if it is false. Guarantees are
|
|
75 |
* stronger than assertions in that they are always checked. Error messages for guarantee
|
|
76 |
* violations should clearly indicate the nature of the problem as well as a suggested solution
|
|
77 |
* if possible.
|
|
78 |
*
|
|
79 |
* @param condition the condition to check
|
|
80 |
* @param msg the message that will be associated with the error, in
|
|
81 |
* {@link String#format(String, Object...)} syntax
|
|
82 |
* @param arg argument to the format string in {@code msg}
|
|
83 |
*/
|
|
84 |
public static void guarantee(boolean condition, String msg, Object arg) {
|
|
85 |
if (!condition) {
|
|
86 |
throw new GraalError("failed guarantee: " + msg, arg);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Checks a given condition and throws a {@link GraalError} if it is false. Guarantees are
|
|
92 |
* stronger than assertions in that they are always checked. Error messages for guarantee
|
|
93 |
* violations should clearly indicate the nature of the problem as well as a suggested solution
|
|
94 |
* if possible.
|
|
95 |
*
|
|
96 |
* @param condition the condition to check
|
43972
|
97 |
* @param msg the message that will be associated with the error, in
|
|
98 |
* {@link String#format(String, Object...)} syntax
|
46393
|
99 |
* @param arg1 argument to the format string in {@code msg}
|
|
100 |
* @param arg2 argument to the format string in {@code msg}
|
43972
|
101 |
*/
|
46393
|
102 |
public static void guarantee(boolean condition, String msg, Object arg1, Object arg2) {
|
|
103 |
if (!condition) {
|
|
104 |
throw new GraalError("failed guarantee: " + msg, arg1, arg2);
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Checks a given condition and throws a {@link GraalError} if it is false. Guarantees are
|
|
110 |
* stronger than assertions in that they are always checked. Error messages for guarantee
|
|
111 |
* violations should clearly indicate the nature of the problem as well as a suggested solution
|
|
112 |
* if possible.
|
|
113 |
*
|
|
114 |
* @param condition the condition to check
|
|
115 |
* @param msg the message that will be associated with the error, in
|
|
116 |
* {@link String#format(String, Object...)} syntax
|
|
117 |
* @param arg1 argument to the format string in {@code msg}
|
|
118 |
* @param arg2 argument to the format string in {@code msg}
|
|
119 |
* @param arg3 argument to the format string in {@code msg}
|
|
120 |
*/
|
|
121 |
public static void guarantee(boolean condition, String msg, Object arg1, Object arg2, Object arg3) {
|
|
122 |
if (!condition) {
|
|
123 |
throw new GraalError("failed guarantee: " + msg, arg1, arg2, arg3);
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* This override exists to catch cases when {@link #guarantee(boolean, String, Object)} is
|
|
129 |
* called with one argument bound to a varargs method parameter. It will bind to this method
|
|
130 |
* instead of the single arg variant and produce a deprecation warning instead of silently
|
|
131 |
* wrapping the Object[] inside of another Object[].
|
|
132 |
*/
|
|
133 |
@Deprecated
|
43972
|
134 |
public static void guarantee(boolean condition, String msg, Object... args) {
|
|
135 |
if (!condition) {
|
|
136 |
throw new GraalError("failed guarantee: " + msg, args);
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* This constructor creates a {@link GraalError} with a given message.
|
|
142 |
*
|
|
143 |
* @param msg the message that will be associated with the error
|
|
144 |
*/
|
|
145 |
public GraalError(String msg) {
|
|
146 |
super(msg);
|
|
147 |
}
|
|
148 |
|
|
149 |
/**
|
|
150 |
* This constructor creates a {@link GraalError} with a message assembled via
|
|
151 |
* {@link String#format(String, Object...)}. It always uses the ENGLISH locale in order to
|
|
152 |
* always generate the same output.
|
|
153 |
*
|
|
154 |
* @param msg the message that will be associated with the error, in String.format syntax
|
|
155 |
* @param args parameters to String.format - parameters that implement {@link Iterable} will be
|
|
156 |
* expanded into a [x, x, ...] representation.
|
|
157 |
*/
|
|
158 |
public GraalError(String msg, Object... args) {
|
|
159 |
super(format(msg, args));
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* This constructor creates a {@link GraalError} for a given causing Throwable instance.
|
|
164 |
*
|
|
165 |
* @param cause the original exception that contains additional information on this error
|
|
166 |
*/
|
|
167 |
public GraalError(Throwable cause) {
|
|
168 |
super(cause);
|
|
169 |
}
|
|
170 |
|
|
171 |
/**
|
59095
|
172 |
* This constructor creates a {@link GraalError} for a given causing Throwable instance with
|
|
173 |
* detailed error message.
|
|
174 |
*/
|
|
175 |
public GraalError(Throwable cause, String msg, Object... args) {
|
|
176 |
super(format(msg, args), cause);
|
|
177 |
}
|
|
178 |
|
|
179 |
/**
|
43972
|
180 |
* This constructor creates a {@link GraalError} and adds all the
|
|
181 |
* {@linkplain #addContext(String) context} of another {@link GraalError}.
|
|
182 |
*
|
|
183 |
* @param e the original {@link GraalError}
|
|
184 |
*/
|
|
185 |
public GraalError(GraalError e) {
|
|
186 |
super(e);
|
|
187 |
context.addAll(e.context);
|
|
188 |
}
|
|
189 |
|
|
190 |
@Override
|
|
191 |
public String toString() {
|
|
192 |
StringBuilder str = new StringBuilder();
|
|
193 |
str.append(super.toString());
|
47667
|
194 |
str.append(context());
|
|
195 |
return str.toString();
|
|
196 |
}
|
|
197 |
|
|
198 |
public String context() {
|
|
199 |
StringBuilder str = new StringBuilder();
|
43972
|
200 |
for (String s : context) {
|
|
201 |
str.append("\n\tat ").append(s);
|
|
202 |
}
|
|
203 |
return str.toString();
|
|
204 |
}
|
|
205 |
|
|
206 |
private static String format(String msg, Object... args) {
|
|
207 |
if (args != null) {
|
|
208 |
// expand Iterable parameters into a list representation
|
|
209 |
for (int i = 0; i < args.length; i++) {
|
|
210 |
if (args[i] instanceof Iterable<?>) {
|
|
211 |
ArrayList<Object> list = new ArrayList<>();
|
|
212 |
for (Object o : (Iterable<?>) args[i]) {
|
|
213 |
list.add(o);
|
|
214 |
}
|
|
215 |
args[i] = list.toString();
|
|
216 |
}
|
|
217 |
}
|
|
218 |
}
|
|
219 |
return String.format(Locale.ENGLISH, msg, args);
|
|
220 |
}
|
|
221 |
|
|
222 |
public GraalError addContext(String newContext) {
|
|
223 |
this.context.add(newContext);
|
|
224 |
return this;
|
|
225 |
}
|
|
226 |
|
|
227 |
public GraalError addContext(String name, Object obj) {
|
|
228 |
return addContext(format("%s: %s", name, obj));
|
|
229 |
}
|
|
230 |
}
|