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