01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: LightweightError.java 3844 2007-07-02 09:40:26Z gbevin $
07: */
08: package com.uwyn.rife.tools.exceptions;
09:
10: import java.lang.reflect.Method;
11:
12: /**
13: * An error that is intended to be as lightweight as possible.
14: * <p>
15: * Typically, this is used for {@link ControlFlowRuntimeException} exceptions so
16: * that as little overhead as possible is imposed when these exceptions are
17: * thrown. This is achieved by enforcing the stack traces to be empty, causing
18: * them to not be captured.
19: *
20: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
21: * @version $Revision: 3844 $
22: * @since 1.6
23: */
24: public class LightweightError extends Error {
25: private static final long serialVersionUID = -6077740593752636392L;
26:
27: private static boolean sUseFastExceptions = true;
28:
29: private Boolean mUseFastExceptions;
30:
31: public static synchronized void setUseFastExceptions(boolean flag) {
32: sUseFastExceptions = flag;
33: }
34:
35: public static boolean getUseFastExceptions() {
36: return sUseFastExceptions;
37: }
38:
39: private void init() {
40: if (mUseFastExceptions != null)
41: return;
42: try {
43: // detect the presence of RifeConfig and use that if possible, otherwise
44: // use the static flag of this class
45: Class global_class = Class
46: .forName("com.uwyn.rife.config.RifeConfig$Global");
47: Method get_use_fast_exceptions = global_class
48: .getDeclaredMethod("getUseFastExceptions",
49: new Class[0]);
50: mUseFastExceptions = ((Boolean) get_use_fast_exceptions
51: .invoke(null, new Object[0])).booleanValue();
52: } catch (Exception e) {
53: mUseFastExceptions = sUseFastExceptions;
54: }
55: }
56:
57: public LightweightError() {
58: super ();
59: init();
60: }
61:
62: public LightweightError(String message) {
63: super (message);
64: init();
65: }
66:
67: public LightweightError(String message, Throwable cause) {
68: super (message, cause);
69: init();
70: }
71:
72: public LightweightError(Throwable cause) {
73: super (cause);
74: init();
75: }
76:
77: public Throwable fillInStackTrace() {
78: init();
79: if (mUseFastExceptions) {
80: return null;
81: } else {
82: return super .fillInStackTrace();
83: }
84: }
85:
86: public StackTraceElement[] getStackTrace() {
87: init();
88: if (mUseFastExceptions) {
89: return new StackTraceElement[0];
90: } else {
91: return super.getStackTrace();
92: }
93: }
94: }
|