PageRenderTime 103ms CodeModel.GetById 70ms app.highlight 11ms RepoModel.GetById 20ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/BSHTryStatement.java

#
Java | 190 lines | 96 code | 25 blank | 69 comment | 21 complexity | 8cda59a8389973ae51281f4645ff9c9a 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 org.gjt.sp.jedit.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.castObject(
124							thrown/*rsh*/, fp.type/*lhsType*/, Types.ASSIGNMENT );
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							// TODO: we do now have a way to test assignment
131							// 	in castObject(), use it?
132						*/
133						continue;
134					}
135
136				// Found match, execute catch block
137				BSHBlock cb = (BSHBlock)(catchBlocks.elementAt(i));
138
139				// Prepare to execute the block.
140				// We must create a new BlockNameSpace to hold the catch
141				// parameter and swap it on the stack after initializing it.
142
143				NameSpace enclosingNameSpace = callstack.top();
144				BlockNameSpace cbNameSpace = 
145					new BlockNameSpace( enclosingNameSpace );
146
147				try {
148					if ( fp.type == BSHFormalParameter.UNTYPED )
149						// set an untyped variable directly in the block
150						cbNameSpace.setBlockVariable( fp.name, thrown );
151					else
152					{
153						// set a typed variable (directly in the block)
154						Modifiers modifiers = new Modifiers();
155						cbNameSpace.setTypedVariable(
156							fp.name, fp.type, thrown, new Modifiers()/*none*/ );
157					}
158				} catch ( UtilEvalError e ) {
159					throw new InterpreterError(
160						"Unable to set var in catch block namespace." );
161				}
162
163				// put cbNameSpace on the top of the stack
164				callstack.swap( cbNameSpace );
165				try {
166					ret = cb.eval( callstack, interpreter );
167				} finally {
168					// put it back
169					callstack.swap( enclosingNameSpace );
170				}
171
172				target = null;  // handled target
173				break;
174			}
175		}
176
177		// evaluate finally block
178		if(finallyBlock != null)
179			ret = finallyBlock.eval(callstack, interpreter);
180
181		// exception fell through, throw it upward...
182		if(target != null)
183			throw target;
184
185		if(ret instanceof ReturnControl)
186			return ret;
187		else	
188			return Primitive.VOID;
189	}
190}