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.

MessageUtil.java 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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 v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.bridge;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import java.io.OutputStream;
  17. import java.io.PrintStream;
  18. import java.io.PrintWriter;
  19. import java.io.StringWriter;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import org.aspectj.bridge.IMessage.Kind;
  27. import org.aspectj.util.LangUtil;
  28. /**
  29. * Convenience API's for constructing, printing, and sending messages.
  30. */
  31. public class MessageUtil {
  32. // ------ some constant, content-less messages
  33. // no variants for "info" or "debug", which should always have content
  34. public static final IMessage ABORT_NOTHING_TO_RUN = new Message("aborting - nothing to run", IMessage.ABORT, null, null);
  35. public static final IMessage FAIL_INCOMPLETE = new Message("run not completed", IMessage.FAIL, null, null);
  36. public static final IMessage ABORT_NOMESSAGE = new Message("", IMessage.ABORT, null, null);
  37. public static final IMessage FAIL_NOMESSAGE = new Message("", IMessage.FAIL, null, null);
  38. public static final IMessage ERROR_NOMESSAGE = new Message("", IMessage.ERROR, null, null);
  39. public static final IMessage WARNING_NOMESSAGE = new Message("", IMessage.WARNING, null, null);
  40. /** handle abort message (ignored if handler is null) */
  41. public static boolean abort(IMessageHandler handler, String message) {
  42. return ((null != handler) && handler.handleMessage(abort(message)));
  43. }
  44. /** create and handle exception message (ignored if handler is null) */
  45. public static boolean abort(IMessageHandler handler, String message, Throwable t) {
  46. if (handler != null) {
  47. return handler.handleMessage(abort(message, t));
  48. }
  49. return false;
  50. }
  51. /** create and handle fail message (ignored if handler is null) */
  52. public static boolean fail(IMessageHandler handler, String message) {
  53. return ((null != handler) && handler.handleMessage(fail(message)));
  54. }
  55. // /** create and handle fail message from reader (ignored if handler is null) */
  56. // public static boolean fail(IMessageHandler handler, String message, LineReader reader) {
  57. // return ((null != handler)
  58. // && handler.handleMessage(fail(message, reader)));
  59. // }
  60. /** create and handle fail message (ignored if handler is null) */
  61. public static boolean fail(IMessageHandler handler, String message, Throwable thrown) {
  62. return ((null != handler) && handler.handleMessage(fail(message, thrown)));
  63. }
  64. /** create and handle error message (ignored if handler is null) */
  65. public static boolean error(IMessageHandler handler, String message) {
  66. return ((null != handler) && handler.handleMessage(error(message)));
  67. }
  68. /** create and handle warn message (ignored if handler is null) */
  69. public static boolean warn(IMessageHandler handler, String message) {
  70. return ((null != handler) && handler.handleMessage(warn(message)));
  71. }
  72. /** create and handle debug message (ignored if handler is null) */
  73. public static boolean debug(IMessageHandler handler, String message) {
  74. return ((null != handler) && handler.handleMessage(debug(message)));
  75. }
  76. /** create and handle info message (ignored if handler is null) */
  77. public static boolean info(IMessageHandler handler, String message) {
  78. return ((null != handler) && handler.handleMessage(info(message)));
  79. }
  80. /** @return ABORT_NOMESSAGE if message is empty or IMessage otherwise */
  81. //
  82. public static IMessage abort(String message) {
  83. if (LangUtil.isEmpty(message)) {
  84. return ABORT_NOMESSAGE;
  85. } else {
  86. return new Message(message, IMessage.ABORT, null, null);
  87. }
  88. }
  89. /**
  90. * @return abort IMessage with thrown and message o ABORT_NOMESSAGE if both are empty/null
  91. */
  92. //
  93. public static IMessage abort(String message, Throwable thrown) {
  94. if (!LangUtil.isEmpty(message)) {
  95. return new Message(message, IMessage.ABORT, thrown, null);
  96. } else if (null == thrown) {
  97. return ABORT_NOMESSAGE;
  98. } else {
  99. return new Message(thrown.getMessage(), IMessage.ABORT, thrown, null);
  100. }
  101. }
  102. /** @return FAIL_NOMESSAGE if message is empty or IMessage otherwise */
  103. public static IMessage fail(String message) {
  104. if (LangUtil.isEmpty(message)) {
  105. return FAIL_NOMESSAGE;
  106. } else {
  107. return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY);
  108. // return fail(message, (LineReader) null);
  109. }
  110. }
  111. /**
  112. * Create fail message. If message is empty but thrown is not, use thrown.getMessage() as the message. If message is empty and
  113. * thrown is null, return FAIL_NOMESSAGE.
  114. *
  115. * @return FAIL_NOMESSAGE if thrown is null and message is empty or IMessage FAIL with message and thrown otherwise
  116. */
  117. public static IMessage fail(String message, Throwable thrown) {
  118. if (LangUtil.isEmpty(message)) {
  119. if (null == thrown) {
  120. return FAIL_NOMESSAGE;
  121. } else {
  122. return new Message(thrown.getMessage(), IMessage.FAIL, thrown, null);
  123. }
  124. } else {
  125. return new Message(message, IMessage.FAIL, thrown, null);
  126. }
  127. }
  128. /**
  129. * @return IMessage with IMessage.Kind FAIL and message as text and soure location from reader
  130. */
  131. // public static IMessage fail(String message) {//, LineReader reader) {
  132. // ISourceLocation loc = null;
  133. // if (null == reader) {
  134. // loc = ISourceLocation.EMPTY;
  135. // } else {
  136. // int line = reader.getLineNumber();
  137. // if (0 < line) {
  138. // line = 0;
  139. // }
  140. // loc = new SourceLocation(reader.getFile(), line, line, 0);
  141. // }
  142. // return new Message(message, IMessage.FAIL, null, ISourceLocation.EMPTY);
  143. // }
  144. /** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */
  145. //
  146. public static IMessage error(String message, ISourceLocation location) {
  147. if (LangUtil.isEmpty(message)) {
  148. return ERROR_NOMESSAGE;
  149. } else {
  150. return new Message(message, IMessage.ERROR, null, location);
  151. }
  152. }
  153. /** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */
  154. //
  155. public static IMessage warn(String message, ISourceLocation location) {
  156. if (LangUtil.isEmpty(message)) {
  157. return WARNING_NOMESSAGE;
  158. } else {
  159. return new Message(message, IMessage.WARNING, null, location);
  160. }
  161. }
  162. /** @return ERROR_NOMESSAGE if message is empty or IMessage otherwise */
  163. //
  164. public static IMessage error(String message) {
  165. if (LangUtil.isEmpty(message)) {
  166. return ERROR_NOMESSAGE;
  167. } else {
  168. return new Message(message, IMessage.ERROR, null, null);
  169. }
  170. }
  171. /** @return WARNING_NOMESSAGE if message is empty or IMessage otherwise */
  172. //
  173. public static IMessage warn(String message) {
  174. if (LangUtil.isEmpty(message)) {
  175. return WARNING_NOMESSAGE;
  176. } else {
  177. return new Message(message, IMessage.WARNING, null, null);
  178. }
  179. }
  180. /** @return IMessage.DEBUG message with message content */
  181. public static IMessage debug(String message) {
  182. return new Message(message, IMessage.DEBUG, null, null);
  183. }
  184. /** @return IMessage.INFO message with message content */
  185. public static IMessage info(String message) {
  186. return new Message(message, IMessage.INFO, null, null);
  187. }
  188. // /** @return ISourceLocation with the current File/line of the reader */
  189. // public static ISourceLocation makeSourceLocation(LineReader reader) {
  190. // LangUtil.throwIaxIfNull(reader, "reader");
  191. //
  192. // int line = reader.getLineNumber();
  193. // if (0 < line) {
  194. // line = 0;
  195. // }
  196. // return new SourceLocation(reader.getFile(), line, line, 0);
  197. // }
  198. // ------------------------ printing messages
  199. /**
  200. * Print total counts message to the print stream, starting each on a new line
  201. *
  202. * @param messageHolder
  203. * @param out
  204. */
  205. public static void printMessageCounts(PrintStream out, IMessageHolder messageHolder) {
  206. if ((null == out) || (null == messageHolder)) {
  207. return;
  208. }
  209. printMessageCounts(out, messageHolder, "");
  210. }
  211. public static void printMessageCounts(PrintStream out, IMessageHolder holder, String prefix) {
  212. out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder));
  213. }
  214. /**
  215. * Print all message to the print stream, starting each on a new line
  216. *
  217. * @param messageHolder
  218. * @param out
  219. * @see #print(PrintStream, IMessageHolder, String, IMessageRenderer, IMessageHandler)
  220. */
  221. public static void print(PrintStream out, IMessageHolder messageHolder) {
  222. print(out, messageHolder, (String) null, (IMessageRenderer) null, (IMessageHandler) null);
  223. }
  224. /**
  225. * Print all message to the print stream, starting each on a new line, with a prefix.
  226. *
  227. * @param holder
  228. * @param out
  229. * @see #print(PrintStream, IMessageHolder, String, IMessageRenderer, IMessageHandler)
  230. */
  231. public static void print(PrintStream out, IMessageHolder holder, String prefix) {
  232. print(out, holder, prefix, (IMessageRenderer) null, (IMessageHandler) null);
  233. }
  234. /**
  235. * Print all message to the print stream, starting each on a new line, with a prefix and using a renderer.
  236. *
  237. * @param holder
  238. * @param out
  239. * @param renderer IMessageRender to render result - use MESSAGE_LINE if null
  240. * @see #print(PrintStream, IMessageHolder, String, IMessageRenderer, IMessageHandler)
  241. */
  242. public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer) {
  243. print(out, holder, prefix, renderer, (IMessageHandler) null);
  244. }
  245. /**
  246. * Print all message to the print stream, starting each on a new line, with a prefix and using a renderer. The first line
  247. * renders a summary: {prefix}MessageHolder: {summary} Each message line has the following form:
  248. *
  249. * <pre>
  250. * {prefix}[{kind} {index}]: {rendering}
  251. * </pre>
  252. *
  253. * (where "{index}" (length 3) is the position within the set of like-kinded messages, ignoring selector omissions. Renderers
  254. * are free to render multi-line output.
  255. *
  256. * @param out the PrintStream sink - return silently if null
  257. * @param holder the IMessageHolder with the messages to print
  258. * @param prefix the prefix for each line
  259. * @param renderer IMessageRender to render result - use MESSAGE_ALL if null
  260. * @param selector IMessageHandler to select messages to render - if null, do all non-null
  261. */
  262. public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer,
  263. IMessageHandler selector) {
  264. print(out, holder, prefix, renderer, selector, true);
  265. }
  266. public static void print(PrintStream out, IMessageHolder holder, String prefix, IMessageRenderer renderer,
  267. IMessageHandler selector, boolean printSummary) {
  268. if ((null == out) || (null == holder)) {
  269. return;
  270. }
  271. if (null == renderer) {
  272. renderer = MESSAGE_ALL;
  273. }
  274. if (null == selector) {
  275. selector = PICK_ALL;
  276. }
  277. if (printSummary) {
  278. out.println(prefix + "MessageHolder: " + MessageUtil.renderCounts(holder));
  279. }
  280. for (IMessage.Kind kind : IMessage.KINDS) {
  281. if (!selector.isIgnoring(kind)) {
  282. IMessage[] messages = holder.getMessages(kind, IMessageHolder.EQUAL);
  283. for (int i = 0; i < messages.length; i++) {
  284. if (selector.handleMessage(messages[i])) {
  285. String label = (null == prefix ? "" : prefix + "[" + kind + " " + LangUtil.toSizedString(i, 3) + "]: ");
  286. out.println(label + renderer.renderToString(messages[i]));
  287. }
  288. }
  289. }
  290. }
  291. }
  292. public static String toShortString(IMessage message) {
  293. if (null == message) {
  294. return "null";
  295. }
  296. String m = message.getMessage();
  297. Throwable t = message.getThrown();
  298. return (message.getKind() + (null == m ? "" : ": " + m) + (null == t ? "" : ": " + LangUtil.unqualifiedClassName(t)));
  299. }
  300. /** @return int number of message of this kind (optionally or greater) in list */
  301. public static int numMessages(List<IMessage> messages, Kind kind, boolean orGreater) {
  302. if (LangUtil.isEmpty(messages)) {
  303. return 0;
  304. }
  305. IMessageHandler selector = makeSelector(kind, orGreater, null);
  306. IMessage[] result = visitMessages(messages, selector, true, false);
  307. return result.length;
  308. }
  309. /**
  310. * Select all messages in holder except those of the same kind (optionally or greater). If kind is null, then all messages are
  311. * rejected, so an empty list is returned.
  312. *
  313. * @return unmodifiable list of specified IMessage
  314. */
  315. public static IMessage[] getMessagesExcept(IMessageHolder holder, final IMessage.Kind kind, final boolean orGreater) {
  316. if ((null == holder) || (null == kind)) {
  317. return IMessage.RA_IMessage;
  318. }
  319. IMessageHandler selector = new IMessageHandler() {
  320. public boolean handleMessage(IMessage message) {
  321. IMessage.Kind test = message.getKind();
  322. return (!(orGreater ? kind.isSameOrLessThan(test) : kind == test));
  323. }
  324. public boolean isIgnoring(Kind kind) {
  325. return false;
  326. }
  327. public void dontIgnore(IMessage.Kind kind) {
  328. }
  329. public void ignore(Kind kind) {
  330. }
  331. };
  332. return visitMessages(holder, selector, true, false);
  333. }
  334. /** @return unmodifiable list of IMessage complying with parameters */
  335. public static List<IMessage> getMessages(IMessageHolder holder, IMessage.Kind kind, boolean orGreater, String infix) {
  336. if (null == holder) {
  337. return Collections.emptyList();
  338. }
  339. if ((null == kind) && LangUtil.isEmpty(infix)) {
  340. return holder.getUnmodifiableListView();
  341. }
  342. IMessageHandler selector = makeSelector(kind, orGreater, infix);
  343. IMessage[] messages = visitMessages(holder, selector, true, false);
  344. if (LangUtil.isEmpty(messages)) {
  345. return Collections.emptyList();
  346. }
  347. return Collections.unmodifiableList(Arrays.asList(messages));
  348. }
  349. /**
  350. * Extract messages of type kind from the input list.
  351. *
  352. * @param messages if null, return EMPTY_LIST
  353. * @param kind if null, return messages
  354. * @see MessageHandler#getMessages(Kind, boolean)
  355. */
  356. public static List<IMessage> getMessages(List<IMessage> messages, IMessage.Kind kind) {
  357. if (null == messages) {
  358. return Collections.emptyList();
  359. }
  360. if (null == kind) {
  361. return messages;
  362. }
  363. List<IMessage> result = new ArrayList<>();
  364. for (IMessage message : messages) {
  365. if (kind == message.getKind()) {
  366. result.add(message);
  367. }
  368. }
  369. if (0 == result.size()) {
  370. return Collections.emptyList();
  371. }
  372. return result;
  373. }
  374. /**
  375. * Map to the kind of messages associated with this string key.
  376. *
  377. * @param kind the String representing the kind of message (IMessage.Kind.toString())
  378. * @return Kind the associated IMessage.Kind, or null if not found
  379. */
  380. public static IMessage.Kind getKind(String kind) {
  381. if (null != kind) {
  382. kind = kind.toLowerCase();
  383. for (IMessage.Kind k : IMessage.KINDS) {
  384. if (kind.equals(k.toString())) {
  385. return k;
  386. }
  387. }
  388. }
  389. return null;
  390. }
  391. /**
  392. * Run visitor over the set of messages in holder, optionally accumulating those accepted by the visitor
  393. */
  394. public static IMessage[] visitMessages(IMessageHolder holder, IMessageHandler visitor, boolean accumulate, boolean abortOnFail) {
  395. if (null == holder) {
  396. return IMessage.RA_IMessage;
  397. } else {
  398. return visitMessages(holder.getUnmodifiableListView(), visitor, accumulate, abortOnFail);
  399. }
  400. }
  401. /**
  402. * Run visitor over the set of messages in holder, optionally accumulating those accepted by the visitor
  403. */
  404. public static IMessage[] visitMessages(IMessage[] messages, IMessageHandler visitor, boolean accumulate, boolean abortOnFail) {
  405. if (LangUtil.isEmpty(messages)) {
  406. return IMessage.RA_IMessage;
  407. } else {
  408. return visitMessages(Arrays.asList(messages), visitor, accumulate, abortOnFail);
  409. }
  410. }
  411. /**
  412. * Run visitor over a collection of messages, optionally accumulating those accepted by the visitor
  413. *
  414. * @param messages if null or empty, return IMessage.RA_IMessage
  415. * @param visitor run visitor.handleMessage(message) on each message - if null and messages not empty, IllegalArgumentException
  416. * @param accumulate if true, then return accepted IMessage[]
  417. * @param abortOnFail if true and visitor returns false, stop visiting
  418. * @return IMessage.RA_IMessage if collection is empty, if not accumulate, or if visitor accepted no IMessage, or IMessage[] of
  419. * accepted messages otherwise
  420. * @throws IllegalArgumentException if any in collection are not instanceof IMessage
  421. */
  422. public static IMessage[] visitMessages(Collection<IMessage> messages, IMessageHandler visitor, final boolean accumulate,
  423. final boolean abortOnFail) {
  424. if (LangUtil.isEmpty(messages)) {
  425. return IMessage.RA_IMessage;
  426. }
  427. LangUtil.throwIaxIfNull(visitor, "visitor");
  428. ArrayList<IMessage> result = (accumulate ? new ArrayList<>() : null);
  429. for (IMessage m : messages) {
  430. if (visitor.handleMessage(m)) {
  431. if (accumulate) {
  432. result.add(m);
  433. }
  434. } else if (abortOnFail) {
  435. break;
  436. }
  437. }
  438. if (!accumulate || (0 == result.size())) {
  439. return IMessage.RA_IMessage;
  440. } else {
  441. return result.toArray(IMessage.RA_IMessage);
  442. }
  443. }
  444. /**
  445. * Make an IMessageHandler that handles IMessage if they have the right kind (or greater) and contain some infix String.
  446. *
  447. * @param kind the IMessage.Kind required of the message
  448. * @param orGreater if true, also accept messages with greater kinds, as defined by IMessage.Kind.COMPARATOR
  449. * @param infix the String text to require in the message - may be null or empty to accept any message with the specified kind.
  450. * @return IMessageHandler selector that works to param specs
  451. */
  452. public static IMessageHandler makeSelector(IMessage.Kind kind, boolean orGreater, String infix) {
  453. if (!orGreater && LangUtil.isEmpty(infix)) {
  454. if (kind == IMessage.ABORT) {
  455. return PICK_ABORT;
  456. } else if (kind == IMessage.DEBUG) {
  457. return PICK_DEBUG;
  458. } else if (kind == IMessage.DEBUG) {
  459. return PICK_DEBUG;
  460. } else if (kind == IMessage.ERROR) {
  461. return PICK_ERROR;
  462. } else if (kind == IMessage.FAIL) {
  463. return PICK_FAIL;
  464. } else if (kind == IMessage.INFO) {
  465. return PICK_INFO;
  466. } else if (kind == IMessage.WARNING) {
  467. return PICK_WARNING;
  468. }
  469. }
  470. return new KindSelector(kind, orGreater, infix);
  471. }
  472. // ------------------ visitors to select messages
  473. public static final IMessageHandler PICK_ALL = new KindSelector((IMessage.Kind) null);
  474. public static final IMessageHandler PICK_ABORT = new KindSelector(IMessage.ABORT);
  475. public static final IMessageHandler PICK_DEBUG = new KindSelector(IMessage.DEBUG);
  476. public static final IMessageHandler PICK_ERROR = new KindSelector(IMessage.ERROR);
  477. public static final IMessageHandler PICK_FAIL = new KindSelector(IMessage.FAIL);
  478. public static final IMessageHandler PICK_INFO = new KindSelector(IMessage.INFO);
  479. public static final IMessageHandler PICK_WARNING = new KindSelector(IMessage.WARNING);
  480. public static final IMessageHandler PICK_ABORT_PLUS = new KindSelector(IMessage.ABORT, true);
  481. public static final IMessageHandler PICK_DEBUG_PLUS = new KindSelector(IMessage.DEBUG, true);
  482. public static final IMessageHandler PICK_ERROR_PLUS = new KindSelector(IMessage.ERROR, true);
  483. public static final IMessageHandler PICK_FAIL_PLUS = new KindSelector(IMessage.FAIL, true);
  484. public static final IMessageHandler PICK_INFO_PLUS = new KindSelector(IMessage.INFO, true);
  485. public static final IMessageHandler PICK_WARNING_PLUS = new KindSelector(IMessage.WARNING, true);
  486. /** implementation for PICK_... constants */
  487. private static class KindSelector implements IMessageHandler {
  488. final IMessage.Kind sought;
  489. final boolean floor;
  490. final String infix;
  491. KindSelector(IMessage.Kind sought) {
  492. this(sought, false);
  493. }
  494. KindSelector(IMessage.Kind sought, boolean floor) {
  495. this(sought, floor, null);
  496. }
  497. KindSelector(IMessage.Kind sought, boolean floor, String infix) {
  498. this.sought = sought;
  499. this.floor = floor;
  500. this.infix = (LangUtil.isEmpty(infix) ? null : infix);
  501. }
  502. /**
  503. * @return false if this message is null, of true if we seek any kind (null) or if this has the exact kind we seek and this
  504. * has any text sought
  505. */
  506. public boolean handleMessage(IMessage message) {
  507. return ((null != message) && !isIgnoring(message.getKind()) && textIn(message));
  508. }
  509. /** @return true if handleMessage would return false for a message of this kind */
  510. public boolean isIgnoring(IMessage.Kind kind) {
  511. if (!floor) {
  512. return ((null != sought) && (sought != kind));
  513. } else if (null == sought) {
  514. return false;
  515. } else {
  516. return (0 < IMessage.Kind.COMPARATOR.compare(sought, kind));
  517. }
  518. }
  519. public void dontIgnore(IMessage.Kind kind) {
  520. }
  521. private boolean textIn(IMessage message) {
  522. if (null == infix) {
  523. return true;
  524. }
  525. String text = message.getMessage();
  526. return (text.contains(infix));
  527. }
  528. public void ignore(Kind kind) {
  529. }
  530. }
  531. // ------------------ components to render messages
  532. /** parameterize rendering behavior for messages */
  533. public interface IMessageRenderer {
  534. String renderToString(IMessage message);
  535. }
  536. /** render message more verbosely if it is worse */
  537. public static final IMessageRenderer MESSAGE_SCALED = new IMessageRenderer() {
  538. public String toString() {
  539. return "MESSAGE_SCALED";
  540. }
  541. public String renderToString(IMessage message) {
  542. if (null == message) {
  543. return "((IMessage) null)";
  544. }
  545. IMessage.Kind kind = message.getKind();
  546. int level = 3;
  547. if ((kind == IMessage.ABORT) || (kind == IMessage.FAIL)) {
  548. level = 1;
  549. } else if ((kind == IMessage.ERROR) || (kind == IMessage.WARNING)) {
  550. level = 2;
  551. } else {
  552. level = 3;
  553. }
  554. String result = null;
  555. switch (level) {
  556. case (1):
  557. result = MESSAGE_TOSTRING.renderToString(message);
  558. break;
  559. case (2):
  560. result = MESSAGE_LINE.renderToString(message);
  561. break;
  562. case (3):
  563. result = MESSAGE_SHORT.renderToString(message);
  564. break;
  565. }
  566. Throwable thrown = message.getThrown();
  567. if (null != thrown) {
  568. if (level == 3) {
  569. result += "Thrown: \n" + LangUtil.renderExceptionShort(thrown);
  570. } else {
  571. result += "Thrown: \n" + LangUtil.renderException(thrown);
  572. }
  573. }
  574. return result;
  575. }
  576. };
  577. /** render message as label, i.e., less than 33 char */
  578. public static final IMessageRenderer MESSAGE_LABEL = new IMessageRenderer() {
  579. public String toString() {
  580. return "MESSAGE_LABEL";
  581. }
  582. public String renderToString(IMessage message) {
  583. if (null == message) {
  584. return "((IMessage) null)";
  585. }
  586. return renderMessageLine(message, 5, 5, 32);
  587. }
  588. };
  589. /** render message as label, i.e., less than 33 char, with no source location */
  590. public static final IMessageRenderer MESSAGE_LABEL_NOLOC = new IMessageRenderer() {
  591. public String toString() {
  592. return "MESSAGE_LABEL_NOLOC";
  593. }
  594. public String renderToString(IMessage message) {
  595. if (null == message) {
  596. return "((IMessage) null)";
  597. }
  598. return renderMessageLine(message, 10, 0, 32);
  599. }
  600. };
  601. /** render message as line, i.e., less than 75 char, no internal line sep */
  602. public static final IMessageRenderer MESSAGE_LINE = new IMessageRenderer() {
  603. public String toString() {
  604. return "MESSAGE_LINE";
  605. }
  606. public String renderToString(IMessage message) {
  607. if (null == message) {
  608. return "((IMessage) null)";
  609. }
  610. return renderMessageLine(message, 8, 2, 74);
  611. }
  612. };
  613. /**
  614. * render message as line, i.e., less than 75 char, no internal line sep, trying to trim text as needed to end with a full
  615. * source location
  616. */
  617. public static final IMessageRenderer MESSAGE_LINE_FORCE_LOC = new IMessageRenderer() {
  618. public String toString() {
  619. return "MESSAGE_LINE_FORCE_LOC";
  620. }
  621. public String renderToString(IMessage message) {
  622. if (null == message) {
  623. return "((IMessage) null)";
  624. }
  625. return renderMessageLine(message, 2, 40, 74);
  626. }
  627. };
  628. /** render message without restriction, up to 10K, including throwable */
  629. public static final IMessageRenderer MESSAGE_ALL = new IMessageRenderer() {
  630. public String toString() {
  631. return "MESSAGE_ALL";
  632. }
  633. public String renderToString(IMessage message) {
  634. return renderMessage(message);
  635. }
  636. };
  637. // /** render message without restriction, up to 10K, including (but eliding) throwable */
  638. // public static final IMessageRenderer MESSAGE_ALL_ELIDED= new IMessageRenderer() {
  639. // public String toString() { return "MESSAGE_ALL_ELIDED"; }
  640. // public String renderToString(IMessage message) {
  641. // return renderMessage(message, true);
  642. // }
  643. // };
  644. /** render message without restriction, except any Throwable thrown */
  645. public static final IMessageRenderer MESSAGE_MOST = new IMessageRenderer() {
  646. public String toString() {
  647. return "MESSAGE_MOST";
  648. }
  649. public String renderToString(IMessage message) {
  650. if (null == message) {
  651. return "((IMessage) null)";
  652. }
  653. return renderMessageLine(message, 1, 1, 10000);
  654. }
  655. };
  656. /**
  657. * render message as wide line, i.e., less than 256 char, no internal line sep, except any Throwable thrown
  658. */
  659. public static final IMessageRenderer MESSAGE_WIDELINE = new IMessageRenderer() {
  660. public String toString() {
  661. return "MESSAGE_WIDELINE";
  662. }
  663. public String renderToString(IMessage message) {
  664. if (null == message) {
  665. return "((IMessage) null)";
  666. }
  667. return renderMessageLine(message, 8, 2, 255); // XXX revert to 256
  668. }
  669. };
  670. /** render message using its toString() or "((IMessage) null)" */
  671. public static final IMessageRenderer MESSAGE_TOSTRING = new IMessageRenderer() {
  672. public String toString() {
  673. return "MESSAGE_TOSTRING";
  674. }
  675. public String renderToString(IMessage message) {
  676. if (null == message) {
  677. return "((IMessage) null)";
  678. }
  679. return message.toString();
  680. }
  681. };
  682. /** render message using toShortString(IMessage)" */
  683. public static final IMessageRenderer MESSAGE_SHORT = new IMessageRenderer() {
  684. public String toString() {
  685. return "MESSAGE_SHORT";
  686. }
  687. public String renderToString(IMessage message) {
  688. return toShortString(message);
  689. }
  690. };
  691. /**
  692. * This renders IMessage as String, ignoring empty elements and eliding any thrown stack traces.
  693. *
  694. * @return "((IMessage) null)" if null or String rendering otherwise, including everything (esp. throwable stack trace)
  695. * @see renderSourceLocation(ISourceLocation loc)
  696. */
  697. public static String renderMessage(IMessage message) {
  698. return renderMessage(message, true);
  699. }
  700. /**
  701. * This renders IMessage as String, ignoring empty elements and eliding any thrown.
  702. *
  703. * @return "((IMessage) null)" if null or String rendering otherwise, including everything (esp. throwable stack trace)
  704. * @see renderSourceLocation(ISourceLocation loc)
  705. */
  706. public static String renderMessage(IMessage message, boolean elide) {
  707. if (null == message) {
  708. return "((IMessage) null)";
  709. }
  710. String result = message.getKind().toString();
  711. ISourceLocation loc = message.getSourceLocation();
  712. if (loc != null) {
  713. String context = loc.getContext();
  714. result += context == null || context.trim().isEmpty() ? " at " : " at\n";
  715. result += loc;
  716. }
  717. result += " " + message.getMessage();
  718. Throwable thrown = message.getThrown();
  719. if (thrown != null) {
  720. result += " -- " + LangUtil.renderExceptionShort(thrown);
  721. result += "\n" + LangUtil.renderException(thrown, elide);
  722. }
  723. if (message.getExtraSourceLocations().isEmpty()) {
  724. return result;
  725. } else {
  726. return addExtraSourceLocations(message, result);
  727. }
  728. }
  729. public static String addExtraSourceLocations(IMessage message, String baseMessage) {
  730. StringWriter buf = new StringWriter();
  731. PrintWriter writer = new PrintWriter(buf);
  732. writer.println(baseMessage);
  733. for (Iterator<ISourceLocation> iter = message.getExtraSourceLocations().iterator(); iter.hasNext();) {
  734. ISourceLocation element = iter.next();
  735. if (element != null) {
  736. writer.print("\tsee also: " + element.toString());
  737. if (iter.hasNext()) {
  738. writer.println();
  739. }
  740. }
  741. }
  742. try {
  743. buf.close();
  744. } catch (IOException ioe) {
  745. }
  746. return buf.getBuffer().toString();
  747. }
  748. /**
  749. * Render ISourceLocation to String, ignoring empty elements (null or ISourceLocation.NO_FILE or ISourceLocation.NO_COLUMN
  750. * (though implementations may return 0 from getColumn() when passed NO_COLUMN as input)).
  751. *
  752. * @return "((ISourceLocation) null)" if null or String rendering
  753. *
  754. * <pre>
  755. * {file:}line{:column}
  756. * </pre>
  757. *
  758. */
  759. public static String renderSourceLocation(ISourceLocation loc) {
  760. if (null == loc) {
  761. return "((ISourceLocation) null)";
  762. }
  763. StringBuilder sb = new StringBuilder();
  764. File sourceFile = loc.getSourceFile();
  765. if (sourceFile != ISourceLocation.NO_FILE) {
  766. sb.append(sourceFile.getPath());
  767. sb.append(":");
  768. }
  769. int line = loc.getLine();
  770. sb.append("" + line);
  771. int column = loc.getColumn();
  772. if (column != ISourceLocation.NO_COLUMN) {
  773. sb.append(":" + column);
  774. }
  775. return sb.toString();
  776. }
  777. /**
  778. * Render message in a line. IMessage.Kind is always printed, then any unqualified exception class, then the remainder of text
  779. * and location according to their relative scale, all to fit in max characters or less. This does not render thrown except for
  780. * the unqualified class name
  781. *
  782. * @param max the number of characters - forced to 32..10000
  783. * @param textScale relative proportion to spend on message and/or exception message, relative to source location - if 0,
  784. * message is suppressed
  785. * @param locScale relative proportion to spend on source location suppressed if 0
  786. * @return "((IMessage) null)" or message per spec
  787. */
  788. public static String renderMessageLine(IMessage message, int textScale, int locScale, int max) {
  789. if (null == message) {
  790. return "((IMessage) null)";
  791. }
  792. if (max < 32) {
  793. max = 32;
  794. } else if (max > 10000) {
  795. max = 10000;
  796. }
  797. if (0 > textScale) {
  798. textScale = -textScale;
  799. }
  800. if (0 > locScale) {
  801. locScale = -locScale;
  802. }
  803. String text = message.getMessage();
  804. Throwable thrown = message.getThrown();
  805. ISourceLocation sl = message.getSourceLocation();
  806. IMessage.Kind kind = message.getKind();
  807. StringBuilder result = new StringBuilder();
  808. result.append(kind.toString());
  809. result.append(": ");
  810. if (null != thrown) {
  811. result.append(LangUtil.unqualifiedClassName(thrown) + " ");
  812. if ((null == text) || ("".equals(text))) {
  813. text = thrown.getMessage();
  814. }
  815. }
  816. if (0 == textScale) {
  817. text = "";
  818. } else if ((null != text) && (null != thrown)) {
  819. // decide between message and exception text?
  820. String s = thrown.getMessage();
  821. if ((null != s) && (0 < s.length())) {
  822. text += " - " + s;
  823. }
  824. }
  825. String loc = "";
  826. if ((0 != locScale) && (null != sl)) {
  827. File f = sl.getSourceFile();
  828. if (f == ISourceLocation.NO_FILE) {
  829. f = null;
  830. }
  831. if (null != f) {
  832. loc = f.getName();
  833. }
  834. int line = sl.getLine();
  835. int col = sl.getColumn();
  836. int end = sl.getEndLine();
  837. if ((0 == line) && (0 == col) && (0 == end)) {
  838. // ignore numbers if default
  839. } else {
  840. loc += ":" + line + (col == 0 ? "" : ":" + col);
  841. if (line != end) { // XXX consider suppressing nonstandard...
  842. loc += ":" + end;
  843. }
  844. }
  845. if (!LangUtil.isEmpty(loc)) {
  846. loc = "@[" + loc; // matching "]" added below after clipping
  847. }
  848. }
  849. // now budget between text and loc
  850. float totalScale = locScale + textScale;
  851. float remainder = max - result.length() - 4;
  852. if ((remainder > 0) && (0 < totalScale)) {
  853. int textSize = (int) (remainder * textScale / totalScale);
  854. int locSize = (int) (remainder * locScale / totalScale);
  855. // adjust for underutilization
  856. int extra = locSize - loc.length();
  857. if (0 < extra) {
  858. locSize = loc.length();
  859. textSize += extra;
  860. }
  861. extra = textSize - text.length();
  862. if (0 < extra) {
  863. textSize = text.length();
  864. if (locSize < loc.length()) {
  865. locSize += extra;
  866. }
  867. }
  868. if (locSize > loc.length()) {
  869. locSize = loc.length();
  870. }
  871. if (textSize > text.length()) {
  872. textSize = text.length();
  873. }
  874. if (0 < textSize) {
  875. result.append(text.substring(0, textSize));
  876. }
  877. if (0 < locSize) {
  878. if (0 < textSize) {
  879. result.append(" ");
  880. }
  881. result.append(loc.substring(0, locSize) + "]");
  882. }
  883. }
  884. return result.toString();
  885. }
  886. /** @return String of the form "{(# {type}) }.." for message kinds, skipping 0 */
  887. public static String renderCounts(IMessageHolder holder) {
  888. if (0 == holder.numMessages(null, false)) {
  889. return "(0 messages)";
  890. }
  891. StringBuilder sb = new StringBuilder();
  892. for (IMessage.Kind kind : IMessage.KINDS) {
  893. int num = holder.numMessages(kind, false);
  894. if (0 < num) {
  895. sb.append(" (" + num + " " + kind + ") ");
  896. }
  897. }
  898. return sb.toString();
  899. }
  900. /**
  901. * Factory for handler adapted to PrintStream XXX weak - only handles println(String)
  902. *
  903. * @param handler the IMessageHandler sink for the messages generated
  904. * @param kind the IMessage.Kind of message to create
  905. * @param overage the OuputStream for text not captured by the handler (if null, System.out used)
  906. * @throws IllegalArgumentException if kind or handler is null
  907. */
  908. public static PrintStream handlerPrintStream(final IMessageHandler handler, final IMessage.Kind kind,
  909. final OutputStream overage, final String prefix) {
  910. LangUtil.throwIaxIfNull(handler, "handler");
  911. LangUtil.throwIaxIfNull(kind, "kind");
  912. class HandlerPrintStream extends PrintStream {
  913. HandlerPrintStream() {
  914. super(null == overage ? System.out : overage);
  915. }
  916. public void println() {
  917. println("");
  918. }
  919. public void println(Object o) {
  920. println(null == o ? "null" : o.toString());
  921. }
  922. public void println(String input) {
  923. String textMessage = (null == prefix ? input : prefix + input);
  924. IMessage m = new Message(textMessage, kind, null, null);
  925. handler.handleMessage(m);
  926. }
  927. }
  928. return new HandlerPrintStream();
  929. }
  930. /** utility class */
  931. private MessageUtil() {
  932. }
  933. /**
  934. * Handle all messages in the second handler using the first
  935. *
  936. * @param sink the IMessageHandler sink for all messages in source
  937. * @param source the IMessageHolder source for all messages to handle
  938. * @param fastFail if true, stop on first failure
  939. * @return false if any sink.handleMessage(..) failed
  940. */
  941. public static boolean handleAll(IMessageHandler sink, IMessageHolder source, boolean fastFail) {
  942. return handleAll(sink, source, null, true, fastFail);
  943. }
  944. /**
  945. * Handle messages in the second handler using the first
  946. *
  947. * @param sink the IMessageHandler sink for all messages in source
  948. * @param source the IMessageHolder source for all messages to handle
  949. * @param kind the IMessage.Kind to select, if not null
  950. * @param orGreater if true, also accept greater kinds
  951. * @param fastFail if true, stop on first failure
  952. * @return false if any sink.handleMessage(..) failed
  953. */
  954. public static boolean handleAll(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater,
  955. boolean fastFail) {
  956. LangUtil.throwIaxIfNull(sink, "sink");
  957. LangUtil.throwIaxIfNull(source, "source");
  958. return handleAll(sink, source.getMessages(kind, orGreater), fastFail);
  959. }
  960. /**
  961. * Handle messages in the second handler using the first if they are NOT of this kind (optionally, or greater). If you pass null
  962. * as the kind, then all messages are ignored and this returns true.
  963. *
  964. * @param sink the IMessageHandler sink for all messages in source
  965. * @param source the IMessageHolder source for all messages to handle
  966. * @param kind the IMessage.Kind to reject, if not null
  967. * @param orGreater if true, also reject greater kinds
  968. * @param fastFail if true, stop on first failure
  969. * @return false if any sink.handleMessage(..) failed
  970. */
  971. public static boolean handleAllExcept(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater,
  972. boolean fastFail) {
  973. LangUtil.throwIaxIfNull(sink, "sink");
  974. LangUtil.throwIaxIfNull(source, "source");
  975. if (null == kind) {
  976. return true;
  977. }
  978. IMessage[] messages = getMessagesExcept(source, kind, orGreater);
  979. return handleAll(sink, messages, fastFail);
  980. }
  981. /**
  982. * Handle messages in the sink.
  983. *
  984. * @param sink the IMessageHandler sink for all messages in source
  985. * @param sources the IMessage[] messages to handle
  986. * @param fastFail if true, stop on first failure
  987. * @return false if any sink.handleMessage(..) failed
  988. * @throws IllegalArgumentException if sink is null
  989. */
  990. public static boolean handleAll(IMessageHandler sink, IMessage[] sources, boolean fastFail) {
  991. LangUtil.throwIaxIfNull(sink, "sink");
  992. if (LangUtil.isEmpty(sources)) {
  993. return true;
  994. }
  995. boolean result = true;
  996. for (IMessage source : sources) {
  997. if (!sink.handleMessage(source)) {
  998. if (fastFail) {
  999. return false;
  1000. }
  1001. if (result) {
  1002. result = false;
  1003. }
  1004. }
  1005. }
  1006. return result;
  1007. }
  1008. }