PageRenderTime 68ms CodeModel.GetById 59ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/bsh/EvalError.java

#
Java | 173 lines | 88 code | 24 blank | 61 comment | 22 complexity | 4ecc7ea0fadba993f6ccad4054c20fc4 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 org.gjt.sp.jedit.bsh;
 36
 37/**
 38	EvalError indicates that we cannot continue evaluating the script
 39	or the script has thrown an exception.
 40
 41	EvalError may be thrown for a script syntax error, an evaluation 
 42	error such as referring to an undefined variable, an internal error.
 43	<p>
 44	
 45	@see TargetError
 46*/
 47public class EvalError extends Exception 
 48{
 49	SimpleNode node;
 50
 51	// Note: no way to mutate the Throwable message, must maintain our own
 52	String message;
 53
 54	CallStack callstack;
 55
 56	public EvalError( String s, SimpleNode node, CallStack callstack ) {
 57		setMessage(s);
 58		this.node = node;
 59		// freeze the callstack for the stack trace.
 60		if ( callstack != null )
 61			this.callstack = callstack.copy();
 62	}
 63
 64	/**
 65		Print the error with line number and stack trace.
 66	*/
 67	public String toString() 
 68	{
 69		String trace;
 70		if ( node != null )
 71			trace = " : at Line: "+ node.getLineNumber() 
 72				+ " : in file: "+ node.getSourceFile()
 73				+ " : "+node.getText();
 74		else
 75			// Users should not normally see this.
 76			trace = ": <at unknown location>";
 77
 78		if ( callstack != null )
 79			trace = trace +"\n" + getScriptStackTrace();
 80
 81		return getMessage() + trace;
 82	}
 83
 84	/**
 85		Re-throw the error, prepending the specified message.
 86	*/
 87	public void reThrow( String msg ) 
 88		throws EvalError 
 89	{
 90		prependMessage( msg );
 91		throw this;
 92	}
 93
 94	/**
 95		The error has trace info associated with it. 
 96		i.e. It has an AST node that can print its location and source text.
 97	*/
 98	SimpleNode getNode() {
 99		return node;
100	}
101
102	void setNode( SimpleNode node ) {
103		this.node = node;
104	}
105
106	public String getErrorText() { 
107		if ( node != null )
108			return node.getText() ;
109		else
110			return "<unknown error>";
111	}
112
113	public int getErrorLineNumber() { 
114		if ( node != null )
115			return node.getLineNumber() ;
116		else
117			return -1;
118	}
119
120	public String getErrorSourceFile() {
121		if ( node != null )
122			return node.getSourceFile() ;
123		else
124			return "<unknown file>";
125	}
126
127	public String getScriptStackTrace() 
128	{
129		if ( callstack == null )
130			return "<Unknown>";
131
132		String trace = "";
133		CallStack stack = callstack.copy();
134		while ( stack.depth() > 0 ) 
135		{
136			NameSpace ns = stack.pop();
137			SimpleNode node = ns.getNode();
138			if ( ns.isMethod )
139			{
140				trace = trace + "\nCalled from method: " + ns.getName();
141				if ( node != null )
142					trace += " : at Line: "+ node.getLineNumber() 
143						+ " : in file: "+ node.getSourceFile()
144						+ " : "+node.getText();
145			}
146		}
147
148		return trace;
149	}
150
151	/**
152		@see #toString() for a full display of the information
153	*/
154	public String getMessage() { return message; }
155
156	public void setMessage( String s ) { message = s; }
157
158	/**
159		Prepend the message if it is non-null.
160	*/
161	protected void prependMessage( String s ) 
162	{ 
163		if ( s == null )
164			return;
165
166		if ( message == null )
167			message = s;
168		else
169			message = s + " : "+ message;
170	}
171
172}
173