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