001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.lang;
017:
018: import java.io.PrintStream;
019:
020: /**
021: * See <a
022: * href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Throwable.html">the
023: * official Java API doc</a> for details.
024: */
025: public class Throwable {
026:
027: private Throwable cause;
028: private String message;
029: private StackTraceElement[] stackTrace;
030:
031: public Throwable() {
032: }
033:
034: public Throwable(String message) {
035: this .message = message;
036: }
037:
038: public Throwable(String message, Throwable cause) {
039: this .cause = cause;
040: this .message = message;
041: }
042:
043: public Throwable(Throwable cause) {
044: this .message = (cause == null) ? null : cause.toString();
045: this .cause = cause;
046: }
047:
048: /**
049: * Stack traces are not currently populated by GWT. This method does nothing.
050: *
051: * @return this
052: */
053: public Throwable fillInStackTrace() {
054: return this ;
055: }
056:
057: public Throwable getCause() {
058: return cause;
059: }
060:
061: public String getLocalizedMessage() {
062: return getMessage();
063: }
064:
065: public String getMessage() {
066: return message;
067: }
068:
069: /**
070: * Stack traces are not currently populated by GWT. This method will return a
071: * zero-length array unless a stack trace has been explicitly set with
072: * {@link #setStackTrace(StackTraceElement[])}
073: *
074: * @return the current stack trace
075: */
076: public StackTraceElement[] getStackTrace() {
077: if (stackTrace == null) {
078: return new StackTraceElement[0];
079: }
080: return stackTrace;
081: }
082:
083: public Throwable initCause(Throwable cause) {
084: if (this .cause != null) {
085: throw new IllegalStateException("Can't overwrite cause");
086: }
087: if (cause == this ) {
088: throw new IllegalArgumentException(
089: "Self-causation not permitted");
090: }
091: this .cause = cause;
092: return this ;
093: }
094:
095: public void printStackTrace() {
096: printStackTrace(System.err);
097: }
098:
099: public void printStackTrace(PrintStream out) {
100: StringBuffer msg = new StringBuffer();
101: Throwable currentCause = this ;
102: while (currentCause != null) {
103: String causeMessage = currentCause.getMessage();
104: if (currentCause != this ) {
105: msg.append("Caused by: ");
106: }
107: msg.append(currentCause.getClass().getName());
108: msg.append(": ");
109: msg.append(causeMessage == null ? "(No exception detail)"
110: : causeMessage);
111: msg.append("\n");
112: currentCause = currentCause.getCause();
113: }
114: out.println(msg);
115: }
116:
117: public void setStackTrace(StackTraceElement[] stackTrace) {
118: StackTraceElement[] copy = new StackTraceElement[stackTrace.length];
119: for (int i = 0, c = stackTrace.length; i < c; ++i) {
120: if (stackTrace[i] == null) {
121: throw new NullPointerException();
122: }
123: copy[i] = stackTrace[i];
124: }
125: this .stackTrace = copy;
126: }
127:
128: @Override
129: public String toString() {
130: String className = this .getClass().getName();
131: String msg = getMessage();
132: if (msg != null) {
133: return className + ": " + msg;
134: } else {
135: return className;
136: }
137: }
138:
139: }
|