/interpreter/tags/at2-build190607/src/edu/vub/at/exceptions/XAmbienttalk.java

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