/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/exceptions/XAmbienttalk.java
Java | 91 lines | 36 code | 12 blank | 43 comment | 0 complexity | c12c64765166206d159d265311475609 MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * XUserDefined.java created on Oct 10, 2006 at 9:31:40 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.exceptions; 29 30import edu.vub.at.objects.ATObject; 31import edu.vub.at.objects.ATTypeTag; 32import edu.vub.at.objects.coercion.NativeTypeTags; 33import edu.vub.at.objects.grammar.ATSymbol; 34import edu.vub.at.objects.natives.NATTable; 35import edu.vub.at.objects.natives.NATText; 36import edu.vub.at.objects.natives.grammar.AGAssignmentSymbol; 37import edu.vub.at.objects.natives.grammar.AGSymbol; 38 39import java.io.PrintWriter; 40import java.io.StringWriter; 41 42/** 43 * The XAmbienttalk exception class is used to wrap non-primitive exceptions in the interpreter. 44 * These exceptions are objects tagged with the Exception type which should contain a message 45 * field, which this class will read out, as well as an assignable field stackTrace which this 46 * class will initialise with the correct ambienttalk stack trace. 47 * 48 * @author smostinc 49 */ 50public class XAmbienttalk extends InterpreterException { 51 52 private static final ATSymbol _STACKTRACE_SYM_ = AGAssignmentSymbol.jAlloc("stackTrace:="); 53 private static final ATSymbol _MESSAGE_SYM_ = AGSymbol.jAlloc("message"); 54 55 private static final long serialVersionUID = -2859841280138142649L; 56 57 private final ATObject customException_; 58 59 /** 60 * Creates a native exception wrapper given an ambienttalk exception object. This constructor 61 * is called whenever an AmbientTalk object is being raised as this mechanism relies on the 62 * Java level to throw an actual Java exception. 63 * 64 * @param customException the object to be raised 65 * @throws InterpreterException if the stackTrace field of the object cannot be assigned 66 */ 67 public XAmbienttalk(ATObject customException) throws InterpreterException { 68 customException_ = customException; 69 70 StringWriter buffer = new StringWriter(); 71 72 runtimeStack_.printStackTrace(new PrintWriter(buffer, /* autoflush = */ true)); 73 customException_.impl_invoke(customException_, _STACKTRACE_SYM_, NATTable.atValue(new ATObject[] { NATText.atValue(buffer.toString()) } )); 74 } 75 76 public ATObject getAmbientTalkRepresentation() { 77 return customException_; 78 } 79 80 public ATTypeTag getType() { 81 return NativeTypeTags._CUSTOMEXCEPTION_; 82 } 83 84 public String getMessage() { 85 try { 86 return customException_.impl_invokeAccessor(customException_, _MESSAGE_SYM_,NATTable.EMPTY).asNativeText().javaValue; 87 } catch (InterpreterException e) { 88 return "Custom exception (cannot print: "+e.getMessage()+")"; 89 } 90 } 91}