選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AbortException.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-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 boolean isSilent = false;
  38. /** used when message text is null */
  39. public static final String NO_MESSAGE_TEXT
  40. = "AbortException (no message)";
  41. private static final ArrayList porters = new ArrayList();
  42. /**
  43. * Get a porter exception from the pool.
  44. * Porter exceptions do <b>not</b> have valid stack traces.
  45. * They are used only to avoid generating stack traces when
  46. * using throw/catch to abruptly complete but continue.
  47. */
  48. public static AbortException borrowPorter(IMessage message) {
  49. AbortException result;
  50. synchronized(porters) {
  51. if (porters.size() > 0) {
  52. result = (AbortException) porters.get(0);
  53. } else {
  54. result = new AbortException();
  55. result.setIsSilent(false);
  56. }
  57. }
  58. result.setIMessage(message);
  59. result.isPorter = true;
  60. return result;
  61. }
  62. /**
  63. * Return (or add) a porter exception to the pool.
  64. */
  65. public static void returnPorter(AbortException porter) {
  66. synchronized(porters) {
  67. if (porters.contains(porter)) {
  68. throw new IllegalStateException("already have " + porter);
  69. } else {
  70. porters.add(porter);
  71. }
  72. }
  73. }
  74. /** @return NO_MESSAGE_TEXT or message.getMessage() if not null */
  75. private static String extractMessage(IMessage message) {
  76. if (null == message) {
  77. return NO_MESSAGE_TEXT;
  78. } else {
  79. String m = message.getMessage();
  80. if (null == m) {
  81. return NO_MESSAGE_TEXT;
  82. } else {
  83. return m;
  84. }
  85. }
  86. }
  87. /** structured message abort */
  88. protected IMessage message;
  89. /** true if this is a porter exception - only used to hold message */
  90. protected boolean isPorter;
  91. /** abort with default String message */
  92. public AbortException() {
  93. this("ABORT");
  94. isSilent = true;
  95. }
  96. /** abort with message */
  97. public AbortException(String s) {
  98. super(null != s ? s : NO_MESSAGE_TEXT);
  99. this.message = null;
  100. }
  101. /** abort with structured message */
  102. public AbortException(IMessage message) {
  103. super(extractMessage(message));
  104. this.message = message;
  105. }
  106. /** @return IMessage structured message, if set */
  107. public IMessage getIMessage() {
  108. return message;
  109. }
  110. /**
  111. * The stack trace of a porter is invalid; it is only used
  112. * to carry a message (which may itself have a wrapped exception).
  113. * @return true if this exception is only to carry exception
  114. */
  115. public boolean isPorter() {
  116. return isPorter;
  117. }
  118. /** @return Throwable at bottom of IMessage chain, if any */
  119. public Throwable getThrown() {
  120. Throwable result = null;
  121. IMessage m = getIMessage();
  122. if (null != m) {
  123. result = m.getThrown();
  124. if (result instanceof AbortException) {
  125. return ((AbortException) result).getThrown();
  126. }
  127. }
  128. return result;
  129. }
  130. private void setIMessage(IMessage message) {
  131. this.message = message;
  132. }
  133. // ----------- proxy attempts
  134. /**
  135. * Get message for this AbortException,
  136. * either associated explicitly as message
  137. * or implicitly as IMessage message or
  138. * its thrown message.
  139. * @see java.lang.Throwable#getMessage()
  140. */
  141. public String getMessage() {
  142. String message = super.getMessage();
  143. if ((null == message) || (NO_MESSAGE_TEXT == message)) {
  144. IMessage m = getIMessage();
  145. if (null != m) {
  146. message = m.getMessage();
  147. if (null == message) {
  148. Throwable thrown = m.getThrown();
  149. if (null != thrown) {
  150. message = thrown.getMessage();
  151. }
  152. }
  153. }
  154. if (null == message) {
  155. message = NO_MESSAGE_TEXT; // better than nothing
  156. }
  157. }
  158. return message;
  159. }
  160. /**
  161. * @see java.lang.Throwable#printStackTrace()
  162. */
  163. public void printStackTrace() {
  164. printStackTrace(System.out);
  165. }
  166. /**
  167. * Print the stack trace of any enclosed thrown
  168. * or this otherwise.
  169. * @see java.lang.Throwable#printStackTrace(PrintStream)
  170. */
  171. public void printStackTrace(PrintStream s) {
  172. IMessage m = getIMessage();
  173. Throwable thrown = (null == m? null : m.getThrown());
  174. if (!isPorter() || (null == thrown)) {
  175. s.println("Message: " + m);
  176. super.printStackTrace(s);
  177. } else {
  178. thrown.printStackTrace(s);
  179. }
  180. }
  181. /**
  182. * Print the stack trace of any enclosed thrown
  183. * or this otherwise.
  184. * @see java.lang.Throwable#printStackTrace(PrintWriter)
  185. */
  186. public void printStackTrace(PrintWriter s) {
  187. IMessage m = getIMessage();
  188. Throwable thrown = (null == m? null : m.getThrown());
  189. if (null == thrown) { // Always print
  190. if (isPorter()) {
  191. s.println("(Warning porter AbortException without thrown)");
  192. }
  193. s.println("Message: " + m);
  194. super.printStackTrace(s);
  195. } else {
  196. thrown.printStackTrace(s);
  197. }
  198. }
  199. public boolean isSilent() {
  200. return isSilent;
  201. }
  202. public void setIsSilent(boolean isSilent) {
  203. this.isSilent = isSilent;
  204. }
  205. }