1 33 34 35 package bsh; 36 37 import java.util.Vector ; 38 39 class 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 BSHBlock finallyBlock = null; 65 if(node != null) 66 finallyBlock = (BSHBlock)node; 67 68 70 TargetError target = null; 71 Throwable thrown = null; 72 Object ret = null; 73 74 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 if ( target != null ) 97 thrown = target.getTarget(); 98 99 100 if (thrown != null) 102 { 103 int n = catchParams.size(); 104 for(i=0; i<n; i++) 105 { 106 BSHFormalParameter fp = 108 (BSHFormalParameter)catchParams.elementAt(i); 109 110 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 ( fp.type != null ) 122 try { 123 thrown = (Throwable )Types.castObject( 124 thrown, fp.type, Types.ASSIGNMENT ); 125 } catch( UtilEvalError e ) { 126 133 continue; 134 } 135 136 BSHBlock cb = (BSHBlock)(catchBlocks.elementAt(i)); 138 139 143 NameSpace enclosingNameSpace = callstack.top(); 144 BlockNameSpace cbNameSpace = 145 new BlockNameSpace( enclosingNameSpace ); 146 147 try { 148 if ( fp.type == BSHFormalParameter.UNTYPED ) 149 cbNameSpace.setBlockVariable( fp.name, thrown ); 151 else 152 { 153 Modifiers modifiers = new Modifiers(); 155 cbNameSpace.setTypedVariable( 156 fp.name, fp.type, thrown, new Modifiers() ); 157 } 158 } catch ( UtilEvalError e ) { 159 throw new InterpreterError( 160 "Unable to set var in catch block namespace." ); 161 } 162 163 callstack.swap( cbNameSpace ); 165 try { 166 ret = cb.eval( callstack, interpreter ); 167 } finally { 168 callstack.swap( enclosingNameSpace ); 170 } 171 172 target = null; break; 174 } 175 } 176 177 if(finallyBlock != null) 179 ret = finallyBlock.eval(callstack, interpreter); 180 181 if(target != null) 183 throw target; 184 185 if(ret instanceof ReturnControl) 186 return ret; 187 else 188 return Primitive.VOID; 189 } 190 } 191 | Popular Tags |