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.

Main.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.tools.ajc;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.PrintStream;
  17. import java.util.Arrays;
  18. import java.util.Date;
  19. import java.util.List;
  20. import org.aspectj.ajdt.internal.core.builder.AjBuildManager;
  21. import org.aspectj.bridge.AbortException;
  22. import org.aspectj.bridge.ICommand;
  23. import org.aspectj.bridge.IMessage;
  24. import org.aspectj.bridge.IMessage.Kind;
  25. import org.aspectj.bridge.IMessageHandler;
  26. import org.aspectj.bridge.IMessageHolder;
  27. import org.aspectj.bridge.ISourceLocation;
  28. import org.aspectj.bridge.Message;
  29. import org.aspectj.bridge.MessageHandler;
  30. import org.aspectj.bridge.MessageUtil;
  31. import org.aspectj.bridge.ReflectionFactory;
  32. import org.aspectj.bridge.Version;
  33. import org.aspectj.bridge.context.CompilationAndWeavingContext;
  34. import org.aspectj.util.FileUtil;
  35. import org.aspectj.util.LangUtil;
  36. import org.aspectj.weaver.Dump;
  37. /**
  38. * Programmatic and command-line interface to AspectJ compiler. The compiler is an ICommand obtained by reflection. Not thread-safe.
  39. * By default, messages are printed as they are emitted; info messages go to the output stream, and warnings and errors go to the
  40. * error stream.
  41. * <p>
  42. * Clients can handle all messages by registering a holder:
  43. *
  44. * <pre>
  45. * Main main = new Main();
  46. * IMessageHolder holder = new MessageHandler();
  47. * main.setHolder(holder);
  48. * </pre>
  49. *
  50. * Clients can get control after each command completes by installing a Runnable:
  51. *
  52. * <pre>
  53. * main.setCompletionRunner(new Runnable() {..});
  54. * </pre>
  55. *
  56. */
  57. public class Main {
  58. /** Header used when rendering exceptions for users */
  59. public static final String THROWN_PREFIX = "Exception thrown from AspectJ " + Version.getText() + LangUtil.EOL + "" + LangUtil.EOL
  60. + "This might be logged as a bug already -- find current bugs at" + LangUtil.EOL
  61. + " http://bugs.eclipse.org/bugs/buglist.cgi?product=AspectJ&component=Compiler" + LangUtil.EOL + "" + LangUtil.EOL
  62. + "Bugs for exceptions thrown have titles File:line from the top stack, " + LangUtil.EOL
  63. + "e.g., \"SomeFile.java:243\"" + LangUtil.EOL + "" + LangUtil.EOL
  64. + "If you don't find the exception below in a bug, please add a new bug" + LangUtil.EOL
  65. + "at http://bugs.eclipse.org/bugs/enter_bug.cgi?product=AspectJ" + LangUtil.EOL
  66. + "To make the bug a priority, please include a test program" + LangUtil.EOL + "that can reproduce this exception."
  67. + LangUtil.EOL;
  68. private static final String OUT_OF_MEMORY_MSG = "AspectJ " + Version.getText() + " ran out of memory during compilation:"
  69. + LangUtil.EOL + LangUtil.EOL + "Please increase the memory available to ajc by editing the ajc script " + LangUtil.EOL
  70. + "found in your AspectJ installation directory. The -Xmx parameter value" + LangUtil.EOL
  71. + "should be increased from 64M (default) to 128M or even 256M." + LangUtil.EOL + LangUtil.EOL
  72. + "See the AspectJ FAQ available from the documentation link" + LangUtil.EOL
  73. + "on the AspectJ home page at http://www.eclipse.org/aspectj";
  74. private static final String MESSAGE_HOLDER_OPTION = "-messageHolder";
  75. /** @param args the String[] of command-line arguments */
  76. public static void main(String[] args) throws IOException {
  77. new Main().runMain(args, true);
  78. }
  79. /**
  80. * Convenience method to run ajc and collect String lists of messages. This can be reflectively invoked with the List collecting
  81. * parameters supplied by a parent class loader. The String messages take the same form as command-line messages.
  82. * This method does not catch unchecked exceptions thrown by the compiler.
  83. *
  84. * @param args the String[] args to pass to the compiler
  85. * @param useSystemExit if true and errors, return System.exit(errs)
  86. * @param fails the List sink, if any, for String failure (or worse) messages
  87. * @param errors the List sink, if any, for String error messages
  88. * @param warnings the List sink, if any, for String warning messages
  89. * @param infos the List sink, if any, for String info messages
  90. * @return number of messages reported with level ERROR or above
  91. */
  92. public static int bareMain(String[] args, boolean useSystemExit, List fails, List errors, List warnings, List infos) {
  93. Main main = new Main();
  94. MessageHandler holder = new MessageHandler();
  95. main.setHolder(holder);
  96. try {
  97. main.runMain(args, useSystemExit);
  98. } finally {
  99. readMessages(holder, IMessage.FAIL, true, fails);
  100. readMessages(holder, IMessage.ERROR, false, errors);
  101. readMessages(holder, IMessage.WARNING, false, warnings);
  102. readMessages(holder, IMessage.INFO, false, infos);
  103. }
  104. return holder.numMessages(IMessage.ERROR, true);
  105. }
  106. /** Read messages of a given kind into a List as String */
  107. private static void readMessages(IMessageHolder holder, IMessage.Kind kind, boolean orGreater, List sink) {
  108. if ((null == sink) || (null == holder)) {
  109. return;
  110. }
  111. IMessage[] messages = holder.getMessages(kind, orGreater);
  112. if (!LangUtil.isEmpty(messages)) {
  113. for (IMessage message : messages) {
  114. sink.add(MessagePrinter.render(message));
  115. }
  116. }
  117. }
  118. /**
  119. * @return String rendering throwable as compiler error for user/console, including information on how to report as a bug.
  120. * @throws NullPointerException if thrown is null
  121. */
  122. public static String renderExceptionForUser(Throwable thrown) {
  123. String m = thrown.getMessage();
  124. return THROWN_PREFIX + (null != m ? m + "\n" : "") + "\n" + CompilationAndWeavingContext.getCurrentContext()
  125. + LangUtil.renderException(thrown, true);
  126. }
  127. private static String parmInArgs(String flag, String[] args) {
  128. int loc = 1 + (null == args ? -1 : Arrays.asList(args).indexOf(flag));
  129. return ((0 == loc) || (args.length <= loc) ? null : args[loc]);
  130. }
  131. private static boolean flagInArgs(String flag, String[] args) {
  132. return ((null != args) && (Arrays.asList(args).contains(flag)));
  133. }
  134. /**
  135. * append nothing if numItems is 0, numItems + label + (numItems > 1? "s" : "") otherwise, prefixing with " " if sink has
  136. * content
  137. */
  138. private static void appendNLabel(StringBuffer sink, String label, int numItems) {
  139. if (0 == numItems) {
  140. return;
  141. }
  142. if (0 < sink.length()) {
  143. sink.append(", ");
  144. }
  145. sink.append(numItems + " ");
  146. if (!LangUtil.isEmpty(label)) {
  147. sink.append(label);
  148. }
  149. if (1 < numItems) {
  150. sink.append("s");
  151. }
  152. }
  153. /** control iteration/continuation for command (compiler) */
  154. protected CommandController controller;
  155. /** ReflectionFactory identifier for command (compiler) */
  156. protected String commandName;
  157. protected ICommand command;
  158. /** client-set message sink */
  159. private IMessageHolder clientHolder;
  160. /** internally-set message sink */
  161. protected final MessageHandler ourHandler;
  162. private int lastFails;
  163. private int lastErrors;
  164. /** if not null, run this synchronously after each compile completes */
  165. private Runnable completionRunner;
  166. public Main() {
  167. controller = new CommandController();
  168. commandName = ReflectionFactory.ECLIPSE;
  169. CompilationAndWeavingContext.setMultiThreaded(false);
  170. try {
  171. String value = System.getProperty("aspectj.multithreaded");
  172. if (value != null && value.equalsIgnoreCase("true")) {
  173. CompilationAndWeavingContext.setMultiThreaded(true);
  174. }
  175. } catch (Exception e) {
  176. // silent
  177. }
  178. ourHandler = new MessageHandler(true);
  179. }
  180. public MessageHandler getMessageHandler() {
  181. return ourHandler;
  182. }
  183. // for unit testing...
  184. void setController(CommandController controller) {
  185. this.controller = controller;
  186. }
  187. public void setCommand(ICommand command) {
  188. this.command = command;
  189. }
  190. /**
  191. * Run without throwing exceptions but optionally using System.exit(..). This sets up a message handler which emits messages
  192. * immediately, so report(boolean, IMessageHandler) only reports total number of errors or warnings.
  193. *
  194. * @param args the String[] command line for the compiler
  195. * @param useSystemExit if true, use System.exit(int) to complete unless one of the args is -noExit. and signal result (0 no
  196. * exceptions/error, &lt;0 exceptions, &gt;0 compiler errors).
  197. */
  198. public void runMain(String[] args, boolean useSystemExit) {
  199. // Urk - default no check for AJDT, enabled here for Ant, command-line
  200. AjBuildManager.enableRuntimeVersionCheck(this);
  201. final boolean verbose = flagInArgs("-verbose", args);
  202. final boolean timers = flagInArgs("-timers", args);
  203. if (null == this.clientHolder) {
  204. this.clientHolder = checkForCustomMessageHolder(args);
  205. }
  206. IMessageHolder holder = clientHolder;
  207. if (null == holder) {
  208. holder = ourHandler;
  209. if (verbose) {
  210. ourHandler.setInterceptor(MessagePrinter.VERBOSE);
  211. } else {
  212. ourHandler.ignore(IMessage.INFO);
  213. ourHandler.setInterceptor(MessagePrinter.TERSE);
  214. }
  215. }
  216. // make sure we handle out of memory gracefully...
  217. try {
  218. // byte[] b = new byte[100000000]; for testing OoME only!
  219. long stime = System.currentTimeMillis();
  220. // uncomment next line to pause before startup (attach jconsole)
  221. // try {Thread.sleep(5000); }catch(Exception e) {}
  222. run(args, holder);
  223. long etime = System.currentTimeMillis();
  224. if (timers) {
  225. System.out.println("Compiler took " + (etime - stime) + "ms");
  226. }
  227. holder.handleMessage(MessageUtil.info("Compiler took " + (etime - stime) + "ms"));
  228. // uncomment next line to pause at end (keeps jconsole alive!)
  229. // try { System.in.read(); } catch (Exception e) {}
  230. } catch (OutOfMemoryError outOfMemory) {
  231. IMessage outOfMemoryMessage = new Message(OUT_OF_MEMORY_MSG, null, true);
  232. holder.handleMessage(outOfMemoryMessage);
  233. System.exit(-1); // we can't reasonably continue from this point.
  234. } finally {
  235. CompilationAndWeavingContext.reset();
  236. Dump.reset();
  237. }
  238. boolean skipExit = false;
  239. if (useSystemExit && !LangUtil.isEmpty(args)) { // sigh - pluck -noExit
  240. for (String arg : args) {
  241. if ("-noExit".equals(arg)) {
  242. skipExit = true;
  243. break;
  244. }
  245. }
  246. }
  247. if (useSystemExit && !skipExit) {
  248. systemExit(holder);
  249. }
  250. }
  251. // put calls around run() call above to allowing connecting jconsole
  252. // private void pause(int ms) {
  253. // try {
  254. // System.err.println("Pausing for "+ms+"ms");
  255. // System.gc();
  256. // Thread.sleep(ms);
  257. // System.gc();
  258. // System.err.println("Continuing");
  259. // } catch (Exception e) {}
  260. // }
  261. /**
  262. * @param args
  263. */
  264. private IMessageHolder checkForCustomMessageHolder(String[] args) {
  265. IMessageHolder holder = null;
  266. final String customMessageHolder = parmInArgs(MESSAGE_HOLDER_OPTION, args);
  267. if (customMessageHolder != null) {
  268. try {
  269. holder = (IMessageHolder) Class.forName(customMessageHolder).newInstance();
  270. } catch (Exception ex) {
  271. holder = ourHandler;
  272. throw new AbortException("Failed to create custom message holder of class '" + customMessageHolder + "' : " + ex);
  273. }
  274. }
  275. return holder;
  276. }
  277. /**
  278. * Run without using System.exit(..), putting all messages in holder:
  279. * <ul>
  280. * <li>ERROR: compiler error</li>
  281. * <li>WARNING: compiler warning</li>
  282. * <li>FAIL: command error (bad arguments, exception thrown)</li>
  283. * </ul>
  284. * This handles incremental behavior:
  285. * <ul>
  286. * <li>If args include "-incremental", repeat for every input char until 'q' is entered.
  287. * <li>
  288. * <li>If args include "-incrementalTagFile {file}", repeat every time we detect that {file} modification time has changed.</li>
  289. * <li>Either way, list files recompiled each time if args includes "-verbose".</li>
  290. * <li>Exit when the commmand/compiler throws any Throwable.</li>
  291. * </ul>
  292. * When complete, this contains all the messages of the final run of the command and/or any FAIL messages produced in running
  293. * the command, including any Throwable thrown by the command itself.
  294. *
  295. * @param args the String[] command line for the compiler
  296. * @param holder the MessageHandler sink for messages.
  297. */
  298. public void run(String[] args, IMessageHolder holder) {
  299. PrintStream logStream = null;
  300. FileOutputStream fos = null;
  301. String logFileName = parmInArgs("-log", args);
  302. if (null != logFileName) {
  303. File logFile = new File(logFileName);
  304. try {
  305. logFile.createNewFile();
  306. fos = new FileOutputStream(logFileName, true);
  307. logStream = new PrintStream(fos, true);
  308. } catch (Exception e) {
  309. fail(holder, "Couldn't open log file: " + logFileName, e);
  310. }
  311. Date now = new Date();
  312. logStream.println(now.toString());
  313. if (flagInArgs("-verbose", args)) {
  314. ourHandler.setInterceptor(new LogModeMessagePrinter(true, logStream));
  315. } else {
  316. ourHandler.ignore(IMessage.INFO);
  317. ourHandler.setInterceptor(new LogModeMessagePrinter(false, logStream));
  318. }
  319. holder = ourHandler;
  320. }
  321. if (LangUtil.isEmpty(args)) {
  322. args = new String[] { "-?" };
  323. } else if (controller.running()) {
  324. fail(holder, "already running with controller: " + controller, null);
  325. return;
  326. }
  327. args = controller.init(args, holder);
  328. if (0 < holder.numMessages(IMessage.ERROR, true)) {
  329. return;
  330. }
  331. if (command == null) {
  332. command = ReflectionFactory.makeCommand(commandName, holder);
  333. }
  334. if (0 < holder.numMessages(IMessage.ERROR, true)) {
  335. return;
  336. }
  337. try {
  338. outer: while (true) {
  339. boolean passed = command.runCommand(args, holder);
  340. if (report(passed, holder) && controller.incremental()) {
  341. while (controller.doRepeatCommand(command)) {
  342. holder.clearMessages();
  343. if (controller.buildFresh()) {
  344. continue outer;
  345. } else {
  346. passed = command.repeatCommand(holder);
  347. }
  348. if (!report(passed, holder)) {
  349. break;
  350. }
  351. }
  352. }
  353. break;
  354. }
  355. } catch (AbortException ae) {
  356. if (ae.isSilent()) {
  357. quit();
  358. } else {
  359. IMessage message = ae.getIMessage();
  360. Throwable thrown = ae.getThrown();
  361. if (null == thrown) { // toss AbortException wrapper
  362. if (null != message) {
  363. holder.handleMessage(message);
  364. } else {
  365. fail(holder, "abort without message", ae);
  366. }
  367. } else if (null == message) {
  368. fail(holder, "aborted", thrown);
  369. } else {
  370. String mssg = MessageUtil.MESSAGE_MOST.renderToString(message);
  371. fail(holder, mssg, thrown);
  372. }
  373. }
  374. } catch (Throwable t) {
  375. fail(holder, "unexpected exception", t);
  376. } finally {
  377. if (logStream != null) {
  378. logStream.close();
  379. logStream = null;
  380. }
  381. if (fos != null) {
  382. try {
  383. fos.close();
  384. } catch (IOException e) {
  385. fail(holder, "unexpected exception", e);
  386. }
  387. fos = null;
  388. }
  389. command = null;
  390. }
  391. }
  392. /** call this to stop after the next iteration of incremental compile */
  393. public void quit() {
  394. controller.quit();
  395. }
  396. /**
  397. * Set holder to be passed all messages. When holder is set, messages will not be printed by default.
  398. *
  399. * @param holder the IMessageHolder sink for all messages (use null to restore default behavior)
  400. */
  401. public void setHolder(IMessageHolder holder) {
  402. clientHolder = holder;
  403. }
  404. public IMessageHolder getHolder() {
  405. return clientHolder;
  406. }
  407. /**
  408. * Install a Runnable to be invoked synchronously after each compile completes.
  409. *
  410. * @param runner the Runnable to invoke - null to disable
  411. */
  412. public void setCompletionRunner(Runnable runner) {
  413. this.completionRunner = runner;
  414. }
  415. /**
  416. * Call System.exit(int) with values derived from the number of failures/aborts or errors in messages.
  417. *
  418. * @param messages the IMessageHolder to interrogate.
  419. */
  420. protected void systemExit(IMessageHolder messages) {
  421. int num = lastFails; // messages.numMessages(IMessage.FAIL, true);
  422. if (0 < num) {
  423. System.exit(-num);
  424. }
  425. num = lastErrors; // messages.numMessages(IMessage.ERROR, false);
  426. if (0 < num) {
  427. System.exit(num);
  428. }
  429. System.exit(0);
  430. }
  431. /** Messages to the user */
  432. protected void outMessage(String message) { // XXX coordinate with MessagePrinter
  433. System.out.print(message);
  434. System.out.flush();
  435. }
  436. /**
  437. * Report results from a (possibly-incremental) compile run. This delegates to any reportHandler or otherwise prints summary
  438. * counts of errors/warnings to System.err (if any errors) or System.out (if only warnings). WARNING: this silently ignores
  439. * other messages like FAIL, but clears the handler of all messages when returning true. XXX false
  440. *
  441. * This implementation ignores the pass parameter but clears the holder after reporting on the assumption messages were
  442. * handled/printed already. (ignoring UnsupportedOperationException from holder.clearMessages()).
  443. *
  444. * @param pass true result of the command
  445. * @param holder IMessageHolder with messages from the command
  446. * @return false if the process should abort
  447. */
  448. protected boolean report(boolean pass, IMessageHolder holder) {
  449. lastFails = holder.numMessages(IMessage.FAIL, true);
  450. boolean result = (0 == lastFails);
  451. final Runnable runner = completionRunner;
  452. if (null != runner) {
  453. runner.run();
  454. }
  455. if (holder == ourHandler) {
  456. lastErrors = holder.numMessages(IMessage.ERROR, false);
  457. int warnings = holder.numMessages(IMessage.WARNING, false);
  458. StringBuffer sb = new StringBuffer();
  459. appendNLabel(sb, "fail|abort", lastFails);
  460. appendNLabel(sb, "error", lastErrors);
  461. appendNLabel(sb, "warning", warnings);
  462. if (0 < sb.length()) {
  463. PrintStream out = (0 < (lastErrors + lastFails) ? System.err : System.out);
  464. out.println(""); // XXX "wrote class file" messages no eol?
  465. out.println(sb.toString());
  466. }
  467. }
  468. return result;
  469. }
  470. /** convenience API to make fail messages (without MessageUtils's fail prefix) */
  471. protected static void fail(IMessageHandler handler, String message, Throwable thrown) {
  472. handler.handleMessage(new Message(message, IMessage.FAIL, thrown, null));
  473. }
  474. /**
  475. * interceptor IMessageHandler to print as we go. This formats all messages to the user.
  476. */
  477. public static class MessagePrinter implements IMessageHandler {
  478. public static final IMessageHandler VERBOSE = new MessagePrinter(true);
  479. public static final IMessageHandler TERSE = new MessagePrinter(false);
  480. final boolean verbose;
  481. protected MessagePrinter(boolean verbose) {
  482. this.verbose = verbose;
  483. }
  484. /**
  485. * Print errors and warnings to System.err, and optionally info to System.out, rendering message String only.
  486. *
  487. * @return false always
  488. */
  489. @Override
  490. public boolean handleMessage(IMessage message) {
  491. if (null != message) {
  492. PrintStream out = getStreamFor(message.getKind());
  493. if (null != out) {
  494. out.println(render(message));
  495. }
  496. }
  497. return false;
  498. }
  499. /**
  500. * Render message differently. If abort, then prefix stack trace with feedback request. If the actual message is empty, then
  501. * use toString on the whole. Prefix message part with file:line; If it has context, suffix message with context.
  502. *
  503. * @param message the IMessage to render
  504. * @return String rendering IMessage (never null)
  505. */
  506. public static String render(IMessage message) {
  507. // IMessage.Kind kind = message.getKind();
  508. StringBuffer sb = new StringBuffer();
  509. String text = message.getMessage();
  510. if (text.equals(AbortException.NO_MESSAGE_TEXT)) {
  511. text = null;
  512. }
  513. boolean toString = (LangUtil.isEmpty(text));
  514. if (toString) {
  515. text = message.toString();
  516. }
  517. ISourceLocation loc = message.getSourceLocation();
  518. String context = null;
  519. if (null != loc) {
  520. File file = loc.getSourceFile();
  521. if (null != file) {
  522. String name = file.getName();
  523. if (!toString || (!text.contains(name))) {
  524. sb.append(FileUtil.getBestPath(file));
  525. if (loc.getLine() > 0) {
  526. sb.append(":" + loc.getLine());
  527. }
  528. int col = loc.getColumn();
  529. if (0 < col) {
  530. sb.append(":" + col);
  531. }
  532. sb.append(" ");
  533. }
  534. }
  535. context = loc.getContext();
  536. }
  537. // per Wes' suggestion on dev...
  538. if (message.getKind() == IMessage.ERROR) {
  539. sb.append("[error] ");
  540. } else if (message.getKind() == IMessage.WARNING) {
  541. sb.append("[warning] ");
  542. }
  543. sb.append(text);
  544. if (null != context) {
  545. sb.append(LangUtil.EOL);
  546. sb.append(context);
  547. }
  548. String details = message.getDetails();
  549. if (details != null) {
  550. sb.append(LangUtil.EOL);
  551. sb.append('\t');
  552. sb.append(details);
  553. }
  554. Throwable thrown = message.getThrown();
  555. if (null != thrown) {
  556. sb.append(LangUtil.EOL);
  557. sb.append(Main.renderExceptionForUser(thrown));
  558. }
  559. if (message.getExtraSourceLocations().isEmpty()) {
  560. return sb.toString();
  561. } else {
  562. return MessageUtil.addExtraSourceLocations(message, sb.toString());
  563. }
  564. }
  565. @Override
  566. public boolean isIgnoring(IMessage.Kind kind) {
  567. return (null != getStreamFor(kind));
  568. }
  569. /**
  570. * No-op
  571. *
  572. * @see org.aspectj.bridge.IMessageHandler#isIgnoring(org.aspectj.bridge.IMessage.Kind)
  573. * @param kind
  574. */
  575. @Override
  576. public void dontIgnore(IMessage.Kind kind) {
  577. }
  578. /**
  579. * @return System.err for FAIL, ABORT, ERROR, and WARNING, System.out for INFO if -verbose and WEAVEINFO if -showWeaveInfo.
  580. */
  581. protected PrintStream getStreamFor(IMessage.Kind kind) {
  582. if (IMessage.WARNING.isSameOrLessThan(kind)) {
  583. return System.err;
  584. } else if (verbose && IMessage.INFO.equals(kind)) {
  585. return System.out;
  586. } else if (IMessage.WEAVEINFO.equals(kind)) {
  587. return System.out;
  588. } else {
  589. return null;
  590. }
  591. }
  592. /**
  593. * No-op
  594. *
  595. * @see org.aspectj.bridge.IMessageHandler#ignore(org.aspectj.bridge.IMessage.Kind)
  596. * @param kind
  597. */
  598. @Override
  599. public void ignore(Kind kind) {
  600. }
  601. }
  602. public static class LogModeMessagePrinter extends MessagePrinter {
  603. protected final PrintStream logStream;
  604. public LogModeMessagePrinter(boolean verbose, PrintStream logStream) {
  605. super(verbose);
  606. this.logStream = logStream;
  607. }
  608. @Override
  609. protected PrintStream getStreamFor(IMessage.Kind kind) {
  610. if (IMessage.WARNING.isSameOrLessThan(kind)) {
  611. return logStream;
  612. } else if (verbose && IMessage.INFO.equals(kind)) {
  613. return logStream;
  614. } else if (IMessage.WEAVEINFO.equals(kind)) {
  615. return logStream;
  616. } else {
  617. return null;
  618. }
  619. }
  620. }
  621. /** controller for repeatable command delays until input or file changed or removed */
  622. public static class CommandController {
  623. public static String TAG_FILE_OPTION = "-XincrementalFile";
  624. public static String INCREMENTAL_OPTION = "-incremental";
  625. /** maximum 10-minute delay between filesystem checks */
  626. public static long MAX_DELAY = 1000 * 600;
  627. /** default 5-second delay between filesystem checks */
  628. public static long DEFAULT_DELAY = 1000 * 5;
  629. /** @see init(String[]) */
  630. private static String[][] OPTIONS = new String[][] { new String[] { INCREMENTAL_OPTION },
  631. new String[] { TAG_FILE_OPTION, null } };
  632. /** true between init(String[]) and doRepeatCommand() that returns false */
  633. private boolean running;
  634. /** true after quit() called */
  635. private boolean quit;
  636. /** true if incremental mode, waiting for input other than 'q' */
  637. private boolean incremental;
  638. /** true if incremental mode, waiting for file to change (repeat) or disappear (quit) */
  639. private File tagFile;
  640. /** last modification time for tagFile as of last command - 0 to start */
  641. private long fileModTime;
  642. /** delay between filesystem checks for tagFile modification time */
  643. private long delay;
  644. /** true just after user types 'r' for rebuild */
  645. private boolean buildFresh;
  646. public CommandController() {
  647. delay = DEFAULT_DELAY;
  648. }
  649. /**
  650. * @param args read and strip incremental args from this
  651. * @param sink IMessageHandler for error messages
  652. * @return String[] remainder of args
  653. */
  654. public String[] init(String[] args, IMessageHandler sink) {
  655. running = true;
  656. // String[] unused;
  657. if (!LangUtil.isEmpty(args)) {
  658. String[][] options = LangUtil.copyStrings(OPTIONS);
  659. /* unused = */LangUtil.extractOptions(args, options);
  660. incremental = (null != options[0][0]);
  661. if (null != options[1][0]) {
  662. File file = new File(options[1][1]);
  663. if (!file.exists()) {
  664. MessageUtil.abort(sink, "tag file does not exist: " + file);
  665. } else {
  666. tagFile = file;
  667. fileModTime = tagFile.lastModified();
  668. }
  669. }
  670. }
  671. return args;
  672. }
  673. /**
  674. * @return true if init(String[]) called but doRepeatCommand has not returned false
  675. */
  676. public boolean running() {
  677. return running;
  678. }
  679. /** @param delay milliseconds between filesystem checks */
  680. public void setDelay(long delay) {
  681. if ((delay > -1) && (delay < MAX_DELAY)) {
  682. this.delay = delay;
  683. }
  684. }
  685. /** @return true if INCREMENTAL_OPTION or TAG_FILE_OPTION was in args */
  686. public boolean incremental() {
  687. return (incremental || (null != tagFile));
  688. }
  689. /** @return true if INCREMENTAL_OPTION was in args */
  690. public boolean commandLineIncremental() {
  691. return incremental;
  692. }
  693. public void quit() {
  694. if (!quit) {
  695. quit = true;
  696. }
  697. }
  698. /** @return true just after user typed 'r' */
  699. boolean buildFresh() {
  700. return buildFresh;
  701. }
  702. /** @return false if we should quit, true to do another command */
  703. boolean doRepeatCommand(ICommand command) {
  704. if (!running) {
  705. return false;
  706. }
  707. boolean result = false;
  708. if (quit) {
  709. result = false;
  710. } else if (incremental) {
  711. try {
  712. if (buildFresh) { // reset before input request
  713. buildFresh = false;
  714. }
  715. System.out.println(" press enter to recompile, r to rebuild, q to quit: ");
  716. System.out.flush();
  717. // boolean doMore = false;
  718. // seek for one q or a series of [\n\r]...
  719. do {
  720. int input = System.in.read();
  721. if ('q' == input) {
  722. break; // result = false;
  723. } else if ('r' == input) {
  724. buildFresh = true;
  725. result = true;
  726. } else if (('\n' == input) || ('\r' == input)) {
  727. result = true;
  728. } // else eat anything else
  729. } while (!result);
  730. System.in.skip(Integer.MAX_VALUE);
  731. } catch (IOException e) { // XXX silence for error?
  732. result = false;
  733. }
  734. } else if (null != tagFile) {
  735. long curModTime;
  736. while (true) {
  737. if (!tagFile.exists()) {
  738. result = false;
  739. break;
  740. } else if (fileModTime == (curModTime = tagFile.lastModified())) {
  741. fileCheckDelay();
  742. } else {
  743. fileModTime = curModTime;
  744. result = true;
  745. break;
  746. }
  747. }
  748. } // else, not incremental - false
  749. if (!result && running) {
  750. running = false;
  751. }
  752. return result;
  753. }
  754. /** delay between filesystem checks, returning if quit is set */
  755. protected void fileCheckDelay() {
  756. // final Thread thread = Thread.currentThread();
  757. long targetTime = System.currentTimeMillis() + delay;
  758. // long curTime;
  759. while (targetTime > System.currentTimeMillis()) {
  760. if (quit) {
  761. return;
  762. }
  763. try {
  764. Thread.sleep(300);
  765. } // 1/3-second delta for quit check
  766. catch (InterruptedException e) {
  767. }
  768. }
  769. }
  770. }
  771. }