PageRenderTime 37ms CodeModel.GetById 28ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/symbiosis/XJavaException.java

http://ambienttalk.googlecode.com/
Java | 121 lines | 55 code | 17 blank | 49 comment | 6 complexity | 83e2227708e818ed5125934e7b2b30b3 MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * XJavaException.java created on 3-nov-2006 at 11:43:15
  4 * (c) Programming Technology Lab, 2006 - 2007
  5 * Authors: Tom Van Cutsem & Stijn Mostinckx
  6 * 
  7 * Permission is hereby granted, free of charge, to any person
  8 * obtaining a copy of this software and associated documentation
  9 * files (the "Software"), to deal in the Software without
 10 * restriction, including without limitation the rights to use,
 11 * copy, modify, merge, publish, distribute, sublicense, and/or
 12 * sell copies of the Software, and to permit persons to whom the
 13 * Software is furnished to do so, subject to the following
 14 * conditions:
 15 *
 16 * The above copyright notice and this permission notice shall be
 17 * included in all copies or substantial portions of the Software.
 18 *
 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26 * OTHER DEALINGS IN THE SOFTWARE.
 27 */
 28package edu.vub.at.objects.symbiosis;
 29
 30import edu.vub.at.exceptions.InterpreterException;
 31import edu.vub.at.objects.ATTypeTag;
 32import edu.vub.at.objects.coercion.NativeTypeTags;
 33
 34import java.io.PrintStream;
 35import java.io.PrintWriter;
 36import java.lang.reflect.Member;
 37
 38/**
 39 * An XJavaException AmbientTalk native exception wraps a Java exception.
 40 * 
 41 * @author tvc
 42 */
 43public final class XJavaException extends InterpreterException {
 44
 45	private static final long serialVersionUID = 328306238980169679L;
 46
 47	private final Throwable wrappedJavaException_;
 48	private final transient Object originatingObject_;
 49	private final transient Member originatingMethod_; // method or constructor in which exception originated
 50	
 51	/**
 52	 * Constructor taking a Java exception to be wrapped as well as information on where the 
 53	 * exception originated from. This constructor allows documenting which message was being 
 54	 * on the AmbientTalk side of the symbiosis border when an exception was raised. 
 55	 * @param jObj the object which was responsible for throwin the method
 56	 * @param jMeth the method which was invoked (from AmbientTalk) when the exception was being raised.
 57	 * @param exc the exception to be wrapped.
 58	 */
 59	public XJavaException(Object jObj, Member jMeth, Throwable exc) {
 60		wrappedJavaException_ = exc;
 61		originatingObject_ = jObj;
 62		originatingMethod_ = jMeth;
 63	}
 64	
 65	/**
 66	 * Constructor wrapping a Java Exception to be propagated through the ambienttalk
 67	 * interpreter.
 68	 * @param exc the exception to be wrapped.
 69	 */
 70	public XJavaException(Throwable exc) {
 71		wrappedJavaException_ = exc;
 72		originatingObject_ = null;
 73		originatingMethod_ = null;
 74	}
 75	
 76	// throwable interface implemented through composition with wrappedJavaException_
 77
 78	public String getLocalizedMessage() {
 79		return wrappedJavaException_.getLocalizedMessage();
 80	}
 81	
 82	/**
 83	 * @return the Java exception wrapped by this AmbientTalk wrapper. 
 84	 */
 85	public Throwable getWrappedJavaException() {
 86		return wrappedJavaException_;
 87	}
 88
 89	public String getMessage() {
 90		if (originatingObject_ != null) {
 91			return "Java exception from " + originatingObject_.toString() + "."
 92			           + originatingMethod_.getName() + ": " + wrappedJavaException_.getClass().getName() + wrappedJavaException_.getMessage();
 93		} else if (originatingMethod_ != null) {
 94			return "Java exception from constructor " + originatingMethod_.getName()
 95			   + ": " + wrappedJavaException_.getClass().getName() + wrappedJavaException_.getMessage();
 96		} else {
 97			return wrappedJavaException_.getMessage();
 98		}
 99	}
100
101	public void printStackTrace() {
102		wrappedJavaException_.printStackTrace();
103	}
104
105	public void printStackTrace(PrintStream arg) {
106		wrappedJavaException_.printStackTrace(arg);
107	}
108
109	public void printStackTrace(PrintWriter arg) {
110		wrappedJavaException_.printStackTrace(arg);
111	}
112
113	public String toString() {
114		return wrappedJavaException_.toString();
115	}
116	
117	public ATTypeTag getType() {
118		return NativeTypeTags._JAVAEXCEPTION_;
119	}
120	
121}