PageRenderTime 250ms CodeModel.GetById 234ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/bsh/BSHTryStatement.java

#
Java | 185 lines | 93 code | 25 blank | 67 comment | 21 complexity | 9485230f0743930520ac94eada865d18 MD5 | raw file
  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
 35package bsh;
 36
 37import java.util.Vector;
 38
 39class BSHTryStatement extends SimpleNode
 40{
 41	BSHTryStatement(int id)
 42	{
 43		super(id);
 44	}
 45
 46	public Object eval( CallStack callstack, Interpreter interpreter)  
 47		throws EvalError
 48	{
 49		BSHBlock tryBlock = ((BSHBlock)jjtGetChild(0));
 50
 51		Vector catchParams = new Vector();
 52		Vector catchBlocks = new Vector();
 53
 54		int nchild = jjtGetNumChildren();
 55		Node node = null;
 56		int i=1;
 57		while((i < nchild) && ((node = jjtGetChild(i++)) instanceof BSHFormalParameter))
 58		{
 59			catchParams.addElement(node);
 60			catchBlocks.addElement(jjtGetChild(i++));
 61			node = null;
 62		}
 63		// finaly block
 64		BSHBlock finallyBlock = null;
 65		if(node != null)
 66			finallyBlock = (BSHBlock)node;
 67
 68// Why both of these?
 69
 70		TargetError target = null;
 71		Throwable thrown = null;
 72		Object ret = null;
 73
 74		/*
 75			Evaluate the contents of the try { } block and catch any resulting
 76			TargetErrors generated by the script.
 77			We save the callstack depth and if an exception is thrown we pop
 78			back to that depth before contiuing.  The exception short circuited
 79			any intervening method context pops.
 80
 81			Note: we the stack info... what do we do with it?  append
 82			to exception message?
 83		*/
 84		int callstackDepth = callstack.depth();
 85		try {
 86			ret = tryBlock.eval(callstack, interpreter);
 87		}
 88		catch( TargetError e ) {
 89			target = e;
 90			String stackInfo = "Bsh Stack: ";
 91			while ( callstack.depth() > callstackDepth )
 92				stackInfo += "\t" + callstack.pop() +"\n";
 93		}
 94
 95		// unwrap the target error
 96		if ( target != null )
 97			thrown = target.getTarget();
 98
 99		
100		// If we have an exception, find a catch
101		if (thrown != null) 
102		{
103			int n = catchParams.size();
104			for(i=0; i<n; i++)
105			{
106				// Get catch block
107				BSHFormalParameter fp = 
108					(BSHFormalParameter)catchParams.elementAt(i);
109
110				// Should cache this subject to classloader change message
111				// Evaluation of the formal parameter simply resolves its
112				// type via the specified namespace.. it doesn't modify the
113				// namespace.
114				fp.eval( callstack, interpreter );
115
116				if ( fp.type == null && interpreter.getStrictJava() )
117					throw new EvalError(
118						"(Strict Java) Untyped catch block", this, callstack );
119
120				// If the param is typed check assignability
121				if ( fp.type != null ) 
122					try {
123						thrown = (Throwable)Types.getAssignableForm(
124							thrown, fp.type);
125					} catch( UtilEvalError e ) {
126						/* 
127							Catch the mismatch and continue to try the next
128							Note: this is innefficient, should have an 
129							isAssignableFrom() that doesn't throw 
130						*/
131						continue;
132					}
133
134				// Found match, execute catch block
135				BSHBlock cb = (BSHBlock)(catchBlocks.elementAt(i));
136
137				// Prepare to execute the block.
138				// We must create a new BlockNameSpace to hold the catch
139				// parameter and swap it on the stack after initializing it.
140
141				NameSpace enclosingNameSpace = callstack.top();
142				BlockNameSpace cbNameSpace = 
143					new BlockNameSpace( enclosingNameSpace );
144
145				try {
146					if ( fp.type == BSHFormalParameter.UNTYPED )
147						// set an untyped variable directly in the block
148						cbNameSpace.setBlockVariable( fp.name, thrown );
149					else
150						// set a typed variable (directly in the block)
151						cbNameSpace.setTypedVariable(
152							fp.name, fp.type, thrown, false);
153				} catch ( UtilEvalError e ) {
154					throw new InterpreterError(
155						"Unable to set var in catch block namespace." );
156				}
157
158				// put cbNameSpace on the top of the stack
159				callstack.swap( cbNameSpace );
160				try {
161					ret = cb.eval( callstack, interpreter );
162				} finally {
163					// put it back
164					callstack.swap( enclosingNameSpace );
165				}
166
167				target = null;  // handled target
168				break;
169			}
170		}
171
172		// evaluate finally block
173		if(finallyBlock != null)
174			ret = finallyBlock.eval(callstack, interpreter);
175
176		// exception fell through, throw it upward...
177		if(target != null)
178			throw target;
179
180		if(ret instanceof ReturnControl)
181			return ret;
182		else	
183			return Primitive.VOID;
184	}
185}