PageRenderTime 150ms CodeModel.GetById 143ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/bsh/BSHTryStatement.java

#
Java | 153 lines | 77 code | 21 blank | 55 comment | 18 complexity | f66bbe4035b8ca8dfe7a72555c97f7b5 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				NameSpace namespace = callstack.top();
107				// get catch block
108				BSHFormalParameter fp = 
109					(BSHFormalParameter)catchParams.elementAt(i);
110
111				// should cache this subject to classloader change message
112				fp.eval( namespace );
113
114				// If the param is typed check assignability
115				if ( fp.type != null ) 
116					try {
117						thrown = (Throwable)NameSpace.getAssignableForm(
118							thrown, fp.type);
119					} catch(EvalError e) {
120						/* Catch the mismatch and continue to try the next
121							Note: this is innefficient, should have an 
122							isAssignableFrom() that doesn't throw */
123						continue;
124					}
125
126				// Found match, execute catch block
127				BSHBlock cb = (BSHBlock)(catchBlocks.elementAt(i));
128
129				if ( fp.type == BSHFormalParameter.UNTYPED )
130					namespace.setVariable(fp.name, thrown );
131				else
132					namespace.setTypedVariable(fp.name, fp.type, thrown,false);
133
134				ret = cb.eval( callstack, interpreter );
135				target = null;  // handled target
136				break;
137			}
138		}
139
140		// evaluate finally block
141		if(finallyBlock != null)
142			ret = finallyBlock.eval(callstack, interpreter);
143
144		// exception fell through, throw it upward...
145		if(target != null)
146			throw target;
147
148		if(ret instanceof ReturnControl)
149			return ret;
150		else	
151			return Primitive.VOID;
152	}
153}