PageRenderTime 106ms CodeModel.GetById 94ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

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

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