1 package groovy.lang; 2 3 import java.io.IOException ; 4 import java.io.PrintWriter ; 5 import java.io.Writer ; 6 7 public class TracingInterceptor implements Interceptor { 8 9 protected Writer writer = new PrintWriter (System.out); 10 11 public Writer getWriter() { 12 return writer; 13 } 14 15 public void setWriter(Writer writer) { 16 this.writer = writer; 17 } 18 19 public Object beforeInvoke(Object object, String methodName, Object [] arguments) { 20 write(object, methodName, arguments, "before"); 21 return null; 22 } 23 24 public Object afterInvoke(Object object, String methodName, Object [] arguments, Object 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 object, String methodName, Object [] arguments, final String origin) { 34 try { 35 writer.write("Interceptor "); 36 writer.write(origin); 37 writer.write(" "); 38 Class theClass = object instanceof Class ? (Class ) object: object.getClass(); 39 writeInfo(theClass, methodName, arguments); 40 writer.write("\n"); 41 writer.flush(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 47 protected void writeInfo(final Class aClass, String methodName, Object [] arguments) throws IOException { 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 argument = arguments[i]; 55 writer.write(argument.getClass().getName()); 56 } 57 writer.write(")"); 58 } 59 } 60 | Popular Tags |