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 }
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 }
|
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 }
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 }
|