You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MessageHandler.java 8.5KB

21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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.util.ArrayList;
  15. import java.util.Collections;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. /**
  19. * This handler accumulates messages.
  20. * To control messages accumulated,
  21. * clients can ignore messages of a given kind,
  22. * or install a listener/interceptor.
  23. * The interceptor handles all messages (even null) first,
  24. * and can halt further processing/accumlation by returning true.
  25. * Clients can obtain messages accumulated using the get... methods.
  26. * XXX this does not permit messages to be removed.
  27. */
  28. public class MessageHandler implements IMessageHolder {
  29. /** messages accumulated */
  30. protected final ArrayList messages;
  31. /** kinds of messages to be ignored */
  32. protected final ArrayList ignoring;
  33. /** result of handleMessage(..) for messages not accumulated (ignored) */
  34. protected boolean handleMessageResult;
  35. /** listener which can halt processing by returning true */
  36. protected IMessageHandler interceptor;
  37. /**
  38. * same as MessageHandler(false)
  39. */
  40. public MessageHandler() {
  41. this(false);
  42. }
  43. /**
  44. * @param accumulateOnly the result of handleMessage
  45. * (i.e., if true, then only accumulate messages - stop processing
  46. */
  47. public MessageHandler(boolean accumulateOnly) {
  48. messages = new ArrayList();
  49. ignoring = new ArrayList();
  50. init(accumulateOnly);
  51. ignore(IMessage.WEAVEINFO); // Off by default, need to explicitly be enabled (see -showWeaveInfo)
  52. }
  53. /**
  54. * Initialize this, removing any messages accumulated,
  55. * kinds being ignored, or interceptor.
  56. * Assume that this should return false from handleMessage(..).
  57. */
  58. public void init() { init(false); }
  59. /**
  60. * Initialize this, removing any messages accumulated,
  61. * kinds being ignored, or interceptor.
  62. * @param accumulateOnly boolean value returned from handleMessage
  63. * after accumulating in list
  64. */
  65. public void init(boolean accumulateOnly) {
  66. handleMessageResult = accumulateOnly;
  67. if (0 < messages.size()) {
  68. messages.clear();
  69. }
  70. if (0 < ignoring.size()) {
  71. boolean ignoringWeaveMessages = isIgnoring(IMessage.WEAVEINFO);
  72. ignoring.clear();
  73. if (ignoringWeaveMessages) ignore(IMessage.WEAVEINFO);
  74. }
  75. if (null != interceptor) {
  76. interceptor = null;
  77. }
  78. }
  79. /**
  80. * Clear the messages without changing other behavior.
  81. */
  82. public void clearMessages() {
  83. if (0 < messages.size()) {
  84. messages.clear();
  85. }
  86. }
  87. // ---------------------- IMessageHandler implementation
  88. /**
  89. * This implementation accumulates message.
  90. * If an interceptor is installed and returns true (message handled),
  91. * then processing halts and the message is not accumulated.
  92. * @see org.aspectj.bridge.IMessageHandler#handleMessage(IMessage)
  93. * @return true on interception or the constructor value otherwise
  94. */
  95. public boolean handleMessage(IMessage message) {
  96. if ((null != interceptor) && (interceptor.handleMessage(message))) {
  97. return true;
  98. }
  99. if (null == message) {
  100. throw new IllegalArgumentException("null message");
  101. }
  102. if (!ignoring.contains(message.getKind())) {
  103. messages.add(message);
  104. }
  105. return handleMessageResult;
  106. }
  107. /**
  108. * @return true if this kind has been flagged to be ignored.
  109. * @see #ignore(IMessage.Kind)
  110. * @see org.aspectj.bridge.IMessageHandler#isIgnoring(Kind)
  111. */
  112. public boolean isIgnoring(IMessage.Kind kind) {
  113. return ((null != kind) && (ignoring.contains(kind)));
  114. }
  115. // ---------------------- end of IMessageHandler implementation
  116. /**
  117. * Set a message kind to be ignored from now on
  118. */
  119. public void ignore(IMessage.Kind kind) { // XXX sync
  120. if ((null != kind) && (!ignoring.contains(kind))) {
  121. ignoring.add(kind);
  122. }
  123. }
  124. /**
  125. * Remove a message kind from the list of those ignored from now on.
  126. */
  127. public void dontIgnore(IMessage.Kind kind) {
  128. if (null != kind) {
  129. ignoring.remove(kind);
  130. }
  131. }
  132. /**
  133. * @see org.aspectj.bridge.IMessageHolder#hasAnyMessage(Kind, boolean)
  134. */
  135. public boolean hasAnyMessage(final IMessage.Kind kind, final boolean orGreater) {
  136. if (null == kind) {
  137. return (0 < messages.size());
  138. }
  139. if (!orGreater) {
  140. for (Iterator iter = messages.iterator(); iter.hasNext();) {
  141. if (kind == ((IMessage) iter.next()).getKind()) {
  142. return true;
  143. }
  144. }
  145. } else {
  146. for (Iterator iter = messages.iterator(); iter.hasNext();) {
  147. IMessage m = (IMessage) iter.next();
  148. if (kind.isSameOrLessThan(m.getKind())) {
  149. return true;
  150. }
  151. }
  152. }
  153. return false;
  154. }
  155. /**
  156. * @return number of messages accumulated of a given kind
  157. */
  158. public int numMessages(IMessage.Kind kind, final boolean orGreater) {
  159. if (null == kind) {
  160. return messages.size();
  161. }
  162. int result = 0;
  163. if (!orGreater) {
  164. for (Iterator iter = messages.iterator(); iter.hasNext();) {
  165. if (kind == ((IMessage) iter.next()).getKind()) {
  166. result++;
  167. }
  168. }
  169. } else {
  170. for (Iterator iter = messages.iterator(); iter.hasNext();) {
  171. IMessage m = (IMessage) iter.next();
  172. if (kind.isSameOrLessThan(m.getKind())) {
  173. result++;
  174. }
  175. }
  176. }
  177. return result;
  178. }
  179. /**
  180. * @see org.aspectj.bridge.IMessageHolder#getUnmodifiableListView()
  181. */
  182. public List getUnmodifiableListView() {
  183. return Collections.unmodifiableList(messages);
  184. }
  185. /**
  186. * Get all messages or those of a specific kind.
  187. * Pass null to get all kinds.
  188. * @param kind the IMessage.Kind expected, or null for all messages
  189. * @return IMessage[] of messages of the right kind
  190. */
  191. public IMessage[] getMessages(IMessage.Kind kind, final boolean orGreater) {
  192. if (null == kind) {
  193. return (IMessage[]) messages.toArray(IMessage.RA_IMessage);
  194. }
  195. ArrayList result = new ArrayList();
  196. if (!orGreater) {
  197. for (Iterator iter = messages.iterator(); iter.hasNext();) {
  198. IMessage m = (IMessage) iter.next();
  199. if (kind == m.getKind()) {
  200. result.add(m);
  201. }
  202. }
  203. } else {
  204. for (Iterator iter = messages.iterator(); iter.hasNext();) {
  205. IMessage m = (IMessage) iter.next();
  206. if (kind.isSameOrLessThan(m.getKind())) {
  207. result.add(m);
  208. }
  209. }
  210. }
  211. if (0 == result.size()) {
  212. return IMessage.RA_IMessage;
  213. }
  214. return (IMessage[]) result.toArray(IMessage.RA_IMessage);
  215. }
  216. /**
  217. * @return array of error messages, or IMessage.NONE
  218. */
  219. public IMessage[] getErrors() {
  220. return getMessages(IMessage.ERROR, false);
  221. }
  222. /**
  223. * @return array of warning messages, or IMessage.NONE
  224. */
  225. public IMessage[] getWarnings() {
  226. return getMessages(IMessage.WARNING, false);
  227. }
  228. /**
  229. * Set the interceptor which gets any message before we process it.
  230. * @param interceptor the IMessageHandler passed the message.
  231. * Pass null to remove the old interceptor.
  232. */
  233. public void setInterceptor(IMessageHandler interceptor) {
  234. this.interceptor = interceptor;
  235. }
  236. /**
  237. * @return String containing list of messages
  238. */
  239. public String toString() {
  240. if (0 == messages.size()) {
  241. return "MessageHandler: no messages";
  242. } else {
  243. return "MessageHandler: " + messages;
  244. }
  245. }
  246. }