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

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