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

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