PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/bsh/EvalError.java

#
Java | 164 lines | 80 code | 23 blank | 61 comment | 18 complexity | dfef08938e8a73ddb62b81999436f162 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. /**
  35. EvalError indicates that we cannot continue evaluating the script
  36. or the script has thrown an exception.
  37. EvalError may be thrown for a script syntax error, an evaluation
  38. error such as referring to an undefined variable, an internal error.
  39. <p>
  40. @see TargetError
  41. */
  42. public class EvalError extends Exception
  43. {
  44. SimpleNode node;
  45. // Note: no way to mutate the Throwable message, must maintain our own
  46. String message;
  47. CallStack callstack;
  48. public EvalError( String s, SimpleNode node, CallStack callstack ) {
  49. setMessage(s);
  50. this.node = node;
  51. // freeze the callstack for the stack trace.
  52. if ( callstack != null )
  53. this.callstack = callstack.copy();
  54. }
  55. /**
  56. Print the error with line number and stack trace.
  57. */
  58. public String toString()
  59. {
  60. String trace;
  61. if ( node != null )
  62. trace = " : at Line: "+ node.getLineNumber()
  63. + " : in file: "+ node.getSourceFile()
  64. + " : "+node.getText();
  65. else
  66. // Users should not normally see this.
  67. trace = ": <at unknown location>";
  68. if ( callstack != null )
  69. trace = trace +"\n" + getScriptStackTrace();
  70. return getMessage() + trace;
  71. }
  72. /**
  73. Re-throw the error, prepending the specified message.
  74. */
  75. public void reThrow( String msg )
  76. throws EvalError
  77. {
  78. prependMessage( msg );
  79. throw this;
  80. }
  81. /**
  82. The error has trace info associated with it.
  83. i.e. It has an AST node that can print its location and source text.
  84. */
  85. SimpleNode getNode() {
  86. return node;
  87. }
  88. void setNode( SimpleNode node ) {
  89. this.node = node;
  90. }
  91. public String getErrorText() {
  92. if ( node != null )
  93. return node.getText() ;
  94. else
  95. return "<unknown error>";
  96. }
  97. public int getErrorLineNumber() {
  98. if ( node != null )
  99. return node.getLineNumber() ;
  100. else
  101. return -1;
  102. }
  103. public String getErrorSourceFile() {
  104. if ( node != null )
  105. return node.getSourceFile() ;
  106. else
  107. return "<unknown file>";
  108. }
  109. public String getScriptStackTrace()
  110. {
  111. if ( callstack == null )
  112. return "<Unknown>";
  113. String trace = "";
  114. CallStack stack = callstack.copy();
  115. while ( stack.depth() > 0 )
  116. {
  117. NameSpace ns = stack.pop();
  118. SimpleNode node = ns.getNode();
  119. if ( ns.isMethod )
  120. trace = trace + "\nCalled from method: " + ns.getName()
  121. + " : at Line: "+ node.getLineNumber()
  122. + " : in file: "+ node.getSourceFile()
  123. + " : "+node.getText();
  124. }
  125. return trace;
  126. }
  127. /**
  128. @see #toString() for a full display of the information
  129. */
  130. public String getMessage() { return message; }
  131. public void setMessage( String s ) { message = s; }
  132. /**
  133. Prepend the message if it is non-null.
  134. */
  135. protected void prependMessage( String s ) {
  136. if ( s != null )
  137. message = s + " : "+ message;
  138. }
  139. }