KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bsh > TargetError


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
34
35 package bsh;
36
37 import java.lang.reflect.InvocationTargetException JavaDoc;
38
39 import java.io.PrintStream JavaDoc;
40
41 /**
42     TargetError is an EvalError that wraps an exception thrown by the script
43     (or by code called from the script). TargetErrors indicate exceptions
44     which can be caught within the script itself, whereas a general EvalError
45     indicates that the script cannot be evaluated further for some reason.
46     
47     If the exception is caught within the script it is automatically unwrapped,
48     so the code looks like normal Java code. If the TargetError is thrown
49     from the eval() or interpreter.eval() method it may be caught and unwrapped
50     to determine what exception was thrown.
51 */

52 public class TargetError extends EvalError
53 {
54     Throwable JavaDoc target;
55     boolean inNativeCode;
56
57     public TargetError(
58         String JavaDoc msg, Throwable JavaDoc t, SimpleNode node, CallStack callstack,
59         boolean inNativeCode )
60     {
61         super( msg, node, callstack );
62         target = t;
63         this.inNativeCode = inNativeCode;
64     }
65
66     public TargetError( Throwable JavaDoc t, SimpleNode node, CallStack callstack )
67     {
68         this("TargetError", t, node, callstack, false);
69     }
70
71     public Throwable JavaDoc getTarget()
72     {
73         // check for easy mistake
74
if(target instanceof InvocationTargetException JavaDoc)
75             return((InvocationTargetException JavaDoc)target).getTargetException();
76         else
77             return target;
78     }
79
80     public String JavaDoc toString()
81     {
82         return super.toString()
83             + "\nTarget exception: " +
84             printTargetError( target );
85     }
86
87     public void printStackTrace() {
88         printStackTrace( false, System.err );
89     }
90
91     public void printStackTrace( PrintStream JavaDoc out ) {
92         printStackTrace( false, out );
93     }
94
95     public void printStackTrace( boolean debug, PrintStream JavaDoc out ) {
96         if ( debug ) {
97             super.printStackTrace( out );
98             out.println("--- Target Stack Trace ---");
99         }
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 JavaDoc printTargetError( Throwable JavaDoc t )
109     {
110         String JavaDoc 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 JavaDoc xPrintTargetError( Throwable JavaDoc t )
128     {
129         String JavaDoc 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 JavaDoc)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
160
Popular Tags