package dynaop.util;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Serializable method handle.
*
* @author Bob Lee (crazybob@crazybob.org)
*/
public class MethodHandle implements Serializable {
static long serialVersionUID = 0;
Class declaringClass;
String name;
Class[] parameterTypes;
public MethodHandle(Method method) {
this.declaringClass = method.getDeclaringClass();
this.name = method.getName();
this.parameterTypes = method.getParameterTypes();
}
public Method getMethod() throws NoSuchMethodException {
return declaringClass.getDeclaredMethod(this.name,
this.parameterTypes);
}
public static Map handleMethodKeys(Map map) {
Map handled = new HashMap();
for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
handled.put(
new MethodHandle((Method) entry.getKey()),
entry.getValue()
);
}
return handled;
}
public static Map unhandleMethodKeys(Map map) throws IOException {
try {
Map unhandled = new HashMap();
for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
Map.Entry entry = (Map.Entry) i.next();
unhandled.put(
((MethodHandle) entry.getKey()).getMethod(),
entry.getValue()
);
}
return unhandled;
}
catch (NoSuchMethodException e) {
throw NestedException.wrap(e);
}
}
}
|