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 7.0KB

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