001 /*****************************************************************************
002 * *
003 * This file is part of the BeanShell Java Scripting distribution. *
004 * Documentation and updates may be found at http://www.beanshell.org/ *
005 * *
006 * Sun Public License Notice: *
007 * *
008 * The contents of this file are subject to the Sun Public License Version *
009 * 1.0 (the "License"); you may not use this file except in compliance with *
010 * the License. A copy of the License is available at http://www.sun.com *
011 * *
012 * The Original Code is BeanShell. The Initial Developer of the Original *
013 * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014 * (C) 2000. All Rights Reserved. *
015 * *
016 * GNU Public License Notice: *
017 * *
018 * Alternatively, the contents of this file may be used under the terms of *
019 * the GNU Lesser General Public License (the "LGPL"), in which case the *
020 * provisions of LGPL are applicable instead of those above. If you wish to *
021 * allow use of your version of this file only under the terms of the LGPL *
022 * and not to allow others to use your version of this file under the SPL, *
023 * indicate your decision by deleting the provisions above and replace *
024 * them with the notice and other provisions required by the LGPL. If you *
025 * do not delete the provisions above, a recipient may use your version of *
026 * this file under either the SPL or the LGPL. *
027 * *
028 * Patrick Niemeyer (pat@pat.net) *
029 * Author of Learning Java, O'Reilly & Associates *
030 * http://www.pat.net/~pat/ *
031 * *
032 *****************************************************************************/
033
034
035 package bsh;
036
037 import java.lang.reflect.InvocationTargetException;
038
039 import java.io.PrintStream;
040
041 /**
042 TargetError is an EvalError that wraps an exception thrown by the script
043 (or by code called from the script). TargetErrors indicate exceptions
044 which can be caught within the script itself, whereas a general EvalError
045 indicates that the script cannot be evaluated further for some reason.
046
047 If the exception is caught within the script it is automatically unwrapped,
048 so the code looks like normal Java code. If the TargetError is thrown
049 from the eval() or interpreter.eval() method it may be caught and unwrapped
050 to determine what exception was thrown.
051 */
052 public class TargetError extends EvalError
053 {
054 Throwable target;
055 boolean inNativeCode;
056
057 public TargetError(
058 String msg, Throwable t, SimpleNode node, CallStack callstack,
059 boolean inNativeCode )
060 {
061 super( msg, node, callstack );
062 target = t;
063 this.inNativeCode = inNativeCode;
064 }
065
066 public TargetError( Throwable t, SimpleNode node, CallStack callstack )
067 {
068 this("TargetError", t, node, callstack, false);
069 }
070
071 public Throwable getTarget()
072 {
073 // check for easy mistake
074 if(target instanceof InvocationTargetException)
075 return((InvocationTargetException)target).getTargetException();
076 else
077 return target;
078 }
079
080 public String toString()
081 {
082 return super.toString()
083 + "\nTarget exception: " +
084 printTargetError( target );
085 }
086
087 public void printStackTrace() {
088 printStackTrace( false, System.err );
089 }
090
091 public void printStackTrace( PrintStream out ) {
092 printStackTrace( false, out );
093 }
094
095 public void printStackTrace( boolean debug, PrintStream out ) {
096 if ( debug ) {
097 super.printStackTrace( out );
098 out.println("--- Target Stack Trace ---");
099 }
100 target.printStackTrace( out );
101 }
102
103 /**
104 Generate a printable string showing the wrapped target exception.
105 If the proxy mechanism is available, allow the extended print to
106 check for UndeclaredThrowableException and print that embedded error.
107 */
108 public String printTargetError( Throwable t )
109 {
110 String s = target.toString();
111
112 if ( Capabilities.canGenerateInterfaces() )
113 s += "\n" + xPrintTargetError( t );
114
115 return s;
116 }
117
118 /**
119 Extended form of print target error.
120 This indirection is used to print UndeclaredThrowableExceptions
121 which are possible when the proxy mechanism is available.
122
123 We are shielded from compile problems by using a bsh script.
124 This is acceptable here because we're not in a critical path...
125 Otherwise we'd need yet another dynamically loaded module just for this.
126 */
127 public String xPrintTargetError( Throwable t )
128 {
129 String getTarget =
130 "import java.lang.reflect.UndeclaredThrowableException;"+
131 "String result=\"\";"+
132 "while ( target instanceof UndeclaredThrowableException ) {"+
133 " target=target.getUndeclaredThrowable(); " +
134 " result+=\"Nested: \"+target.toString();" +
135 "}"+
136 "return result;";
137 Interpreter i = new Interpreter();
138 try {
139 i.set("target", t);
140 return (String)i.eval( getTarget );
141 } catch ( EvalError e ) {
142 throw new InterpreterError("xprintarget: "+e.toString() );
143 }
144 }
145
146 /**
147 Return true if the TargetError was generated from native code.
148 e.g. if the script called into a compiled java class which threw
149 the excpetion. We distinguish so that we can print the stack trace
150 for the native code case... the stack trace would not be useful if
151 the exception was generated by the script. e.g. if the script
152 explicitly threw an exception... (the stack trace would simply point
153 to the bsh internals which generated the exception).
154 */
155 public boolean inNativeCode() {
156 return inNativeCode;
157 }
158 }
159