PageRenderTime 177ms CodeModel.GetById 167ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-1-pre5/bsh/TargetError.java

#
Java | 178 lines | 76 code | 20 blank | 82 comment | 3 complexity | 13408cde4e83a8c6a6cd4232efbe0ecd 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( SimpleNode )
 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			"result=\"\";"+
151			"while ( target instanceof UndeclaredThrowableException ) {"+
152			"	target=target.getUndeclaredThrowable(); " +
153			"	result+=\"Nested: \"+target.toString();" +
154			"}"+
155			"return result;";
156		Interpreter i = new Interpreter();
157		try {
158			i.set("target", t);
159			return (String)i.eval( getTarget );
160		} catch ( EvalError e ) {
161			throw new InterpreterError("xprintarget: "+e.toString() );
162		}
163	}
164
165	/**
166		Return true if the TargetError was generated from native code.
167		e.g. if the script called into a compiled java class which threw
168		the excpetion.  We distinguish so that we can print the stack trace
169		for the native code case... the stack trace would not be useful if
170		the exception was generated by the script.  e.g. if the script
171		explicitly threw an exception... (the stack trace would simply point
172		to the bsh internals which generated the exception).
173	*/
174	public boolean inNativeCode() { 
175		return inNativeCode; 
176	}
177}
178