KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > groovy > lang > TracingInterceptor


1 package groovy.lang;
2
3 import java.io.IOException JavaDoc;
4 import java.io.PrintWriter JavaDoc;
5 import java.io.Writer JavaDoc;
6
7 public class TracingInterceptor implements Interceptor {
8
9     protected Writer JavaDoc writer = new PrintWriter JavaDoc(System.out);
10
11     public Writer JavaDoc getWriter() {
12         return writer;
13     }
14
15     public void setWriter(Writer JavaDoc writer) {
16         this.writer = writer;
17     }
18
19     public Object JavaDoc beforeInvoke(Object JavaDoc object, String JavaDoc methodName, Object JavaDoc[] arguments) {
20         write(object, methodName, arguments, "before");
21         return null;
22     }
23
24     public Object JavaDoc afterInvoke(Object JavaDoc object, String JavaDoc methodName, Object JavaDoc[] arguments, Object JavaDoc result) {
25         write(object, methodName, arguments, "after");
26         return result;
27     }
28
29     public boolean doInvoke() {
30         return true;
31     }
32
33     protected void write(Object JavaDoc object, String JavaDoc methodName, Object JavaDoc[] arguments, final String JavaDoc origin) {
34         try {
35             writer.write("Interceptor ");
36             writer.write(origin);
37             writer.write(" ");
38             Class JavaDoc theClass = object instanceof Class JavaDoc ? (Class JavaDoc) object: object.getClass();
39             writeInfo(theClass, methodName, arguments);
40             writer.write("\n");
41             writer.flush();
42         } catch (IOException JavaDoc e) {
43             e.printStackTrace();
44         }
45     }
46
47     protected void writeInfo(final Class JavaDoc aClass, String JavaDoc methodName, Object JavaDoc[] arguments) throws IOException JavaDoc {
48         writer.write(aClass.getName());
49         writer.write(".");
50         writer.write(methodName);
51         writer.write("(");
52         for (int i = 0; i < arguments.length; i++) {
53             if (i > 0) writer.write(", ");
54             Object JavaDoc argument = arguments[i];
55             writer.write(argument.getClass().getName());
56         }
57         writer.write(")");
58     }
59 }
60
Popular Tags