KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > CallStack


1 /*****************************************************************************
2  * *
3  * This file is part of the BeanShell Java Scripting distribution. *
4  * Documentation and updates may be found at http://www.beanshell.org/ *
5  * *
6  * Sun Public License Notice: *
7  * *
8  * The contents of this file are subject to the Sun Public License Version *
9  * 1.0 (the "License"); you may not use this file except in compliance with *
10  * the License. A copy of the License is available at http://www.sun.com *
11  * *
12  * The Original Code is BeanShell. The Initial Developer of the Original *
13  * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
14  * (C) 2000. All Rights Reserved. *
15  * *
16  * GNU Public License Notice: *
17  * *
18  * Alternatively, the contents of this file may be used under the terms of *
19  * the GNU Lesser General Public License (the "LGPL"), in which case the *
20  * provisions of LGPL are applicable instead of those above. If you wish to *
21  * allow use of your version of this file only under the terms of the LGPL *
22  * and not to allow others to use your version of this file under the SPL, *
23  * indicate your decision by deleting the provisions above and replace *
24  * them with the notice and other provisions required by the LGPL. If you *
25  * do not delete the provisions above, a recipient may use your version of *
26  * this file under either the SPL or the LGPL. *
27  * *
28  * Patrick Niemeyer (pat@pat.net) *
29  * Author of Learning Java, O'Reilly & Associates *
30  * http://www.pat.net/~pat/ *
31  * *
32  *****************************************************************************/

33
34 package bsh;
35
36 import java.util.Vector JavaDoc;
37
38 /**
39     A stack of NameSpaces representing the call path.
40     Each method invocation, for example, pushes a new NameSpace onto the stack.
41     The top of the stack is always the current namespace of evaluation.
42     <p>
43
44     This is used to support the this.caller magic reference and to print
45     script "stack traces" when evaluation errors occur.
46     <p>
47
48     Note: it would be awefully nice to use the java.util.Stack here.
49     Sigh... have to stay 1.1 compatible.
50     <p>
51
52     Note: How can this be thread safe, you might ask? Wouldn't a thread
53     executing various beanshell methods be mutating the callstack? Don't we
54     need one CallStack per Thread in the interpreter? The answer is that we do.
55     Any java.lang.Thread enters our script via an external (hard) Java
56     reference via a This type interface, e.g. the Runnable interface
57     implemented by This or an arbitrary interface implemented by XThis.
58     In that case the This invokeMethod() method (called by any interface that
59     it exposes) creates a new CallStack for each external call.
60     <p>
61 */

62 public class CallStack
63 {
64     private Vector JavaDoc stack = new Vector JavaDoc(2);
65
66     public CallStack() { }
67
68     public CallStack( NameSpace namespace ) {
69         push( namespace );
70     }
71
72     public void clear() {
73         stack.removeAllElements();
74     }
75
76     public void push( NameSpace ns ) {
77         stack.insertElementAt( ns, 0 );
78     }
79
80     public NameSpace top() {
81         return get(0);
82     }
83
84     /**
85         zero based.
86     */

87     public NameSpace get(int depth) {
88         if ( depth >= depth() )
89             return NameSpace.JAVACODE;
90         else
91             return (NameSpace)(stack.elementAt(depth));
92     }
93     
94     /**
95         This is kind of crazy, but used by the setNameSpace command.
96         zero based.
97     */

98     public void set(int depth, NameSpace ns) {
99         stack.setElementAt(ns, depth );
100     }
101
102     public NameSpace pop() {
103         if ( depth() < 1 )
104             throw new InterpreterError("pop on empty CallStack");
105         NameSpace top = top();
106         stack.removeElementAt(0);
107         return top;
108     }
109
110     /**
111         Swap in the value as the new top of the stack and return the old
112         value.
113     */

114     public NameSpace swap( NameSpace newTop ) {
115         NameSpace oldTop = (NameSpace)(stack.elementAt(0));
116         stack.setElementAt( newTop, 0 );
117         return oldTop;
118     }
119
120     public int depth() {
121         return stack.size();
122     }
123
124     public NameSpace [] toArray() {
125         NameSpace [] nsa = new NameSpace [ depth() ];
126         stack.copyInto( nsa );
127         return nsa;
128     }
129
130     public String JavaDoc toString() {
131         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
132         sb.append("CallStack:\n");
133         NameSpace [] nsa = toArray();
134         for(int i=0; i<nsa.length; i++)
135             sb.append("\t"+nsa[i]+"\n");
136
137         return sb.toString();
138     }
139
140     /**
141         Occasionally we need to freeze the callstack for error reporting
142         purposes, etc.
143     */

144     public CallStack copy() {
145         CallStack cs = new CallStack();
146         cs.stack = (Vector JavaDoc)this.stack.clone();
147         return cs;
148     }
149 }
150
Popular Tags