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.

Dump.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /* *******************************************************************
  2. * Copyright (c) 2004,2010 Contributors
  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. * Matthew Webster, IBM
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.PrintStream;
  16. import java.net.URL;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Date;
  19. import java.util.List;
  20. import java.util.Properties;
  21. import org.aspectj.bridge.IMessage;
  22. import org.aspectj.bridge.IMessageHolder;
  23. import org.aspectj.bridge.Version;
  24. import org.aspectj.weaver.tools.Trace;
  25. import org.aspectj.weaver.tools.TraceFactory;
  26. import org.aspectj.weaver.tools.Traceable;
  27. /**
  28. * @author Matthew Webster
  29. */
  30. public class Dump {
  31. public final static String DUMP_CONDITION_PROPERTY = "org.aspectj.weaver.Dump.condition";
  32. public final static String DUMP_DIRECTORY_PROPERTY = "org.aspectj.dump.directory";
  33. /* Format for unique filename based on date & time */
  34. private static final String FILENAME_PREFIX = "ajcore";
  35. // private static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  36. // private static final DateFormat timeFormat = new SimpleDateFormat("HHmmss.SSS");
  37. private static final String FILENAME_SUFFIX = "txt";
  38. public static final String UNKNOWN_FILENAME = "Unknown";
  39. public static final String DUMP_EXCLUDED = "Excluded";
  40. public static final String NULL_OR_EMPTY = "Empty";
  41. private static Class<?> exceptionClass;
  42. private static IMessage.Kind conditionKind = IMessage.ABORT;
  43. private static File directory = new File(".");
  44. private String reason;
  45. private String fileName;
  46. private PrintStream print;
  47. private static String[] savedCommandLine;
  48. private static List<String> savedFullClasspath;
  49. private static IMessageHolder savedMessageHolder;
  50. // private static Map<INode, WeakReference<INode>> nodes = Collections
  51. // .synchronizedMap(new WeakHashMap<INode, WeakReference<INode>>());
  52. private static String lastDumpFileName = UNKNOWN_FILENAME;
  53. private static boolean preserveOnNextReset = false;
  54. private static final Trace trace = TraceFactory.getTraceFactory().getTrace(Dump.class);
  55. /**
  56. * for testing only, so that we can verify dump contents after compilation has completely finished
  57. */
  58. public static void preserveOnNextReset() {
  59. preserveOnNextReset = true;
  60. }
  61. public static void reset() {
  62. if (preserveOnNextReset) {
  63. preserveOnNextReset = false;
  64. return;
  65. } else {
  66. // nodes.clear();
  67. savedMessageHolder = null;
  68. }
  69. }
  70. /*
  71. * Dump methods
  72. */
  73. public static String dump(String reason) {
  74. String fileName = UNKNOWN_FILENAME;
  75. Dump dump = null;
  76. try {
  77. dump = new Dump(reason);
  78. fileName = dump.getFileName();
  79. dump.dumpDefault();
  80. } finally {
  81. if (dump != null) {
  82. dump.close();
  83. }
  84. }
  85. return fileName;
  86. }
  87. public static String dumpWithException(Throwable th) {
  88. return dumpWithException(savedMessageHolder, th);
  89. }
  90. public static String dumpWithException(IMessageHolder messageHolder, Throwable th) {
  91. if (!getDumpOnException()) {
  92. return null;
  93. }
  94. if (trace.isTraceEnabled()) {
  95. trace.enter("dumpWithException", null, new Object[] { messageHolder, th });
  96. }
  97. String fileName = UNKNOWN_FILENAME;
  98. Dump dump = null;
  99. try {
  100. dump = new Dump(th.getClass().getName());
  101. fileName = dump.getFileName();
  102. dump.dumpException(messageHolder, th);
  103. } finally {
  104. if (dump != null) {
  105. dump.close();
  106. }
  107. }
  108. if (trace.isTraceEnabled()) {
  109. trace.exit("dumpWithException", fileName);
  110. }
  111. return fileName;
  112. }
  113. public static String dumpOnExit() {
  114. return dumpOnExit(savedMessageHolder, false);
  115. }
  116. public static String dumpOnExit(IMessageHolder messageHolder, boolean reset) {
  117. if (!getDumpOnException()) {
  118. return null;
  119. }
  120. if (trace.isTraceEnabled()) {
  121. trace.enter("dumpOnExit", null, messageHolder);
  122. }
  123. String fileName = UNKNOWN_FILENAME;
  124. if (!shouldDumpOnExit(messageHolder)) {
  125. fileName = DUMP_EXCLUDED;
  126. } else {
  127. Dump dump = null;
  128. try {
  129. dump = new Dump(conditionKind.toString());
  130. fileName = dump.getFileName();
  131. dump.dumpDefault(messageHolder);
  132. } finally {
  133. if (dump != null) {
  134. dump.close();
  135. }
  136. }
  137. }
  138. if (reset) {
  139. messageHolder.clearMessages();
  140. }
  141. if (trace.isTraceEnabled()) {
  142. trace.exit("dumpOnExit", fileName);
  143. }
  144. return fileName;
  145. }
  146. private static boolean shouldDumpOnExit(IMessageHolder messageHolder) {
  147. if (trace.isTraceEnabled()) {
  148. trace.enter("shouldDumpOnExit", null, messageHolder);
  149. }
  150. if (trace.isTraceEnabled()) {
  151. trace.event("shouldDumpOnExit", null, conditionKind);
  152. }
  153. boolean result = (messageHolder == null) || messageHolder.hasAnyMessage(conditionKind, true);
  154. if (trace.isTraceEnabled()) {
  155. trace.exit("shouldDumpOnExit", result);
  156. }
  157. return result;
  158. }
  159. /*
  160. * Dump configuration
  161. */
  162. public static void setDumpOnException(boolean b) {
  163. if (b) {
  164. exceptionClass = java.lang.Throwable.class;
  165. } else {
  166. exceptionClass = null;
  167. }
  168. }
  169. public static boolean setDumpDirectory(String directoryName) {
  170. if (trace.isTraceEnabled()) {
  171. trace.enter("setDumpDirectory", null, directoryName);
  172. }
  173. boolean success = false;
  174. File newDirectory = new File(directoryName);
  175. if (newDirectory.exists()) {
  176. directory = newDirectory;
  177. success = true;
  178. }
  179. if (trace.isTraceEnabled()) {
  180. trace.exit("setDumpDirectory", success);
  181. }
  182. return success;
  183. }
  184. public static boolean getDumpOnException() {
  185. return (exceptionClass != null);
  186. }
  187. public static boolean setDumpOnExit(IMessage.Kind condition) {
  188. if (trace.isTraceEnabled()) {
  189. trace.event("setDumpOnExit", null, condition);
  190. }
  191. conditionKind = condition;
  192. return true;
  193. }
  194. public static boolean setDumpOnExit(String condition) {
  195. for (IMessage.Kind kind : IMessage.KINDS) {
  196. if (kind.toString().equals(condition)) {
  197. return setDumpOnExit(kind);
  198. }
  199. }
  200. return false;
  201. }
  202. public static IMessage.Kind getDumpOnExit() {
  203. return conditionKind;
  204. }
  205. public static String getLastDumpFileName() {
  206. return lastDumpFileName;
  207. }
  208. public static void saveCommandLine(String[] args) {
  209. savedCommandLine = new String[args.length];
  210. System.arraycopy(args, 0, savedCommandLine, 0, args.length);
  211. }
  212. public static void saveFullClasspath(List<String> list) {
  213. savedFullClasspath = list;
  214. }
  215. public static void saveMessageHolder(IMessageHolder holder) {
  216. savedMessageHolder = holder;
  217. }
  218. // public static void registerNode(Class<?> module, INode newNode) {
  219. // if (trace.isTraceEnabled()) {
  220. // trace.enter("registerNode", null, new Object[] { module, newNode });
  221. // }
  222. //
  223. // // TODO surely this should preserve the module???? it never has....
  224. // nodes.put(newNode, new WeakReference<INode>(newNode));
  225. //
  226. // if (trace.isTraceEnabled()) {
  227. // trace.exit("registerNode", nodes.size());
  228. // }
  229. // }
  230. private Dump(String reason) {
  231. if (trace.isTraceEnabled()) {
  232. trace.enter("<init>", this, reason);
  233. }
  234. this.reason = reason;
  235. openDump();
  236. dumpAspectJProperties();
  237. dumpDumpConfiguration();
  238. if (trace.isTraceEnabled()) {
  239. trace.exit("<init>", this);
  240. }
  241. }
  242. public String getFileName() {
  243. return fileName;
  244. }
  245. private void dumpDefault() {
  246. dumpDefault(savedMessageHolder);
  247. }
  248. private void dumpDefault(IMessageHolder holder) {
  249. dumpSytemProperties();
  250. dumpCommandLine();
  251. dumpFullClasspath();
  252. dumpCompilerMessages(holder);
  253. // dumpNodes();
  254. }
  255. // private void dumpNodes() {
  256. //
  257. // IVisitor dumpVisitor = new IVisitor() {
  258. //
  259. // public void visitObject(Object obj) {
  260. // println(formatObj(obj));
  261. // }
  262. //
  263. // public void visitList(List list) {
  264. // println(list);
  265. // }
  266. // };
  267. //
  268. // Set<INode> keys = nodes.keySet();
  269. // for (INode dumpNode : keys) {
  270. // println("---- " + formatObj(dumpNode) + " ----");
  271. // try {
  272. // dumpNode.accept(dumpVisitor);
  273. // } catch (Exception ex) {
  274. // trace.error(formatObj(dumpNode).toString(), ex);
  275. // }
  276. // }
  277. // }
  278. private void dumpException(IMessageHolder messageHolder, Throwable th) {
  279. println("---- Exception Information ---");
  280. println(th);
  281. dumpDefault(messageHolder);
  282. }
  283. private void dumpAspectJProperties() {
  284. println("---- AspectJ Properties ---");
  285. println("AspectJ Compiler " + Version.getText() + " built on " + Version.getTimeText());
  286. }
  287. private void dumpDumpConfiguration() {
  288. println("---- Dump Properties ---");
  289. println("Dump file: " + fileName);
  290. println("Dump reason: " + reason);
  291. println("Dump on exception: " + (exceptionClass != null));
  292. println("Dump at exit condition: " + conditionKind);
  293. }
  294. private void dumpFullClasspath() {
  295. println("---- Full Classpath ---");
  296. if (savedFullClasspath != null && savedFullClasspath.size() > 0) {
  297. for (String fileName : savedFullClasspath) {
  298. File file = new File(fileName);
  299. println(file);
  300. }
  301. } else {
  302. println(NULL_OR_EMPTY);
  303. }
  304. }
  305. private void dumpSytemProperties() {
  306. println("---- System Properties ---");
  307. Properties props = System.getProperties();
  308. println(props);
  309. }
  310. private void dumpCommandLine() {
  311. println("---- Command Line ---");
  312. println(savedCommandLine);
  313. }
  314. private void dumpCompilerMessages(IMessageHolder messageHolder) {
  315. println("---- Compiler Messages ---");
  316. if (messageHolder != null) {
  317. for (IMessage message : messageHolder.getUnmodifiableListView()) {
  318. println(message.toString());
  319. }
  320. } else {
  321. println(NULL_OR_EMPTY);
  322. }
  323. }
  324. /*
  325. * Dump output
  326. */
  327. private void openDump() {
  328. if (print != null) {
  329. return;
  330. }
  331. Date now = new Date();
  332. fileName = FILENAME_PREFIX + "." + new SimpleDateFormat("yyyyMMdd").format(now) + "."
  333. + new SimpleDateFormat("HHmmss.SSS").format(now) + "." + FILENAME_SUFFIX;
  334. try {
  335. File file = new File(directory, fileName);
  336. print = new PrintStream(new FileOutputStream(file), true);
  337. trace.info("Dumping to " + file.getAbsolutePath());
  338. } catch (Exception ex) {
  339. print = System.err;
  340. trace.info("Dumping to stderr");
  341. fileName = UNKNOWN_FILENAME;
  342. }
  343. lastDumpFileName = fileName;
  344. }
  345. public void close() {
  346. print.close();
  347. }
  348. private void println(Object obj) {
  349. print.println(obj);
  350. }
  351. private void println(Object[] array) {
  352. if (array == null) {
  353. println(NULL_OR_EMPTY);
  354. return;
  355. }
  356. for (Object o : array) {
  357. print.println(o);
  358. }
  359. }
  360. private void println(Properties props) {
  361. for (Object o : props.keySet()) {
  362. String key = (String) o;
  363. String value = props.getProperty(key);
  364. print.println(key + "=" + value);
  365. }
  366. }
  367. private void println(Throwable th) {
  368. th.printStackTrace(print);
  369. }
  370. private void println(File file) {
  371. print.print(file.getAbsolutePath());
  372. if (!file.exists()) {
  373. println("(missing)");
  374. } else if (file.isDirectory()) {
  375. int count = file.listFiles().length;
  376. println("(" + count + " entries)");
  377. } else {
  378. println("(" + file.length() + " bytes)");
  379. }
  380. }
  381. @SuppressWarnings("rawtypes")
  382. private void println(List list) {
  383. if (list == null || list.isEmpty()) {
  384. println(NULL_OR_EMPTY);
  385. } else {
  386. for (Object o : list) {
  387. if (o instanceof Exception) {
  388. println((Exception) o);
  389. } else {
  390. println(o.toString());
  391. }
  392. }
  393. }
  394. }
  395. private static Object formatObj(Object obj) {
  396. /* These classes have a safe implementation of toString() */
  397. if (obj == null || obj instanceof String || obj instanceof Number || obj instanceof Boolean || obj instanceof Exception
  398. || obj instanceof Character || obj instanceof Class || obj instanceof File || obj instanceof StringBuffer
  399. || obj instanceof URL) {
  400. return obj;
  401. } else {
  402. try {
  403. /* Classes can provide an alternative implementation of toString() */
  404. if (obj instanceof Traceable) {
  405. Traceable t = (Traceable) obj;
  406. return t.toTraceString();
  407. } else {
  408. return obj.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(obj));
  409. }
  410. /* Object.hashCode() can be override and may thow an exception */
  411. } catch (Exception ex) {
  412. return obj.getClass().getName() + "@FFFFFFFF";
  413. }
  414. }
  415. }
  416. static {
  417. String exceptionName = System.getProperty("org.aspectj.weaver.Dump.exception", "true");
  418. if (!exceptionName.equals("false")) {
  419. setDumpOnException(true);
  420. }
  421. String conditionName = System.getProperty(DUMP_CONDITION_PROPERTY);
  422. if (conditionName != null) {
  423. setDumpOnExit(conditionName);
  424. }
  425. String directoryName = System.getProperty(DUMP_DIRECTORY_PROPERTY);
  426. if (directoryName != null) {
  427. setDumpDirectory(directoryName);
  428. }
  429. }
  430. public interface INode {
  431. void accept(IVisitor visior);
  432. }
  433. public interface IVisitor {
  434. void visitObject(Object s);
  435. void visitList(List list);
  436. }
  437. }