Friday, November 04, 2005

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:

Anonymous ZuX said...

How would you use the toromaki exactly? Is that a kind of decorator thinghy?

Tuesday, November 15, 2005 12:36:00 PM  
Blogger Chiaroscuro said...

Framko, do you have any example of use of your toromaki?

Sunday, November 27, 2005 4:20:00 AM  

Post a Comment

Links to this post:

Create a Link

<< Home