/interpreter/tags/reactive-pattern-matching/src/edu/vub/at/exceptions/XAmbienttalk.java

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