001: /*=============================================================================
002: * Copyright Texas Instruments 2000. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.exceptions;
022:
023: import oscript.data.*;
024:
025: /**
026: * When a script object is thrown, it is packaged as an instance of this
027: * class.
028: *
029: * @author Rob Clark (rob@ti.com)
030: * <!--$Format: " * @version $Revision$"$-->
031: * @version 1.19
032: */
033: public class PackagedScriptObjectException extends RuntimeException {
034: /**
035: * The wrapped exception object.
036: */
037: public Value val;
038:
039: /*=======================================================================*/
040: /**
041: * Class Constructor.
042: *
043: * @param val the packaged script object
044: */
045: private PackagedScriptObjectException(Value val) {
046: super ();
047:
048: this .val = val;
049: }
050:
051: /*=======================================================================*/
052: /**
053: * Use this method to get a new exception to throw... eventually we
054: * might play tricks like caching a pre-allocated exception per thread.
055: *
056: * @param val the script "exception" object to wrap
057: * @return a real java exception (ie <code>PackagedScriptObjectException</code>
058: */
059: public static final PackagedScriptObjectException makeExceptionWrapper(
060: Value val) {
061: return new PackagedScriptObjectException(val);
062: }
063:
064: /**
065: * A helper for evaluating "throw" statements, so script code can throw
066: * java exceptions
067: */
068: public static final PackagedScriptObjectException makeExceptionWrapper2(
069: Value val) {
070: if ((val instanceof JavaObjectWrapper)
071: && (val.castToJavaObject() instanceof Throwable))
072: return makeExceptionWrapper(new OJavaException(
073: (Throwable) (val.castToJavaObject()), val));
074: else
075: return makeExceptionWrapper(val);
076: }
077:
078: /*=======================================================================*/
079: /**
080: *
081: */
082: public Throwable fillInStackTrace() {
083: if (Value.DEBUG)
084: return super .fillInStackTrace();
085: // no-op... don't waste time filling in stack trace, because we don't
086: // want it!
087: return this ;
088: }
089:
090: /*=======================================================================*/
091: /**
092: *
093: */
094: public String getMessage() {
095: if (getMessageLevel > 0) {
096: System.err.println(getClass().getName() + ": " + hashCode()
097: + " -> " + val.getType() + "/"
098: + val.getClass().getName() + ": " + val.hashCode());
099: if (getMessageLevel > 5) {
100: System.err
101: .println("detected getMessage() loop... doh!");
102: Thread.dumpStack();
103: return "<<<unknown exception message>>>";
104: }
105: }
106: try {
107: getMessageLevel++;
108: return val.castToString();
109: } finally {
110: getMessageLevel--;
111: }
112: }
113:
114: // XXX used to detect a problem with infinite loop... seems to be that if val
115: // is itself a OJavaException with a link back to this PackagedScriptObjectException
116: // that end up in a loop!
117: private int getMessageLevel = 0;
118: }
119:
120: /*
121: * Local Variables:
122: * tab-width: 2
123: * indent-tabs-mode: nil
124: * mode: java
125: * c-indentation-style: java
126: * c-basic-offset: 2
127: * eval: (c-set-offset 'substatement-open '0)
128: * eval: (c-set-offset 'case-label '+)
129: * eval: (c-set-offset 'inclass '+)
130: * eval: (c-set-offset 'inline-open '0)
131: * End:
132: */
|