2015-03-13 18:52:59 +00:00
|
|
|
/* Copyright (c) 2008-2015, Avian Contributors
|
2008-02-19 18:06:52 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-06-21 01:37:43 +00:00
|
|
|
package java.lang;
|
|
|
|
|
2007-07-29 01:29:01 +00:00
|
|
|
import java.io.PrintStream;
|
|
|
|
import java.io.PrintWriter;
|
2009-06-02 23:14:38 +00:00
|
|
|
import java.io.Serializable;
|
2007-07-29 01:29:01 +00:00
|
|
|
|
2009-06-02 23:14:38 +00:00
|
|
|
public class Throwable implements Serializable {
|
2007-06-21 01:37:43 +00:00
|
|
|
private String message;
|
2007-07-04 22:27:08 +00:00
|
|
|
private Object trace;
|
2007-06-24 19:57:00 +00:00
|
|
|
private Throwable cause;
|
2007-06-30 01:37:45 +00:00
|
|
|
|
|
|
|
public Throwable(String message, Throwable cause) {
|
|
|
|
this.message = message;
|
2007-06-30 02:41:49 +00:00
|
|
|
this.trace = trace(1);
|
2007-06-30 01:37:45 +00:00
|
|
|
this.cause = cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Throwable(String message) {
|
|
|
|
this(message, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Throwable(Throwable cause) {
|
|
|
|
this(null, cause);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Throwable() {
|
|
|
|
this(null, null);
|
|
|
|
}
|
|
|
|
|
2007-07-21 20:44:39 +00:00
|
|
|
public Throwable getCause() {
|
|
|
|
return cause;
|
|
|
|
}
|
|
|
|
|
2008-01-03 17:08:33 +00:00
|
|
|
public Throwable initCause(Throwable e) {
|
2007-09-12 02:56:02 +00:00
|
|
|
if (cause == null) {
|
|
|
|
cause = e;
|
2008-01-03 17:08:33 +00:00
|
|
|
return this;
|
2007-09-12 02:56:02 +00:00
|
|
|
} else {
|
|
|
|
throw new IllegalStateException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-27 02:39:53 +00:00
|
|
|
public String getMessage() {
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2009-08-13 15:03:04 +00:00
|
|
|
public String getLocalizedMessage() {
|
|
|
|
return getMessage();
|
|
|
|
}
|
|
|
|
|
2007-08-14 13:27:10 +00:00
|
|
|
public String toString() {
|
2007-08-20 02:57:32 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append(getClass().getName());
|
|
|
|
if (message != null) {
|
|
|
|
sb.append(": ").append(message);
|
|
|
|
}
|
|
|
|
return sb.toString();
|
2007-08-14 13:27:10 +00:00
|
|
|
}
|
|
|
|
|
2007-07-04 22:27:08 +00:00
|
|
|
private static native Object trace(int skipCount);
|
|
|
|
|
2008-04-11 22:48:39 +00:00
|
|
|
static native StackTraceElement[] resolveTrace(Object trace);
|
2007-07-04 22:27:08 +00:00
|
|
|
|
|
|
|
private StackTraceElement[] resolveTrace() {
|
|
|
|
if (! (trace instanceof StackTraceElement[])) {
|
|
|
|
trace = resolveTrace(trace);
|
|
|
|
}
|
|
|
|
return (StackTraceElement[]) trace;
|
|
|
|
}
|
|
|
|
|
2007-09-26 20:46:21 +00:00
|
|
|
public StackTraceElement[] getStackTrace() {
|
|
|
|
return resolveTrace();
|
|
|
|
}
|
|
|
|
|
2009-08-13 15:03:04 +00:00
|
|
|
public void setStackTrace(StackTraceElement[] trace) {
|
|
|
|
this.trace = trace;
|
|
|
|
}
|
|
|
|
|
2007-07-29 01:29:01 +00:00
|
|
|
public void printStackTrace(PrintStream out) {
|
2007-07-04 22:27:08 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
printStackTrace(sb, System.getProperty("line.separator"));
|
2007-07-29 01:29:01 +00:00
|
|
|
out.print(sb.toString());
|
2007-08-10 23:45:47 +00:00
|
|
|
out.flush();
|
2007-07-29 01:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void printStackTrace(PrintWriter out) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
printStackTrace(sb, System.getProperty("line.separator"));
|
|
|
|
out.print(sb.toString());
|
2007-08-10 23:45:47 +00:00
|
|
|
out.flush();
|
2007-07-29 01:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void printStackTrace() {
|
|
|
|
printStackTrace(System.err);
|
2007-07-04 22:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void printStackTrace(StringBuilder sb, String nl) {
|
2007-08-20 02:57:32 +00:00
|
|
|
sb.append(toString()).append(nl);
|
2007-07-04 22:27:08 +00:00
|
|
|
|
|
|
|
StackTraceElement[] trace = resolveTrace();
|
|
|
|
for (int i = 0; i < trace.length; ++i) {
|
2008-04-21 22:31:50 +00:00
|
|
|
sb.append(" at ").append(trace[i].toString()).append(nl);
|
2007-07-04 22:27:08 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 15:46:38 +00:00
|
|
|
Throwable printCause = getCause();
|
|
|
|
if (printCause != null) {
|
2007-07-04 22:27:08 +00:00
|
|
|
sb.append("caused by: ");
|
2014-05-30 15:46:38 +00:00
|
|
|
printCause.printStackTrace(sb, nl);
|
2007-07-04 22:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-02 23:14:38 +00:00
|
|
|
|
|
|
|
public Throwable fillInStackTrace() {
|
|
|
|
trace = trace(0);
|
|
|
|
return this;
|
|
|
|
}
|
2013-11-04 21:07:43 +00:00
|
|
|
|
|
|
|
public void addSuppressed(Throwable exception) {
|
|
|
|
}
|
2007-06-21 01:37:43 +00:00
|
|
|
}
|