001: /*=============================================================================
002: * Copyright Texas Instruments 2000-2004. 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.data;
022:
023: import oscript.exceptions.*;
024: import oscript.util.*;
025:
026: import java.util.*;
027:
028: /**
029: * Base class for the script type "Exception". This isn't a java exception,
030: * but instead is wrapped in a <code>PackagedScriptObjectException</code>.
031: *
032: * @author Rob Clark (rob@ti.com)
033: */
034: public class OException extends OObject {
035: /**
036: * The type object for an instance of Exception.
037: */
038: public final static Value TYPE = BuiltinType
039: .makeBuiltinType("oscript.data.OException");
040: public final static String PARENT_TYPE_NAME = "oscript.data.OObject";
041: public final static String TYPE_NAME = "Exception";
042: public final static String[] MEMBER_NAMES = new String[] {
043: "castToString", "stackFrameIterator", "getMessage" };
044:
045: /**
046: * The type of the exception object
047: */
048: private Value type;
049:
050: /**
051: * The message
052: */
053: private String str;
054:
055: /**
056: * The stack trace.
057: */
058: private StackFrame sf;
059:
060: /**
061: * Because the line number may change before {@link #preserveStackFrame},
062: * we need to take care to remember the original line number for accurate
063: * back-traces.
064: */
065: private int line;
066:
067: // called from StackFrame#evalNode
068: public final void preserveStackFrame() {
069: if (line != sf.getLineNumber())
070: sf.setLineNumber(line);
071: sf = sf.getSafeCopy();
072: }
073:
074: /*=======================================================================*/
075: /**
076: * Class Constructor.
077: */
078: public OException(String str) {
079: this (TYPE, str);
080: }
081:
082: /*=======================================================================*/
083: /**
084: * Class Constructor. This is the constructor called from built-in types
085: * that subclss <i>Exception</i>.
086: */
087: protected OException(Value type, String str) {
088: super ();
089: this .type = type;
090: this .str = str;
091:
092: sf = StackFrame.currentStackFrame();
093: line = sf.getLineNumber();
094: }
095:
096: /*=======================================================================*/
097: /**
098: * Get the type of this object. The returned type doesn't have to take
099: * into account the possibility of a script type extending a built-in
100: * type, since that is handled by {@link #getType}.
101: *
102: * @return the object's type
103: */
104: protected Value getTypeImpl() {
105: return type;
106: }
107:
108: /*=======================================================================*/
109: /**
110: * Class Constructor. This is the constructor that is called via a
111: * <code>BuiltinType</code> instance.
112: *
113: * @param args arguments to this constructor
114: * @throws PackagedScriptObjectException(Exception) if wrong number of args
115: */
116: public OException(oscript.util.MemberTable args) {
117: this (getArg0(args));
118: }
119:
120: protected static final String getArg0(oscript.util.MemberTable args) {
121: if (args.length() != 1)
122: throw PackagedScriptObjectException
123: .makeExceptionWrapper(new OIllegalArgumentException(
124: "wrong number of args!"));
125: else
126: return args.referenceAt(0).castToString();
127: }
128:
129: /*=======================================================================*/
130: /**
131: * Convert this object to a native java <code>String</code> value.
132: *
133: * @return a String value
134: * @throws PackagedScriptObjectException(NoSuchMethodException)
135: */
136: public String castToString() throws PackagedScriptObjectException {
137: Iterator iterator = stackFrameIterator();
138: String str = getType().castToString() + ": " + getMessage();
139:
140: while (iterator.hasNext())
141: str += "\n at " + iterator.next();
142:
143: return str;
144: }
145:
146: /*=======================================================================*/
147: /**
148: * Get the message.
149: *
150: * @return a String value
151: */
152: public String getMessage() {
153: return str;
154: }
155:
156: /*=======================================================================*/
157: /**
158: * Return an iteration of stack frames, starting with the most deeply
159: * nested stack frame.
160: *
161: * @return an iterator of StackFrames
162: */
163: public Iterator stackFrameIterator() {
164: preserveStackFrame();
165: return sf.iterator();
166: }
167: }
168:
169: /*
170: * Local Variables:
171: * tab-width: 2
172: * indent-tabs-mode: nil
173: * mode: java
174: * c-indentation-style: java
175: * c-basic-offset: 2
176: * eval: (c-set-offset 'substatement-open '0)
177: * eval: (c-set-offset 'case-label '+)
178: * eval: (c-set-offset 'inclass '+)
179: * eval: (c-set-offset 'inline-open '0)
180: * End:
181: */
|