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.

CompilationResult.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Adrian Colyer,
  11. * ******************************************************************/
  12. package org.aspectj.tools.ajc;
  13. import java.util.Collections;
  14. import java.util.List;
  15. import org.aspectj.bridge.IMessage;
  16. /**
  17. * Utility class that makes the results of a compiler run available.
  18. * <p>
  19. * Instances of this class are returned by the Ajc.compile() and
  20. * doIncrementalCompile() methods (and the AjcTestCase.ajc() wrapper).
  21. * </p>
  22. * <p>
  23. * This class provides a useful toString() method that is very helpful when
  24. * debugging or creating messages for assert statements.
  25. * </p>
  26. * <p>Note that the stdOut and stdErr captured from the compiler run do
  27. * not contain any rendered messages - these are in the messages lists
  28. * instead. Therefore for many compiler runs, they will be empty.
  29. * </p>
  30. */
  31. public class CompilationResult {
  32. private String[] args;
  33. private String stdOut;
  34. private String stdErr;
  35. private List<IMessage> infoMessages;
  36. private List<IMessage> errorMessages;
  37. private List<IMessage> warningMessages;
  38. private List<IMessage> failMessages;
  39. private List<IMessage> weaveMessages;
  40. /**
  41. * Build a compilation result - called by the Ajc.compile and
  42. * Ajc.doIncrementalCompile methods. Should be no need for you
  43. * to construct an instance yourself.
  44. */
  45. protected CompilationResult(
  46. String[] args,
  47. String stdOut,
  48. String stdErr,
  49. List<IMessage> infoMessages,
  50. List<IMessage> errorMessages,
  51. List<IMessage> warningMessages,
  52. List<IMessage> failMessages,
  53. List<IMessage> weaveMessages) {
  54. this.args = args;
  55. this.stdOut = stdOut;
  56. this.stdErr = stdErr;
  57. this.infoMessages = (infoMessages == null) ? Collections.<IMessage>emptyList() : infoMessages;
  58. this.errorMessages = (errorMessages == null) ? Collections.<IMessage>emptyList() : errorMessages;
  59. this.warningMessages = (warningMessages == null) ? Collections.<IMessage>emptyList() : warningMessages;
  60. this.failMessages = (failMessages == null) ? Collections.<IMessage>emptyList() : failMessages;
  61. this.weaveMessages = (weaveMessages == null) ? Collections.<IMessage>emptyList() : weaveMessages;
  62. }
  63. /**
  64. * The arguments that were passed to the compiler.
  65. */
  66. public String[] getArgs() { return args; }
  67. /**
  68. * The standard output written by the compiler, excluding any messages.
  69. */
  70. public String getStandardOutput() { return stdOut; }
  71. /**
  72. * The standard error written by the compiler, excluding any messages.
  73. */
  74. public String getStandardError() { return stdErr; }
  75. /**
  76. * True if the compiler issued any messages of any kind.
  77. */
  78. public boolean hasMessages() { return (hasInfoMessages() || hasErrorMessages() || hasWarningMessages() || hasFailMessages() || hasWeaveMessages()); }
  79. /**
  80. * True if the compiler issued one or more informational messages.
  81. */
  82. public boolean hasInfoMessages() { return !infoMessages.isEmpty(); }
  83. /**
  84. * True if the compiler issued one or more error messages.
  85. */
  86. public boolean hasErrorMessages() { return !errorMessages.isEmpty(); }
  87. /**
  88. * True if the compiler issued one or more warning messages.
  89. */
  90. public boolean hasWarningMessages() { return !warningMessages.isEmpty(); }
  91. /**
  92. * True if the compiler issued one or more fail or abort messages.
  93. */
  94. public boolean hasFailMessages() { return !failMessages.isEmpty(); }
  95. /**
  96. * True if the compiler issued one or more weave info messages.
  97. */
  98. public boolean hasWeaveMessages() { return !weaveMessages.isEmpty(); }
  99. /**
  100. * The informational messages produced by the compiler. The list
  101. * entries are the <code>IMessage</code> objects created during the
  102. * compile - so that you can programmatically test source locations
  103. * etc. etc.. It may often be easier to use the <code>assertMessages</code>
  104. * helper methods defined in the AjcTestCase class to test for messages
  105. * though.
  106. * @see org.aspectj.tools.ajc.AjcTestCase
  107. */
  108. public List<IMessage> getInfoMessages() { return infoMessages; }
  109. /**
  110. * The error messages produced by the compiler. The list
  111. * entries are the <code>IMessage</code> objects created during the
  112. * compile - so that you can programmatically test source locations
  113. * etc. etc.. It may often be easier to use the <code>assertMessages</code>
  114. * helper methods defined in the AjcTestCase class to test for messages
  115. * though.
  116. * @see org.aspectj.tools.ajc.AjcTestCase
  117. */
  118. public List<IMessage> getErrorMessages() { return errorMessages; }
  119. /**
  120. * The warning messages produced by the compiler. The list
  121. * entries are the <code>IMessage</code> objects created during the
  122. * compile - so that you can programmatically test source locations
  123. * etc. etc.. It may often be easier to use the <code>assertMessages</code>
  124. * helper methods defined in the AjcTestCase class to test for messages
  125. * though.
  126. * @see org.aspectj.tools.ajc.AjcTestCase
  127. */
  128. public List<IMessage> getWarningMessages() { return warningMessages; }
  129. /**
  130. * The fail or abort messages produced by the compiler. The list
  131. * entries are the <code>IMessage</code> objects created during the
  132. * compile - so that you can programmatically test source locations
  133. * etc. etc.. It may often be easier to use the <code>assertMessages</code>
  134. * helper methods defined in the AjcTestCase class to test for messages
  135. * though.
  136. * @see org.aspectj.tools.ajc.AjcTestCase
  137. */
  138. public List<IMessage> getFailMessages() { return failMessages; }
  139. public List<IMessage> getWeaveMessages() { return weaveMessages; }
  140. /**
  141. * Returns string containing message count summary, list of messages
  142. * by type, and the actual ajc compilation command that was issued.
  143. */
  144. @Override
  145. public String toString() {
  146. StringBuilder buff = new StringBuilder();
  147. buff.append("AspectJ Compilation Result:\n");
  148. int totalMessages = infoMessages.size() + warningMessages.size() + errorMessages.size() + failMessages.size() + weaveMessages.size();
  149. buff.append(totalMessages);
  150. buff.append(" messages");
  151. if (totalMessages > 0) {
  152. buff.append(" (");
  153. buff.append(infoMessages.size());
  154. buff.append(" info, ");
  155. buff.append(warningMessages.size());
  156. buff.append(" warning, ");
  157. buff.append(errorMessages.size());
  158. buff.append(" error, ");
  159. buff.append(failMessages.size());
  160. buff.append(" fail, )");
  161. buff.append(weaveMessages.size());
  162. buff.append(" weaveInfo");
  163. }
  164. buff.append("\n");
  165. int msgNo = 1;
  166. for (IMessage failMessage : failMessages) {
  167. buff.append("[fail ");
  168. buff.append(msgNo++);
  169. buff.append("] ");
  170. buff.append(failMessage.toString());
  171. buff.append("\n");
  172. }
  173. msgNo = 1;
  174. for (IMessage errorMessage : errorMessages) {
  175. buff.append("[error ");
  176. buff.append(msgNo++);
  177. buff.append("] ");
  178. buff.append(errorMessage.toString());
  179. buff.append("\n");
  180. }
  181. msgNo = 1;
  182. for (IMessage warningMessage : warningMessages) {
  183. buff.append("[warning ");
  184. buff.append(msgNo++);
  185. buff.append("] ");
  186. buff.append(warningMessage.toString());
  187. buff.append("\n");
  188. }
  189. msgNo = 1;
  190. for (IMessage infoMessage : infoMessages) {
  191. buff.append("[info ");
  192. buff.append(msgNo++);
  193. buff.append("] ");
  194. buff.append(infoMessage.toString());
  195. buff.append("\n");
  196. }
  197. msgNo = 1;
  198. for (IMessage weaveMessage : weaveMessages) {
  199. buff.append("[weaveInfo ");
  200. buff.append(msgNo++);
  201. buff.append("] ");
  202. buff.append(weaveMessage.toString());
  203. buff.append("\n");
  204. }
  205. buff.append("\ncommand was: 'ajc");
  206. for (String arg : args) {
  207. buff.append(' ');
  208. buff.append(arg);
  209. }
  210. buff.append("'\n");
  211. return buff.toString();
  212. }
  213. }