01: package groovy.lang;
02:
03: import java.io.IOException;
04: import java.io.PrintWriter;
05: import java.io.Writer;
06:
07: public class TracingInterceptor implements Interceptor {
08:
09: protected Writer writer = new PrintWriter(System.out);
10: private int indent = 0;
11:
12: public Writer getWriter() {
13: return writer;
14: }
15:
16: public void setWriter(Writer writer) {
17: this .writer = writer;
18: }
19:
20: public Object beforeInvoke(Object object, String methodName,
21: Object[] arguments) {
22: write(object, methodName, arguments, "before");
23: indent++;
24: return null;
25: }
26:
27: public Object afterInvoke(Object object, String methodName,
28: Object[] arguments, Object result) {
29: indent--;
30: write(object, methodName, arguments, "after ");
31: return result;
32: }
33:
34: public boolean doInvoke() {
35: return true;
36: }
37:
38: private String indent() {
39: StringBuffer result = new StringBuffer();
40: for (int i = 0; i < indent; i++) {
41: result.append(" ");
42: }
43: return result.toString();
44: }
45:
46: protected void write(Object object, String methodName,
47: Object[] arguments, final String origin) {
48: try {
49: writer.write(indent());
50: writer.write(origin);
51: writer.write(" ");
52: Class theClass = object instanceof Class ? (Class) object
53: : object.getClass();
54: writeInfo(theClass, methodName, arguments);
55: writer.write("\n");
56: writer.flush();
57: } catch (IOException e) {
58: e.printStackTrace();
59: }
60: }
61:
62: protected void writeInfo(final Class aClass, String methodName,
63: Object[] arguments) throws IOException {
64: writer.write(aClass.getName());
65: writer.write(".");
66: writer.write(methodName);
67: writer.write("(");
68: for (int i = 0; i < arguments.length; i++) {
69: if (i > 0)
70: writer.write(", ");
71: Object argument = arguments[i];
72: writer.write(argument.getClass().getName());
73: }
74: writer.write(")");
75: }
76: }
|