Guest User

Untitled

a guest
Oct 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import java.io.PrintStream;
  2.  
  3. /**
  4. *
  5. * This class provides support for printing trace messages into a stream.
  6. * Trace messages are printed before and after constructors and methods
  7. * are executed.
  8. * The messages are appended with the string representation of the objects
  9. * whose constructors and methods are being traced.
  10. * It defines one abstract pointcut for injecting that tracing functionality
  11. * into any application classes.
  12. *
  13. */
  14. abstract aspect Trace {
  15.  
  16. /*
  17. * Functional part
  18. */
  19.  
  20. /**
  21. * There are 3 trace levels (values of TRACELEVEL):
  22. * 0 - No messages are printed
  23. * 1 - Trace messages are printed, but there is no indentation
  24. * according to the call stack
  25. * 2 - Trace messages are printed, and they are indented
  26. * according to the call stack
  27. */
  28. public static int TRACELEVEL = 0;
  29. protected static PrintStream stream = null;
  30. protected static int callDepth = 0;
  31.  
  32. /**
  33. * Initialization.
  34. */
  35. public static void initStream(PrintStream s) {
  36. stream = s;
  37. }
  38.  
  39. protected static void traceEntry(String str, Object o) {
  40. if (TRACELEVEL == 0) return;
  41. if (TRACELEVEL == 2) callDepth++;
  42. printEntering(str + ": " + o.toString());
  43. }
  44.  
  45. protected static void traceExit(String str, Object o) {
  46. if (TRACELEVEL == 0) return;
  47. printExiting(str + ": " + o.toString());
  48. if (TRACELEVEL == 2) callDepth--;
  49. }
  50.  
  51. private static void printEntering(String str) {
  52. printIndent();
  53. stream.println("--> " + str);
  54. }
  55.  
  56. private static void printExiting(String str) {
  57. printIndent();
  58. stream.println("<-- " + str);
  59. }
  60.  
  61.  
  62. private static void printIndent() {
  63. for (int i = 0; i < callDepth; i++)
  64. stream.print(" ");
  65. }
  66.  
  67.  
  68. /*
  69. * Crosscut part
  70. */
  71.  
  72. /**
  73. * Application classes - left unspecified.
  74. */
  75. abstract pointcut myClass(Object obj);
  76. /**
  77. * The constructors in those classes.
  78. */
  79. pointcut myConstructor(Object obj): myClass(obj) && execution(new(..));
  80. /**
  81. * The methods of those classes.
  82. */
  83. // toString is called from within our advice, so we shouldn't
  84. // advise its executions. But if toString is overridden, even
  85. // this might not be enough, so we might want
  86. // && !cflow(execution(String toString()))
  87. pointcut myMethod(Object obj): myClass(obj) &&
  88. execution(* *(..)) && !execution(String toString());
  89.  
  90. before(Object obj): myConstructor(obj) {
  91. traceEntry("" + thisJoinPointStaticPart.getSignature(), obj);
  92. }
  93. after(Object obj): myConstructor(obj) {
  94. traceExit("" + thisJoinPointStaticPart.getSignature(), obj);
  95. }
  96.  
  97. before(Object obj): myMethod(obj) {
  98. traceEntry("" + thisJoinPointStaticPart.getSignature(), obj);
  99. }
  100. after(Object obj): myMethod(obj) {
  101. traceExit("" + thisJoinPointStaticPart.getSignature(), obj);
  102. }
  103. }
Add Comment
Please, Sign In to add comment