PageRenderTime 83ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/bsh/TargetError.java

#
Java | 176 lines | 73 code | 21 blank | 82 comment | 3 complexity | 2de082e18ff87e9f5a136fad23f61dc3 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.lang.reflect.InvocationTargetException;
  35. import java.io.PrintStream;
  36. /**
  37. TargetError is an EvalError that wraps an exception thrown by the script
  38. (or by code called from the script). TargetErrors indicate exceptions
  39. which can be caught within the script itself, whereas a general EvalError
  40. indicates that the script cannot be evaluated further for some reason.
  41. If the exception is caught within the script it is automatically unwrapped,
  42. so the code looks like normal Java code. If the TargetError is thrown
  43. from the eval() or interpreter.eval() method it may be caught and unwrapped
  44. to determine what exception was thrown.
  45. */
  46. /*
  47. Implementation note:
  48. Although it is easy to catch exceptions generated by called Java code
  49. and wrap them (e.g. in the method dispatching code), we must be careful
  50. do the same with arbitrary exceptions that we generate in interpreted
  51. code, e.g. ArithmeticException, ClassCastException.
  52. Also an important location to look at is BSHMethodInvocation. There
  53. we catch eval errors and rethrow them to compound the location information
  54. */
  55. public class TargetError extends EvalError
  56. {
  57. Throwable target;
  58. boolean inNativeCode;
  59. public TargetError(
  60. String msg, Throwable t, SimpleNode node, boolean inNativeCode )
  61. {
  62. super(msg, node);
  63. target = t;
  64. this.inNativeCode = inNativeCode;
  65. }
  66. public TargetError(Throwable t, SimpleNode node )
  67. {
  68. this("TargetError", t, node, false);
  69. }
  70. /**
  71. If you're going to use this please catch and re-throw the exception
  72. in an AST and add the node...
  73. @see reThrow()
  74. */
  75. public TargetError( String s, Throwable t )
  76. {
  77. this(s, t, null, false);
  78. }
  79. public Throwable getTarget()
  80. {
  81. // check for easy mistake
  82. if(target instanceof InvocationTargetException)
  83. return((InvocationTargetException)target).getTargetException();
  84. else
  85. return target;
  86. }
  87. public String toString() {
  88. return super.toString()
  89. + "\nTarget exception: " +
  90. printTargetError( target );
  91. }
  92. public void printStackTrace() {
  93. printStackTrace( false, System.err );
  94. }
  95. public void printStackTrace( PrintStream out ) {
  96. printStackTrace( false, out );
  97. }
  98. public void printStackTrace( boolean debug, PrintStream out ) {
  99. if ( debug ) {
  100. super.printStackTrace( out );
  101. System.out.println("--- Target Stack Trace ---");
  102. }
  103. target.printStackTrace( out );
  104. }
  105. /**
  106. Generate a printable string showing the wrapped target exception.
  107. If the proxy mechanism is available, allow the extended print to
  108. check for UndeclaredThrowableException and print that embedded error.
  109. */
  110. public String printTargetError( Throwable t )
  111. {
  112. String s = target.toString();
  113. if ( Capabilities.canGenerateInterfaces() )
  114. s += "\n" + xPrintTargetError( t );
  115. return s;
  116. }
  117. /**
  118. Extended form of print target error.
  119. This indirection is used to print UndeclaredThrowableExceptions
  120. which are possible when the proxy mechanism is available.
  121. We are shielded from compile problems by using a bsh script.
  122. This is acceptable here because we're not in a critical path...
  123. Otherwise we'd need yet another dynamically loaded module just for this.
  124. */
  125. public String xPrintTargetError( Throwable t )
  126. {
  127. String getTarget =
  128. "import java.lang.reflect.UndeclaredThrowableException;"+
  129. "if ( target instanceof UndeclaredThrowableException )"+
  130. " return target.getUndeclaredThrowable().toString();" +
  131. "else return \"\"";
  132. Interpreter i = new Interpreter();
  133. try {
  134. i.set("target", t);
  135. return (String)i.eval( getTarget );
  136. } catch ( EvalError e ) {
  137. throw new InterpreterError("xprintarget: "+e.toString() );
  138. }
  139. }
  140. /**
  141. Return true if the TargetError was generated from native code.
  142. e.g. if the script called into a compiled java class which threw
  143. the excpetion. We distinguish so that we can print the stack trace
  144. for the native code case... the stack trace would not be useful if
  145. the exception was generated by the script. e.g. if the script
  146. explicitly threw an exception... (the stack trace would simply point
  147. to the bsh internals which generated the exception).
  148. */
  149. public boolean inNativeCode() {
  150. return inNativeCode;
  151. }
  152. }