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.

TraceJoinPoints.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import org.aspectj.lang.*;
  2. import org.aspectj.lang.reflect.*;
  3. import java.io.*;
  4. /**
  5. * Produces an XML representation of all of the join points in a program
  6. * within the control-flow of those points matching <code>entry</code>.
  7. *
  8. * To use this, extend this aspect and fill in an appropriate value for
  9. * <code>entry</code>.
  10. */
  11. public abstract aspect TraceJoinPoints dominates * {
  12. /**
  13. * The join points which mark the entry of the traced control-flow.
  14. * To trace all command-line programs, set this to:
  15. * <pre>call(public static void main(String[]));</pre>
  16. */
  17. protected abstract pointcut entry();
  18. /**
  19. * Join points which mark an exit from the control-flow. Use this
  20. * to exclude parts of the call-graph that you're not interested in.
  21. * The default value matches the current implementation limitation
  22. * (in ajc-1.0) that join points within system libraries are not
  23. * visible.
  24. */
  25. protected pointcut exit(): call(* java..*.*(..));
  26. final pointcut start(): entry() && !cflowbelow(entry());
  27. final pointcut trace():
  28. cflow(entry()) && !cflowbelow(exit()) && !within(TraceJoinPoints+);
  29. before(): start() { makeLogStream(); }
  30. before(): trace() { logEnter(thisJoinPointStaticPart); }
  31. after(): trace() { logExit(thisJoinPointStaticPart); }
  32. after(): start() { closeLogStream(); }
  33. PrintStream out;
  34. int logs = 0;
  35. protected void makeLogStream() {
  36. try {
  37. out = new PrintStream(new FileOutputStream("log" + logs++ + ".xml"));
  38. } catch (IOException ioe) {
  39. out = System.err;
  40. }
  41. }
  42. protected void closeLogStream() {
  43. out.close();
  44. }
  45. int depth = 0;
  46. boolean terminal = false;
  47. protected void logEnter(JoinPoint.StaticPart jp) {
  48. if (terminal) out.println(">");
  49. indent(depth);
  50. out.print("<" + jp.getKind());
  51. writeSig(jp);
  52. writePos(jp);
  53. depth += 1;
  54. terminal = true;
  55. }
  56. void writeSig(JoinPoint.StaticPart jp) {
  57. out.print(" sig=");
  58. out.print(quoteXml(jp.getSignature().toShortString()));
  59. }
  60. void writePos(JoinPoint.StaticPart jp) {
  61. SourceLocation loc = jp.getSourceLocation();
  62. if (loc == null) return;
  63. out.print(" pos=");
  64. out.print(quoteXml(loc.getFileName() +
  65. ":" + loc.getLine() +
  66. ":" + loc.getColumn()));
  67. }
  68. String quoteXml(String s) {
  69. return "\"" + s.replace('<', '_').replace('>', '_') + "\"";
  70. }
  71. protected void logExit(JoinPoint.StaticPart jp) {
  72. depth -= 1;
  73. if (terminal) {
  74. out.println("/>");
  75. } else {
  76. indent(depth);
  77. out.println("</" + jp.getKind() + ">");
  78. }
  79. terminal = false;
  80. }
  81. void indent(int i) {
  82. while (i-- > 0) out.print(" ");
  83. }
  84. }