Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AbortException.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.bridge;
  14. import java.io.PrintStream;
  15. import java.io.PrintWriter;
  16. import java.util.ArrayList;
  17. /**
  18. * Signal that a process was aborted before completion.
  19. * This may contain a structured IMessage which indicates
  20. * why the process was aborted (e.g., the underlying exception).
  21. * For processes using try/catch to complete a method abruptly
  22. * but complete the process normally (e.g., a test failure
  23. * causes the test to abort but the reporting and testing continues
  24. * normally), use the static methods to borrow and return a "porter"
  25. * to avoid the expense of constructing a stack trace each time.
  26. * A porter stack trace is invalid, and it should only be used
  27. * to convey a message. E.g., to print the stack of the
  28. * AbortException and any contained message:
  29. * <pre>catch (AbortException ae) {
  30. * IMessage m = ae.getMessage();
  31. * if (!ae.isPorter()) ae.printStackTrace(System.err);
  32. * Throwable thrown = ae.getThrown();
  33. * if (null != thrown) thrown.printStackTrace(System.err);
  34. * }</pre>
  35. */
  36. public class AbortException extends RuntimeException { // XXX move porters out, handle proxy better
  37. private static final long serialVersionUID = -7211791639898586417L;
  38. private boolean isSilent = false;
  39. /** used when message text is null */
  40. public static final String NO_MESSAGE_TEXT
  41. = "AbortException (no message)";
  42. private static final ArrayList porters = new ArrayList();
  43. /**
  44. * Get a porter exception from the pool.
  45. * Porter exceptions do <b>not</b> have valid stack traces.
  46. * They are used only to avoid generating stack traces when
  47. * using throw/catch to abruptly complete but continue.
  48. */
  49. public static AbortException borrowPorter(IMessage message) {
  50. AbortException result;
  51. synchronized(porters) {
  52. if (porters.size() > 0) {
  53. result = (AbortException) porters.get(0);
  54. } else {
  55. result = new AbortException();
  56. result.setIsSilent(false);
  57. }
  58. }
  59. result.setIMessage(message);
  60. result.isPorter = true;
  61. return result;
  62. }
  63. /**
  64. * Return (or add) a porter exception to the pool.
  65. */
  66. public static void returnPorter(AbortException porter) {
  67. synchronized(porters) {
  68. if (porters.contains(porter)) {
  69. throw new IllegalStateException("already have " + porter);
  70. } else {
  71. porters.add(porter);
  72. }
  73. }
  74. }
  75. /** @return NO_MESSAGE_TEXT or message.getMessage() if not null */
  76. private static String extractMessage(IMessage message) {
  77. if (null == message) {
  78. return NO_MESSAGE_TEXT;
  79. } else {
  80. String m = message.getMessage();
  81. if (null == m) {
  82. return NO_MESSAGE_TEXT;
  83. } else {
  84. return m;
  85. }
  86. }
  87. }
  88. /** structured message abort */
  89. protected IMessage message;
  90. /** true if this is a porter exception - only used to hold message */
  91. protected boolean isPorter;
  92. /** abort with default String message */
  93. public AbortException() {
  94. this("ABORT");
  95. isSilent = true;
  96. }
  97. /** abort with message */
  98. public AbortException(String s) {
  99. super(null != s ? s : NO_MESSAGE_TEXT);
  100. this.message = null;
  101. }
  102. /** abort with structured message */
  103. public AbortException(IMessage message) {
  104. super(extractMessage(message));
  105. this.message = message;
  106. }
  107. /** @return IMessage structured message, if set */
  108. public IMessage getIMessage() {
  109. return message;
  110. }
  111. /**
  112. * The stack trace of a porter is invalid; it is only used
  113. * to carry a message (which may itself have a wrapped exception).
  114. * @return true if this exception is only to carry exception
  115. */
  116. public boolean isPorter() {
  117. return isPorter;
  118. }
  119. /** @return Throwable at bottom of IMessage chain, if any */
  120. public Throwable getThrown() {
  121. Throwable result = null;
  122. IMessage m = getIMessage();
  123. if (null != m) {
  124. result = m.getThrown();
  125. if (result instanceof AbortException) {
  126. return ((AbortException) result).getThrown();
  127. }
  128. }
  129. return result;
  130. }
  131. private void setIMessage(IMessage message) {
  132. this.message = message;
  133. }
  134. // ----------- proxy attempts
  135. /**
  136. * Get message for this AbortException,
  137. * either associated explicitly as message
  138. * or implicitly as IMessage message or
  139. * its thrown message.
  140. * @see java.lang.Throwable#getMessage()
  141. */
  142. public String getMessage() {
  143. String message = super.getMessage();
  144. if ((null == message) || (NO_MESSAGE_TEXT == message)) {
  145. IMessage m = getIMessage();
  146. if (null != m) {
  147. message = m.getMessage();
  148. if (null == message) {
  149. Throwable thrown = m.getThrown();
  150. if (null != thrown) {
  151. message = thrown.getMessage();
  152. }
  153. }
  154. }
  155. if (null == message) {
  156. message = NO_MESSAGE_TEXT; // better than nothing
  157. }
  158. }
  159. return message;
  160. }
  161. /**
  162. * @see java.lang.Throwable#printStackTrace()
  163. */
  164. public void printStackTrace() {
  165. printStackTrace(System.out);
  166. }
  167. /**
  168. * Print the stack trace of any enclosed thrown
  169. * or this otherwise.
  170. * @see java.lang.Throwable#printStackTrace(PrintStream)
  171. */
  172. public void printStackTrace(PrintStream s) {
  173. IMessage m = getIMessage();
  174. Throwable thrown = (null == m? null : m.getThrown());
  175. if (!isPorter() || (null == thrown)) {
  176. s.println("Message: " + m);
  177. super.printStackTrace(s);
  178. } else {
  179. thrown.printStackTrace(s);
  180. }
  181. }
  182. /**
  183. * Print the stack trace of any enclosed thrown
  184. * or this otherwise.
  185. * @see java.lang.Throwable#printStackTrace(PrintWriter)
  186. */
  187. public void printStackTrace(PrintWriter s) {
  188. IMessage m = getIMessage();
  189. Throwable thrown = (null == m? null : m.getThrown());
  190. if (null == thrown) { // Always print
  191. if (isPorter()) {
  192. s.println("(Warning porter AbortException without thrown)");
  193. }
  194. s.println("Message: " + m);
  195. super.printStackTrace(s);
  196. } else {
  197. thrown.printStackTrace(s);
  198. }
  199. }
  200. public boolean isSilent() {
  201. return isSilent;
  202. }
  203. public void setIsSilent(boolean isSilent) {
  204. this.isSilent = isSilent;
  205. }
  206. }