Toromaki
Toromaki 1
package org.codesushi;
import java.lang.reflect.*;
import java.util.Arrays;
/**
* @author codesushi
*/
public class SushiDebugProxy implements InvocationHandler {
private Object obj;
public static Object newInstance(Object obj) {
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new SushiDebugProxy(obj));
}
private SushiDebugProxy(Object obj) {
this.obj = obj;
}
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
Object result;
try {
System.out.println("before method " + m.getName() + "(" + Arrays.asList(args) + ")");
result = m.invoke(obj, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
} catch (Exception e) {
throw new RuntimeException("unexpected invocation exception: " + e.getMessage());
} finally {
System.out.println("after method " + m.getName() + "(" + Arrays.asList(args) + ")");
}
return result;
}
}
2 Comments:
How would you use the toromaki exactly? Is that a kind of decorator thinghy?
Framko, do you have any example of use of your toromaki?
Post a Comment
<< Home