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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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 new IMessage[0];
  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. ISourceLocation loc = message.getSourceLocation();
  711. String locString = (null == loc ? "" : " at " + loc);
  712. String result = message.getKind() + locString + " " + message.getMessage();
  713. Throwable thrown = message.getThrown();
  714. if (thrown != null) {
  715. result += " -- " + LangUtil.renderExceptionShort(thrown);
  716. result += "\n" + LangUtil.renderException(thrown, elide);
  717. }
  718. if (message.getExtraSourceLocations().isEmpty()) {
  719. return result;
  720. } else {
  721. return addExtraSourceLocations(message, result);
  722. }
  723. }
  724. public static String addExtraSourceLocations(IMessage message, String baseMessage) {
  725. StringWriter buf = new StringWriter();
  726. PrintWriter writer = new PrintWriter(buf);
  727. writer.println(baseMessage);
  728. for (Iterator<ISourceLocation> iter = message.getExtraSourceLocations().iterator(); iter.hasNext();) {
  729. ISourceLocation element = iter.next();
  730. if (element != null) {
  731. writer.print("\tsee also: " + element.toString());
  732. if (iter.hasNext()) {
  733. writer.println();
  734. }
  735. }
  736. }
  737. try {
  738. buf.close();
  739. } catch (IOException ioe) {
  740. }
  741. return buf.getBuffer().toString();
  742. }
  743. /**
  744. * Render ISourceLocation to String, ignoring empty elements (null or ISourceLocation.NO_FILE or ISourceLocation.NO_COLUMN
  745. * (though implementations may return 0 from getColumn() when passed NO_COLUMN as input)).
  746. *
  747. * @return "((ISourceLocation) null)" if null or String rendering
  748. *
  749. * <pre>
  750. * {file:}line{:column}
  751. * </pre>
  752. *
  753. */
  754. public static String renderSourceLocation(ISourceLocation loc) {
  755. if (null == loc) {
  756. return "((ISourceLocation) null)";
  757. }
  758. StringBuilder sb = new StringBuilder();
  759. File sourceFile = loc.getSourceFile();
  760. if (sourceFile != ISourceLocation.NO_FILE) {
  761. sb.append(sourceFile.getPath());
  762. sb.append(":");
  763. }
  764. int line = loc.getLine();
  765. sb.append("" + line);
  766. int column = loc.getColumn();
  767. if (column != ISourceLocation.NO_COLUMN) {
  768. sb.append(":" + column);
  769. }
  770. return sb.toString();
  771. }
  772. /**
  773. * Render message in a line. IMessage.Kind is always printed, then any unqualified exception class, then the remainder of text
  774. * and location according to their relative scale, all to fit in max characters or less. This does not render thrown except for
  775. * the unqualified class name
  776. *
  777. * @param max the number of characters - forced to 32..10000
  778. * @param textScale relative proportion to spend on message and/or exception message, relative to source location - if 0,
  779. * message is suppressed
  780. * @param locScale relative proportion to spend on source location suppressed if 0
  781. * @return "((IMessage) null)" or message per spec
  782. */
  783. public static String renderMessageLine(IMessage message, int textScale, int locScale, int max) {
  784. if (null == message) {
  785. return "((IMessage) null)";
  786. }
  787. if (max < 32) {
  788. max = 32;
  789. } else if (max > 10000) {
  790. max = 10000;
  791. }
  792. if (0 > textScale) {
  793. textScale = -textScale;
  794. }
  795. if (0 > locScale) {
  796. locScale = -locScale;
  797. }
  798. String text = message.getMessage();
  799. Throwable thrown = message.getThrown();
  800. ISourceLocation sl = message.getSourceLocation();
  801. IMessage.Kind kind = message.getKind();
  802. StringBuilder result = new StringBuilder();
  803. result.append(kind.toString());
  804. result.append(": ");
  805. if (null != thrown) {
  806. result.append(LangUtil.unqualifiedClassName(thrown) + " ");
  807. if ((null == text) || ("".equals(text))) {
  808. text = thrown.getMessage();
  809. }
  810. }
  811. if (0 == textScale) {
  812. text = "";
  813. } else if ((null != text) && (null != thrown)) {
  814. // decide between message and exception text?
  815. String s = thrown.getMessage();
  816. if ((null != s) && (0 < s.length())) {
  817. text += " - " + s;
  818. }
  819. }
  820. String loc = "";
  821. if ((0 != locScale) && (null != sl)) {
  822. File f = sl.getSourceFile();
  823. if (f == ISourceLocation.NO_FILE) {
  824. f = null;
  825. }
  826. if (null != f) {
  827. loc = f.getName();
  828. }
  829. int line = sl.getLine();
  830. int col = sl.getColumn();
  831. int end = sl.getEndLine();
  832. if ((0 == line) && (0 == col) && (0 == end)) {
  833. // ignore numbers if default
  834. } else {
  835. loc += ":" + line + (col == 0 ? "" : ":" + col);
  836. if (line != end) { // XXX consider suppressing nonstandard...
  837. loc += ":" + end;
  838. }
  839. }
  840. if (!LangUtil.isEmpty(loc)) {
  841. loc = "@[" + loc; // matching "]" added below after clipping
  842. }
  843. }
  844. // now budget between text and loc
  845. float totalScale = locScale + textScale;
  846. float remainder = max - result.length() - 4;
  847. if ((remainder > 0) && (0 < totalScale)) {
  848. int textSize = (int) (remainder * textScale / totalScale);
  849. int locSize = (int) (remainder * locScale / totalScale);
  850. // adjust for underutilization
  851. int extra = locSize - loc.length();
  852. if (0 < extra) {
  853. locSize = loc.length();
  854. textSize += extra;
  855. }
  856. extra = textSize - text.length();
  857. if (0 < extra) {
  858. textSize = text.length();
  859. if (locSize < loc.length()) {
  860. locSize += extra;
  861. }
  862. }
  863. if (locSize > loc.length()) {
  864. locSize = loc.length();
  865. }
  866. if (textSize > text.length()) {
  867. textSize = text.length();
  868. }
  869. if (0 < textSize) {
  870. result.append(text.substring(0, textSize));
  871. }
  872. if (0 < locSize) {
  873. if (0 < textSize) {
  874. result.append(" ");
  875. }
  876. result.append(loc.substring(0, locSize) + "]");
  877. }
  878. }
  879. return result.toString();
  880. }
  881. /** @return String of the form "{(# {type}) }.." for message kinds, skipping 0 */
  882. public static String renderCounts(IMessageHolder holder) {
  883. if (0 == holder.numMessages(null, false)) {
  884. return "(0 messages)";
  885. }
  886. StringBuilder sb = new StringBuilder();
  887. for (IMessage.Kind kind : IMessage.KINDS) {
  888. int num = holder.numMessages(kind, false);
  889. if (0 < num) {
  890. sb.append(" (" + num + " " + kind + ") ");
  891. }
  892. }
  893. return sb.toString();
  894. }
  895. /**
  896. * Factory for handler adapted to PrintStream XXX weak - only handles println(String)
  897. *
  898. * @param handler the IMessageHandler sink for the messages generated
  899. * @param kind the IMessage.Kind of message to create
  900. * @param overage the OuputStream for text not captured by the handler (if null, System.out used)
  901. * @throws IllegalArgumentException if kind or handler is null
  902. */
  903. public static PrintStream handlerPrintStream(final IMessageHandler handler, final IMessage.Kind kind,
  904. final OutputStream overage, final String prefix) {
  905. LangUtil.throwIaxIfNull(handler, "handler");
  906. LangUtil.throwIaxIfNull(kind, "kind");
  907. class HandlerPrintStream extends PrintStream {
  908. HandlerPrintStream() {
  909. super(null == overage ? System.out : overage);
  910. }
  911. public void println() {
  912. println("");
  913. }
  914. public void println(Object o) {
  915. println(null == o ? "null" : o.toString());
  916. }
  917. public void println(String input) {
  918. String textMessage = (null == prefix ? input : prefix + input);
  919. IMessage m = new Message(textMessage, kind, null, null);
  920. handler.handleMessage(m);
  921. }
  922. }
  923. return new HandlerPrintStream();
  924. }
  925. /** utility class */
  926. private MessageUtil() {
  927. }
  928. /**
  929. * Handle all messages in the second handler using the first
  930. *
  931. * @param sink the IMessageHandler sink for all messages in source
  932. * @param source the IMessageHolder source for all messages to handle
  933. * @param fastFail if true, stop on first failure
  934. * @return false if any sink.handleMessage(..) failed
  935. */
  936. public static boolean handleAll(IMessageHandler sink, IMessageHolder source, boolean fastFail) {
  937. return handleAll(sink, source, null, true, fastFail);
  938. }
  939. /**
  940. * Handle messages in the second handler using the first
  941. *
  942. * @param sink the IMessageHandler sink for all messages in source
  943. * @param source the IMessageHolder source for all messages to handle
  944. * @param kind the IMessage.Kind to select, if not null
  945. * @param orGreater if true, also accept greater kinds
  946. * @param fastFail if true, stop on first failure
  947. * @return false if any sink.handleMessage(..) failed
  948. */
  949. public static boolean handleAll(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater,
  950. boolean fastFail) {
  951. LangUtil.throwIaxIfNull(sink, "sink");
  952. LangUtil.throwIaxIfNull(source, "source");
  953. return handleAll(sink, source.getMessages(kind, orGreater), fastFail);
  954. }
  955. /**
  956. * Handle messages in the second handler using the first if they are NOT of this kind (optionally, or greater). If you pass null
  957. * as the kind, then all messages are ignored and this returns true.
  958. *
  959. * @param sink the IMessageHandler sink for all messages in source
  960. * @param source the IMessageHolder source for all messages to handle
  961. * @param kind the IMessage.Kind to reject, if not null
  962. * @param orGreater if true, also reject greater kinds
  963. * @param fastFail if true, stop on first failure
  964. * @return false if any sink.handleMessage(..) failed
  965. */
  966. public static boolean handleAllExcept(IMessageHandler sink, IMessageHolder source, IMessage.Kind kind, boolean orGreater,
  967. boolean fastFail) {
  968. LangUtil.throwIaxIfNull(sink, "sink");
  969. LangUtil.throwIaxIfNull(source, "source");
  970. if (null == kind) {
  971. return true;
  972. }
  973. IMessage[] messages = getMessagesExcept(source, kind, orGreater);
  974. return handleAll(sink, messages, fastFail);
  975. }
  976. /**
  977. * Handle messages in the sink.
  978. *
  979. * @param sink the IMessageHandler sink for all messages in source
  980. * @param sources the IMessage[] messages to handle
  981. * @param fastFail if true, stop on first failure
  982. * @return false if any sink.handleMessage(..) failed
  983. * @throws IllegalArgumentException if sink is null
  984. */
  985. public static boolean handleAll(IMessageHandler sink, IMessage[] sources, boolean fastFail) {
  986. LangUtil.throwIaxIfNull(sink, "sink");
  987. if (LangUtil.isEmpty(sources)) {
  988. return true;
  989. }
  990. boolean result = true;
  991. for (IMessage source : sources) {
  992. if (!sink.handleMessage(source)) {
  993. if (fastFail) {
  994. return false;
  995. }
  996. if (result) {
  997. result = false;
  998. }
  999. }
  1000. }
  1001. return result;
  1002. }
  1003. }