001 
002 /*
003  *  JScripter Emulation 1.0 - To Script Java
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.jscripter.emu.java.lang;
021 
022 import js.*;
023 
024 /**
025  * <p>The <b>Internal</b> superclass of all errors and exceptions, emulating a
026  * standard <tt>java.lang</tt> interface or class with the same simple name as
027  * this one.</p>
028  * <p>This interface or class is only used internally by JS re-compiler implementations.</p>
029  * <p>Please refer to <a href="http://java.sun.com/docs/">the Java API Standards</a> for detail description of the original class or interface.</p>
030  * 
031  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
032  * 
033  * @javascript Re-compilers must redirect the resolution of the emulated original Java class or interface to this one.
034  */
035 public class Throwable
036 {
037     private Throwable cause;
038     private java.lang.String message;
039     private ArrayLike<ArrayLike<java.lang.String>> trace;
040 
041     /**
042      * <p>Internally constructs a new throwable with <tt>null</tt> as its detail message.</p>
043      * @since 1.0
044      * @javascript Re-compilers must report error on end-users directly using this constructor.
045      */
046     public Throwable() {}
047 
048     /**
049      * <p>Internally constructs a new throwable with detail message.</p>
050      * @param message The detail message.
051      * @since 1.0
052      * @javascript Re-compilers must report error on end-users directly using this constructor.
053      */
054     public Throwable(java.lang.String message) {
055         this.message = message;
056     }
057 
058     /**
059      * <p>Internally constructs a new throwable with detail message and cause.</p>
060      * @param message The detail message.
061      * @param cause The cause.
062      * @since 1.0
063      * @javascript Re-compilers must report error on end-users directly using this constructor.
064      */
065     public Throwable(java.lang.String message, Throwable cause) {
066         this.cause = cause;
067         this.message = message;
068     }
069 
070     /**
071      * <p>Internally constructs a new throwable with detail cause.</p>
072      * @param cause The cause.
073      * @since 1.0
074      * @javascript Re-compilers must report error on end-users directly using this constructor.
075      */
076     public Throwable(Throwable cause) {
077         this.message = (cause == null) ? null : cause.toString();
078         this.cause = cause;
079     }
080 
081     /**
082      * <p>Returns the cause of this throwable or <tt>null</tt> if the cause is nonexistent or unknown.</p>
083      * <p>The cause is a throwable that caused this throwable to get thrown.</p>
084      * @return The cause of this throwable or <tt>null</tt> if the cause is nonexistent or unknown.
085      * @since 1.0
086      * @javascript Re-compilers must report error on end-users directly using this method.
087      */
088     public Throwable getCause() {
089         return cause;
090     }
091 
092     /**
093      * <p>Returns a localized description of this throwable.</p>
094      * @return The localized description of this throwable.
095      * @since 1.0
096      * @javascript Re-compilers must report error on end-users directly using this method.
097      */
098     public java.lang.String getLocalizedMessage() {
099         return getMessage();
100     }
101 
102     /**
103      * <p>Returns the detail message string of this throwable.</p>
104      * @return The detail message string of this {@link Throwable} instance (which may be <tt>null</tt>).
105      * @since 1.0
106      * @javascript Re-compilers must report error on end-users directly using this method.
107      */
108     public java.lang.String getMessage() {
109         return message;
110     }
111 
112     /**
113      * <p>Initializes the cause of this throwable to the specified value.</p>
114      * @param cause The cause that caused this throwable to get thrown.
115      * @return A reference to this {@link Throwable} instance.
116      * @since 1.0
117      * @javascript Re-compilers must report error on end-users directly using this method.
118      */
119     public Throwable initCause(Throwable cause) {
120         if (this.cause != null) {
121             throw new java.lang.IllegalStateException(new Value.String("Can't")
122                     .add(" ").add("overwrite").add(" ").add("cause").var());
123         }
124         if (cause == this) {
125             throw new java.lang.IllegalArgumentException(new Value.String(
126                     "Self").add("-").add("causation").add(" ").add("not")
127                     .add(" ").add("permitted").var());
128         }
129         this.cause = cause;
130         return this;
131     }
132 
133     /**
134      * <p>Returns a short description of this throwable.</p>
135      * @return A string representation of this throwable.
136      * @since 1.0
137      * @javascript Re-compilers must report error on end-users directly using this method.
138      */
139     @Override
140     public java.lang.String toString() {
141         if (Js.not(this)) {
142             return Js.typeof(this);
143         } else {
144             java.lang.String className = getClass().getName();
145             return Js.cond(
146                     message,
147                     new Value.String(className).add(" ").add(":").add(" ")
148                             .add(message).var(), className);
149         }
150     }
151 
152     /**
153      * <p>Fills in the execution stack trace.</p>
154      * <p>This method is only used by re-compilers for a "trace mode" re-compilation.</p>
155      * @param t A {@link Throwable} that stores a stack trace.
156      * @param trace A stack tract of the current execution.
157      * @since 1.0
158      * @javascript Re-compilers must report error on end-users directly using this method.
159      */
160     protected static final void fillInStackTrace(Throwable t,
161             ArrayLike<ArrayLike<java.lang.String>> trace) {
162         if (Js.not(t.trace)) {
163             t.trace = new Vars<ArrayLike<java.lang.String>>().var();
164         }
165         t.trace.push(trace);
166     }
167 
168     /**
169      * <p>Prints this throwable and its backtrace to the standard error stream.</p>
170      * <p>In this emulation code, it has to be alerted to a message box.</p>
171      * 
172      * @since 1.0
173      * @javascript Re-compilers must report error on end-users directly using this method.
174      */
175     public void printStackTrace() {
176         java.lang.String msg = toString();
177         if (Js.be(this) && Js.be(trace)) {
178             for (int i = 0; i < trace.length(); ++i) {
179                 ((Value.String) (java.lang.Object) msg)
180                         .aadd(Js.add("\n", "\t"));
181                 ArrayLike<java.lang.String> e = trace.get(i);
182                 if (i < 16) {
183                     new Value.String(msg).aadd(new Value.String("at").add(" ")
184                             .add(e.get(0)).add(".").add(e.get(1)).var());
185                 } else {
186                     new Value.String(msg).aadd(new Value.String("more")
187                             .add(".").add(".").add(".").var());
188                 }
189             }
190         } else {
191             new Value.String(msg).aadd(new Value.String("\n").add("\t")
192                     .add("(").add("details").add(" ").add("for").add(" ")
193                     .add("trace").add(" ").add("mode").add(" ").add("only")
194                     .add(")").var());
195         }
196         Js.alert(msg);
197     }
198 }