PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/bsh/TargetError.java

#
Java | 159 lines | 74 code | 18 blank | 67 comment | 3 complexity | d294a7f08867775574d0c0ee7ac146d0 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. public class TargetError extends EvalError
  47. {
  48. Throwable target;
  49. boolean inNativeCode;
  50. public TargetError(
  51. String msg, Throwable t, SimpleNode node, CallStack callstack,
  52. boolean inNativeCode )
  53. {
  54. super( msg, node, callstack );
  55. target = t;
  56. this.inNativeCode = inNativeCode;
  57. }
  58. public TargetError( Throwable t, SimpleNode node, CallStack callstack )
  59. {
  60. this("TargetError", t, node, callstack, false);
  61. }
  62. public Throwable getTarget()
  63. {
  64. // check for easy mistake
  65. if(target instanceof InvocationTargetException)
  66. return((InvocationTargetException)target).getTargetException();
  67. else
  68. return target;
  69. }
  70. public String toString()
  71. {
  72. return super.toString()
  73. + "\nTarget exception: " +
  74. printTargetError( target );
  75. }
  76. public void printStackTrace() {
  77. printStackTrace( false, System.err );
  78. }
  79. public void printStackTrace( PrintStream out ) {
  80. printStackTrace( false, out );
  81. }
  82. public void printStackTrace( boolean debug, PrintStream out ) {
  83. if ( debug ) {
  84. super.printStackTrace( out );
  85. out.println("--- Target Stack Trace ---");
  86. }
  87. target.printStackTrace( out );
  88. }
  89. /**
  90. Generate a printable string showing the wrapped target exception.
  91. If the proxy mechanism is available, allow the extended print to
  92. check for UndeclaredThrowableException and print that embedded error.
  93. */
  94. public String printTargetError( Throwable t )
  95. {
  96. String s = target.toString();
  97. if ( Capabilities.canGenerateInterfaces() )
  98. s += "\n" + xPrintTargetError( t );
  99. return s;
  100. }
  101. /**
  102. Extended form of print target error.
  103. This indirection is used to print UndeclaredThrowableExceptions
  104. which are possible when the proxy mechanism is available.
  105. We are shielded from compile problems by using a bsh script.
  106. This is acceptable here because we're not in a critical path...
  107. Otherwise we'd need yet another dynamically loaded module just for this.
  108. */
  109. public String xPrintTargetError( Throwable t )
  110. {
  111. String getTarget =
  112. "import java.lang.reflect.UndeclaredThrowableException;"+
  113. "String result=\"\";"+
  114. "while ( target instanceof UndeclaredThrowableException ) {"+
  115. " target=target.getUndeclaredThrowable(); " +
  116. " result+=\"Nested: \"+target.toString();" +
  117. "}"+
  118. "return result;";
  119. Interpreter i = new Interpreter();
  120. try {
  121. i.set("target", t);
  122. return (String)i.eval( getTarget );
  123. } catch ( EvalError e ) {
  124. throw new InterpreterError("xprintarget: "+e.toString() );
  125. }
  126. }
  127. /**
  128. Return true if the TargetError was generated from native code.
  129. e.g. if the script called into a compiled java class which threw
  130. the excpetion. We distinguish so that we can print the stack trace
  131. for the native code case... the stack trace would not be useful if
  132. the exception was generated by the script. e.g. if the script
  133. explicitly threw an exception... (the stack trace would simply point
  134. to the bsh internals which generated the exception).
  135. */
  136. public boolean inNativeCode() {
  137. return inNativeCode;
  138. }
  139. }