This Interceptor traces method calls on the proxied object to a log. By default, the log is simply System.out ; however, that can be changed with the setWriter(Writer) method. A message will be written to output before a method is invoked and after a method is invoke
| 50 | * </pre> |
| 51 | */ |
| 52 | public class TracingInterceptor implements Interceptor { |
| 53 | |
| 54 | /** |
| 55 | * Writer used to emit trace output. |
| 56 | */ |
| 57 | protected Writer writer = new PrintWriter(System.out); |
| 58 | private int indent = 0; |
| 59 | |
| 60 | /** |
| 61 | * Returns the writer associated with this interceptor. |
| 62 | */ |
| 63 | public Writer getWriter() { |
| 64 | return writer; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Changes the writer associated with this interceptor. |
| 69 | */ |
| 70 | public void setWriter(Writer writer) { |
| 71 | this.writer = writer; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * {@inheritDoc} |
| 76 | */ |
| 77 | @Override |
| 78 | public Object beforeInvoke(Object object, String methodName, Object[] arguments) { |
| 79 | write(object, methodName, arguments, "before"); |
| 80 | indent++ ; |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * {@inheritDoc} |
| 86 | */ |
| 87 | @Override |
| 88 | public Object afterInvoke(Object object, String methodName, Object[] arguments, Object result) { |
| 89 | indent--; |
| 90 | write(object, methodName, arguments, "after "); |
| 91 | return result; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * {@inheritDoc} |
| 96 | */ |
| 97 | @Override |
| 98 | public boolean doInvoke() { |
| 99 | return true; |
| 100 | } |
| 101 | private String indent(){ |
| 102 | return " ".repeat(Math.max(0, indent)); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Writes a formatted trace line for the supplied invocation stage. |
| 107 | * |
| 108 | * @param object the receiver object |
| 109 | * @param methodName the invoked method name |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…