PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms 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
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 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. NameSpace namespace = callstack.top();
  93. // get catch block
  94. BSHFormalParameter fp =
  95. (BSHFormalParameter)catchParams.elementAt(i);
  96. // should cache this subject to classloader change message
  97. fp.eval( namespace );
  98. // If the param is typed check assignability
  99. if ( fp.type != null )
  100. try {
  101. thrown = (Throwable)NameSpace.getAssignableForm(
  102. thrown, fp.type);
  103. } catch(EvalError e) {
  104. /* Catch the mismatch and continue to try the next
  105. Note: this is innefficient, should have an
  106. isAssignableFrom() that doesn't throw */
  107. continue;
  108. }
  109. // Found match, execute catch block
  110. BSHBlock cb = (BSHBlock)(catchBlocks.elementAt(i));
  111. if ( fp.type == BSHFormalParameter.UNTYPED )
  112. namespace.setVariable(fp.name, thrown );
  113. else
  114. namespace.setTypedVariable(fp.name, fp.type, thrown,false);
  115. ret = cb.eval( callstack, interpreter );
  116. target = null; // handled target
  117. break;
  118. }
  119. }
  120. // evaluate finally block
  121. if(finallyBlock != null)
  122. ret = finallyBlock.eval(callstack, interpreter);
  123. // exception fell through, throw it upward...
  124. if(target != null)
  125. throw target;
  126. if(ret instanceof ReturnControl)
  127. return ret;
  128. else
  129. return Primitive.VOID;
  130. }
  131. }