/interpreter/tags/at2dist170907/src/edu/vub/at/objects/symbiosis/XJavaException.java

http://ambienttalk.googlecode.com/ · Java · 121 lines · 55 code · 17 blank · 49 comment · 6 complexity · 83e2227708e818ed5125934e7b2b30b3 MD5 · raw file

  1. /**
  2. * AmbientTalk/2 Project
  3. * XJavaException.java created on 3-nov-2006 at 11:43:15
  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.symbiosis;
  29. import edu.vub.at.exceptions.InterpreterException;
  30. import edu.vub.at.objects.ATTypeTag;
  31. import edu.vub.at.objects.coercion.NativeTypeTags;
  32. import java.io.PrintStream;
  33. import java.io.PrintWriter;
  34. import java.lang.reflect.Member;
  35. /**
  36. * An XJavaException AmbientTalk native exception wraps a Java exception.
  37. *
  38. * @author tvc
  39. */
  40. public final class XJavaException extends InterpreterException {
  41. private static final long serialVersionUID = 328306238980169679L;
  42. private final Throwable wrappedJavaException_;
  43. private final transient Object originatingObject_;
  44. private final transient Member originatingMethod_; // method or constructor in which exception originated
  45. /**
  46. * Constructor taking a Java exception to be wrapped as well as information on where the
  47. * exception originated from. This constructor allows documenting which message was being
  48. * on the AmbientTalk side of the symbiosis border when an exception was raised.
  49. * @param jObj the object which was responsible for throwin the method
  50. * @param jMeth the method which was invoked (from AmbientTalk) when the exception was being raised.
  51. * @param exc the exception to be wrapped.
  52. */
  53. public XJavaException(Object jObj, Member jMeth, Throwable exc) {
  54. wrappedJavaException_ = exc;
  55. originatingObject_ = jObj;
  56. originatingMethod_ = jMeth;
  57. }
  58. /**
  59. * Constructor wrapping a Java Exception to be propagated through the ambienttalk
  60. * interpreter.
  61. * @param exc the exception to be wrapped.
  62. */
  63. public XJavaException(Throwable exc) {
  64. wrappedJavaException_ = exc;
  65. originatingObject_ = null;
  66. originatingMethod_ = null;
  67. }
  68. // throwable interface implemented through composition with wrappedJavaException_
  69. public String getLocalizedMessage() {
  70. return wrappedJavaException_.getLocalizedMessage();
  71. }
  72. /**
  73. * @return the Java exception wrapped by this AmbientTalk wrapper.
  74. */
  75. public Throwable getWrappedJavaException() {
  76. return wrappedJavaException_;
  77. }
  78. public String getMessage() {
  79. if (originatingObject_ != null) {
  80. return "Java exception from " + originatingObject_.toString() + "."
  81. + originatingMethod_.getName() + ": " + wrappedJavaException_.getClass().getName() + wrappedJavaException_.getMessage();
  82. } else if (originatingMethod_ != null) {
  83. return "Java exception from constructor " + originatingMethod_.getName()
  84. + ": " + wrappedJavaException_.getClass().getName() + wrappedJavaException_.getMessage();
  85. } else {
  86. return wrappedJavaException_.getMessage();
  87. }
  88. }
  89. public void printStackTrace() {
  90. wrappedJavaException_.printStackTrace();
  91. }
  92. public void printStackTrace(PrintStream arg) {
  93. wrappedJavaException_.printStackTrace(arg);
  94. }
  95. public void printStackTrace(PrintWriter arg) {
  96. wrappedJavaException_.printStackTrace(arg);
  97. }
  98. public String toString() {
  99. return wrappedJavaException_.toString();
  100. }
  101. public ATTypeTag getType() {
  102. return NativeTypeTags._JAVAEXCEPTION_;
  103. }
  104. }