PageRenderTime 34ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/objects/natives/NATException.java

http://ambienttalk.googlecode.com/
Java | 120 lines | 67 code | 15 blank | 38 comment | 2 complexity | 2c09895bde3fe9a40d9883e0be90a9eb MD5 | raw file
  1/**
  2 * AmbientTalk/2 Project
  3 * NATException.java created on Oct 13, 2006 at 1:46:47 PM
  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.natives;
 29
 30import java.io.ByteArrayOutputStream;
 31import java.io.PrintStream;
 32
 33import edu.vub.at.eval.Evaluator;
 34import edu.vub.at.exceptions.InterpreterException;
 35import edu.vub.at.objects.ATBoolean;
 36import edu.vub.at.objects.ATClosure;
 37import edu.vub.at.objects.ATException;
 38import edu.vub.at.objects.ATObject;
 39import edu.vub.at.objects.ATTable;
 40import edu.vub.at.objects.coercion.NativeTypeTags;
 41import edu.vub.at.objects.grammar.ATSymbol;
 42import edu.vub.at.objects.mirrors.NativeClosure;
 43import edu.vub.at.objects.mirrors.Reflection;
 44import edu.vub.at.objects.symbiosis.Symbiosis;
 45
 46/**
 47 * Instances of the class NATException provide a AmbientTalk representation for the 
 48 * default instances of all InterpreterExceptions. They allow catching exceptions 
 49 * thrown by the interpreter in AmbientTalk, as well as raising them for within
 50 * custom code. 
 51 *
 52 * @author smostinc
 53 */
 54public class NATException extends NATByCopy implements ATException {
 55	
 56	private final InterpreterException wrappedException_;
 57	
 58	public NATException(InterpreterException wrappedException) {
 59		wrappedException_ = wrappedException;
 60	}
 61
 62	public InterpreterException getWrappedException() {
 63		return wrappedException_;
 64	}
 65
 66	public NATText base_message() throws InterpreterException {
 67		return NATText.atValue(wrappedException_.getMessage());
 68	}
 69	
 70	public NATText base_stackTrace() throws InterpreterException {
 71		ByteArrayOutputStream out = new ByteArrayOutputStream();
 72		wrappedException_.printAmbientTalkStackTrace(new PrintStream(out));
 73		return NATText.atValue(out.toString());
 74	}
 75	
 76	public NativeATObject base_stackTrace__opeql_(NATText newTrace) throws InterpreterException {
 77		return Evaluator.getNil();
 78	}
 79	
 80	public ATObject meta_newInstance(ATTable initargs) throws InterpreterException {
 81        return Reflection.upExceptionCreation(wrappedException_, initargs);
 82    }
 83
 84	public ATBoolean meta_isRelatedTo(ATObject object) throws InterpreterException {
 85		if(object instanceof NATException) {
 86			return NATBoolean.atValue(
 87					wrappedException_.getClass().isAssignableFrom(
 88					((NATException)object).wrappedException_.getClass())); 
 89		} else {
 90			return NATBoolean._FALSE_;
 91		}
 92	}
 93	
 94    public ATTable meta_typeTags() throws InterpreterException {
 95    	return NATTable.of(wrappedException_.getType(), NativeTypeTags._ISOLATE_);
 96    }
 97    
 98    public NATText meta_print() throws InterpreterException {
 99    	return NATText.atValue("<exception:" + wrappedException_.getType() + ": " + wrappedException_.getMessage() + ">");
100    }
101    
102	public ATObject meta_clone() throws InterpreterException {
103		return this;
104	}
105	
106	/**
107	 * delegate messages not understood to the wrapped exception object
108	 */
109	public ATClosure meta_doesNotUnderstand(final ATSymbol selector) {
110		return new NativeClosure(this) {
111			public ATObject base_apply(ATTable args) throws InterpreterException {
112				return Symbiosis.symbioticInvocation(scope_,
113						                             wrappedException_,
114						                             wrappedException_.getClass(),
115						                             Reflection.upSelector(selector),
116						                             args.asNativeTable().elements_);
117			}
118		};
119	}
120}